3

From my android application, I am trying to delete an image that is stored in the tomcat's webapps directory. When I try the following code its giving me 403 status code. I looked up online and found it gives that code if the request is legal but the action is forbidden. Can anybody tell me where I am going wrong.My code is:

HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("DELETE");
int responseCode = connection.getResponseCode();

and when I tried using HttpClient, it gave me the same error- HTTP/1.1 403 Forbidden

HttpClient httpClient = new DefaultHttpClient();
                  try {
                    httpClient.getParams().setParameter(
                            "http.socket.timeout", new Integer(90000));
                    HttpDelete delete = new HttpDelete(new URI(
                            "http://192.168.2.1:9090/LocationUpdaterServlet/images/"
                                    + userid));
                    Toast.makeText(Image.this, "Removing your picture", 5000).show();
                    HttpResponse response = httpClient.execute(delete);
                    if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
                        System.out.println(response.getStatusLine());
                    } else {
                        // Here every thing is fine.
                    }
                    HttpEntity resEntity = response.getEntity();
                    if (resEntity == null)
                        System.out
                                .println("---------Error No Response !!!-----");
                }catch (Exception ex) {
                    System.out.println("---------Error----"+ ex.getMessage());
                    ex.printStackTrace();
                } finally {
                    httpClient.getConnectionManager().shutdown();

                }
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Ash
  • 95
  • 2
  • 3
  • 11

2 Answers2

3

In web.xml, enable other http methods with this:

<servlet>
    <servlet-name>default</servlet-name>
    <servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
    <init-param>
        <param-name>debug</param-name>
        <param-value>0</param-value>
    </init-param>
    <init-param>
        <param-name>listings</param-name>
        <param-value>false</param-value>
    </init-param>
    <init-param>
        <param-name>readonly</param-name>
        <param-value>false</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

Parameters debug and listings are loaded by default in tomcat, while the default readonly is true, meaning that only GET and POST are available.

Other params available are:

     debug               Debugging detail level for messages logged     
                         by this servlet.  [0]                          

     fileEncoding        Encoding to be used to read static resources   
                         [platform default]                             

     input               Input buffer size (in bytes) when reading      
                         resources to be served.  [2048]                

     listings            Should directory listings be produced if there 
                         is no welcome file in this directory?  [false] 
                         WARNING: Listings for directories with many    
                         entries can be slow and may consume            
                         significant proportions of server resources.   

     output              Output buffer size (in bytes) when writing     
                         resources to be served.  [2048]                

     readonly            Is this context "read only", so HTTP           
                         commands like PUT and DELETE are               
                         rejected?  [true]                              

     readmeFile          File to display together with the directory    
                         contents. [null]                               

     sendfileSize        If the connector used supports sendfile, this  
                         represents the minimal file size in KB for     
                         which sendfile will be used. Use a negative    
                         value to always disable sendfile.  [48]        

     useAcceptRanges     Should the Accept-Ranges header be included    
                         in responses where appropriate? [true]         

Moesio
  • 3,100
  • 1
  • 27
  • 36
0

Tomcat provides a servlet init parameter (readonly) than you can set to enable PUT, DELETE.

I was having a different though related issue and I noticed this parameter.

Check the documentation here for the information: http://tomcat.apache.org/tomcat-7.0-doc/default-servlet.html

Stephan
  • 41,764
  • 65
  • 238
  • 329
turf00
  • 31
  • 2