0

why am I getting HTTP Status 405 - HTTP method GET is not supported by this URL error in this Program.Well I completed 3 programs before this one and they just ran fine but this is showing the error.

package com.aamir;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Servlet4 extends HttpServlet {

   protected void service(HttpServletResponse res, HttpServletRequest req)
        throws IOException, ServletException {

             PrintWriter out = res.getWriter();
             res.setContentType("text/html");

             out.println("<em>through out</em>");
             System.out.println("through SOP");
        }
}

web.xml

<web-app>

   <servlet>
        <servlet-name>Servlet4</servlet-name>
        <servlet-class>com.aamir.Servlet4</servlet-class>
   </servlet>

   <servlet-mapping>
         <servlet-name>Servlet4</servlet-name>
         <url-pattern>/Servlet4</url-pattern>
   </servlet-mapping>

Aamir
  • 2,380
  • 3
  • 24
  • 54

1 Answers1

2

Here,

protected void service(HttpServletResponse res, HttpServletRequest req) throws IOException, ServletException {
    // ...
}

You swapped HttpServletResponse and HttpServletRequest and hence it didn't match the interface method and hence it's never invoked by the container. The container will still invoke the original service() method of the HttpServlet template class which in turn calls the original doGet() method of the HttpServlet template class which in turn returns a HTTP 405 error.

Swap them back to the right place and add @Override annotation:

@Override
protected void service(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
    // ...
}

This way the container will actually call the overriden service() method. Using the @Override annotation properly will cause a compilation error on wrong method signatures.

That said, you shouldn't have the need to override the service() method, unless you intend to homegrow another MVC controller (which is in turn questionable unless for hobby/learning purposes - see also Design Patterns web based applications).

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I am getting the same error after implementing what you refer, I don't know what the hell is wrong with my program, I tried to restarting the server but still same error. – Aamir Aug 19 '14 at 08:06
  • Clean, rebuild, redeploy, etc. Note that my answer is based on the information provided so far in the question. If you made other changes not mentioned in the question, you might want to revert them. – BalusC Aug 19 '14 at 08:08
  • @BalusC nice observation, just didn't struck my mind to examine the arguments of the `service()` method. – AllTooSir Aug 19 '14 at 08:23
  • @NINCOMPOOP: The error just indicates that the `doGet()` is called. Which means that OP's own `service()` isn't called. – BalusC Aug 19 '14 at 08:36