java - 此 URL 不支持 HTTP 方法 GET,尽管它执行 doGet

标签 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>

当我访问 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 开源版 3.1.2.2

最佳答案

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

关于java - 此 URL 不支持 HTTP 方法 GET,尽管它执行 doGet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16207536/

相关文章:

java - 无效的列类型 : getInt not implemented for class oracle. jdbc.driver.T4CRowidAccessor

mysql - 当行插入表时如何通过邮件收到通知?

Java HttpServletResponse 带有空白值 header ?

jsf - 项目上线时Manager Bean不返回

Java JLabel/JButton : on some systems I get "..." (an ellipsis) and on some systems I don't. 我怎样才能强制禁用省略号?

Java 泛型 : Question Type parameters inference in Java

security - 如何通过 ip 限制对特定 servlet 的访问 - 通过容器配置

glassfish-3 - 需要使用 Netbeans 运行 GlassFish 服务器

java - Glassfish - 上传图片 - 做对了

java - 错误消息 "non-static variable this cannot be referenced from a static context"第 18 行