【问题标题】:HTTP method GET is not supported by this URL though it executes doGet [duplicate]此 URL 不支持 HTTP 方法 GET,尽管它执行 doGet [重复]
【发布时间】:2013-04-18 22:07:21
【问题描述】:
public class RoarHistoryUpdate extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException{
        super.doGet(request, response);
        System.out.println("do Get");
        response.setContentType("text/html");
        response.getOutputStream().print("Success");
    }
}

这是我的 Servlet。它是这样在 web.xml 中注册的:

  <servlet>
      <display-name>RoarHistoryUpdateServlet</display-name>
      <servlet-name>RoarHistoryUpdateServlet</servlet-name>
      <servlet-class>de.ulm.uni.vs.avid.roary.servlets.RoarHistoryUpdate</servlet-class>
  </servlet>
  <servlet-mapping>
      <servlet-name>RoarHistoryUpdateServlet</servlet-name>
      <url-pattern>/Roary/UpdateServlet</url-pattern>
  </servlet-mapping>

当我访问 URL http://localhost:8080/Roary-JSP/Roary/UpdateServlet 时,它会显示 HTTP Status 405 - HTTP method GET is not supported by this URL

有趣的是,我将do Get 登录到我的控制台。所以它实际上找到了doGet-方法。

我使用的是 GlassFish Server Open Source Edition 3.1.2.2

【问题讨论】:

    标签: java servlets glassfish-3


    【解决方案1】:

    因为当您在 Servlet 的 doGet() 方法中执行 super.doGet(request, response); 时,您实际上调用了 HttpServlet 类的 doget()。该方法的Tomcat 7 实现如下(Glassfish 可能存在类似的实现):

    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException
    {
        String protocol = req.getProtocol();
        String msg = lStrings.getString("http.method_get_not_supported");
        if (protocol.endsWith("1.1")) {
            resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, msg);
        } else {
            resp.sendError(HttpServletResponse.SC_BAD_REQUEST, msg);
        }
    }
    

    【讨论】:

    • 是的,就是这样。谢谢!
    • 请将其标记为答案,因为它对您有帮助。
    【解决方案2】:

    我的猜测是因为调用了super.doGet()。如果您检查HttpServlet 的源代码,您会看到它在响应中设置了此状态代码。所以放弃超级电话。不需要。

    【讨论】:

      猜你喜欢
      • 2016-02-13
      • 2013-11-14
      • 1970-01-01
      • 1970-01-01
      • 2015-09-05
      相关资源
      最近更新 更多