HTTP Status 405 - HTTP method GET is not supported by this URL

穿精又带淫゛_ 提交于 2019-11-26 06:44:55

问题


I have the following servlet:

public class MyServlet extends HttpServlet {

    private static final long serialVersionUID = 16252534;
    private static int ping = 3000;
    private Thread t;
    private static boolean shouldStop = false;

    @Override
    public void init() throws ServletException {
        super.init();

        t = new Thread(new Runnable() { 
            @Override
            public void run() {
                while(!shouldStop) {
                    System.out.println(\"Now:\" + System.currentTimeMillis());
                    try {
                        Thread.sleep(ping);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        t.start();

    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {
        super.doGet(req, resp);
        System.out.println(\"doGet\");
        PrintWriter out = resp.getWriter();
        out.println(\"<html><h1>It works!!</h1></html>\");
    }

    @Override
    public void service(ServletRequest req, ServletResponse resp) throws ServletException, IOException {
        super.service(req, resp);
        System.out.println(\"service\");

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
        System.out.println(\"doPost\");

    }

    @Override
    public void destroy() {
        super.destroy();
        System.out.println(\"Destroy servlet\");
        shouldStop = true;
    }
}

Which is mapped as follows in my web.xml:

<display-name>MyServer</display-name>
<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<servlet>
    <servlet-name>MyServlet</servlet-name>
    <servlet-class>com.myserver.MyServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>      
   <servlet-mapping>
    <servlet-name>MyServlet</servlet-name>
    <url-pattern>/MyServlet</url-pattern>
</servlet-mapping>

When I open my browser (Chrome) on http://localhost:8080/MyServer/MyServlet, then I see \"service\" from doService() being logged on console and my thread works correctly, however I don\'t see \"It Works\" from doGet() being logged and I get the following error in the browser:

HTTP method GET is not supported by this URL

How is this caused and how can I solve it?


回答1:


This is the default response of the default implementation of HttpServlet#doGet(). This means that the doGet() method is not properly being @Overriden, or it is explicitly being called.

You did properly @Override the doGet() method, but you're still explicitly calling the default implementation for unclear reason.

super.doGet(req, resp);

Get rid of this line and this problem shall disappear.

The HttpServlet basically follows the template method pattern where all non-overridden HTTP methods returns this HTTP 405 error "Method not supported". When you override such a method, you should not call super method, because you would otherwise still get the HTTP 405 error. The same story goes on for your doPost() method.

This also applies on service() by the way, but that does technically not harm in this construct since you need it to let the default implementation execute the proper methods. Actually, the whole service() method is unnecessary for you, you can just remove the entire method from your servlet.

The super.init(); is also unnecessary. It's is only necessary when you override the init(ServletConfig), because otherwise the ServletConfig wouldn't be set. This is also explicitly mentioned in the javadoc. It's the only method which requires a super call.


Unrelated to the concrete problem, spawning a thread in a servlet like that is a bad idea. For the correct approach, head to How to run a background task in a servlet based web application?




回答2:


you have overridden the service method which is responsible to delegate the call to doGet or doPost. see this for more details
Also get rid of super.doxxx(..) calls from each method. Don't override the service method and you should see, "It Works" from doGet.



来源:https://stackoverflow.com/questions/5370633/http-status-405-http-method-get-is-not-supported-by-this-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!