HTTP 状态 405 - 此 URL 不支持 HTTP 方法 GET

标签 http jakarta-ee tomcat http-status-code-405

我正在编写 jsp-servlet 应用程序并获得 405 http-status。我一直在寻找很长时间,但我不明白我做错了什么。

我的应用服务器是 Apache Tomcat-7.0.25。

我的 JSP 转发页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>
   <jsp:forward page="/myservlet" />
</body>
</html>

我的小服务程序

public class MyServlet extends HttpServlet {

  private static final String url = "http://ibm.com";

  public void doGet(ServletRequest request, ServletResponse response)
       throws ServletException, IOException {

  URLConnection conn = null;
  URL connectURL = null;

  try {
    PrintWriter out = response.getWriter();
    connectURL = new URL(url);
    conn = connectURL.openConnection();
    DataInputStream theHTML = new DataInputStream(conn.getInputStream());
    String thisLine;
    while ((thisLine = theHTML.readLine()) != null) {
       out.println(thisLine);
    }
    out.flush();
    out.close();
   } catch (Exception e) {
    System.out.println("Exception in MyServlet: " + e.getMessage());
    e.printStackTrace();
   }
  }
}

我的部署描述符 (web.xml) 文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
       version="3.0">

   <display-name>My web application</display-name>
   <description>My servet application</description>

   <servlet>
       <servlet-name>MyServlet</servlet-name>
       <servlet-class>MyServlet</servlet-class>
   </servlet>

   <servlet-mapping>
       <servlet-name>MyServlet</servlet-name>
       <url-pattern>/myservlet</url-pattern>
   </servlet-mapping>

   <welcome-file-list>
       <welcome-file>index.html</welcome-file>
       <welcome-file>index.jsp</welcome-file>
   </welcome-file-list>

</web-app>

这可能是什么原因造成的,我该如何解决这个问题?你有什么想法吗?

最佳答案

toGet() 方法中的参数类型有误。它们应该是 HttpServletRequestHttpServletResponse :

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)

这样的错误非常令人不快,因为它们不容易被发现。 所以不要忽视注释 @Override。 使用它来避免方法名称或其签名中的不同打印错误和其他不幸的误解。它将帮助您在编译时发现这些错误。

关于HTTP 状态 405 - 此 URL 不支持 HTTP 方法 GET,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14086727/

相关文章:

tomcat - HttpSession.setMaxInactiveInterval 在 Tomcat 6 中不起作用

java - 状态更新不起作用

java - 重新编译在没有源 java 文件夹的情况下部署在 apache tomcat 上的 Java 中创建的 Web 应用程序

Java Web应用程序 "Event Listener for Expired Sessions"

java - 获取登录用户的详细信息

debugging - 如何调试 RESTful 服务?

http - HTTP header If-None-Match : * mean?是什么意思

http - HTTP 请求字段中的特殊字符

Java Maven WebApp Tomcat

python - 在长时间运行的过程中,Flask 会不会对新请求不敏感?