405: HTTP method GET is not supported by this URL

405: HTTP method GET is not supported by this URL

?

先看错误代码:

?

public class WXSenderServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		super.doGet(req, resp);
		resp.getWriter().print("WXSenderServlet----doGet()");
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		super.doPost(req, resp);
		resp.getWriter().print("WXSenderServlet----doPost()");
	}

}

?继承了HttpServlet而且实现了两个方法,并且:

<!-- Servlets -->
    <servlet>
        <servlet-name>WXSenderServlet</servlet-name>
        <servlet-class>pa.wechat.plugin.servlet.WXSenderServlet</servlet-class>
    </servlet>

    <!-- Servlet mappings  url-pattern???é???°????-->
    <servlet-mapping>
        <servlet-name>WXSenderServlet</servlet-name>
        <url-pattern>/wechatsender</url-pattern>
    </servlet-mapping>

?那为什么还会有错误呢?

?

?

哈哈哈,原来是因为super.doGet(req, resp);里面不是空的。

?

看一位网友的回答的不错,照抄了过来:

?

The?HttpServlet?basically follows the?template method pattern?where all non-overridden HTTP methods returns a 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 alsoexplicitly?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 more detail, start reading?this answer.

?

?

?

恩,就是这样的。

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。