2
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-method。

我正在使用 GlassFish Server 开源版 3.1.2.2

4

2 回答 2

8

因为当您super.doGet(request, response);在 Servlet 的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);
    }
}
于 2013-04-25T06:09:05.413 回答
2

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

于 2013-04-25T06:09:13.700 回答