此URL虽然执行doGet,但不支持HTTP方法GET

时间:2013-04-25 06:06:39

标签: java servlets glassfish-3

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>

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

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

我使用的是GlassFish Server开源版3.1.2.2

2 个答案:

答案 0 :(得分:8)

因为当您在Servlet的super.doGet(request, response);方法中执行doGet()时,实际上是在调用doget()类的HttpServlet。该方法的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);
    }
}

答案 1 :(得分:2)

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