【问题标题】:HTTP Status 405 - HTTP methоd GET is not supported by this URLHTTP 状态 405 - 此 URL 不支持 HTTP 方法 GET
【发布时间】:2012-12-14 17:38:36
【问题描述】:

我正在编写 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>

这可能是什么原因,我该如何解决这个问题?你有什么假设吗?

【问题讨论】:

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


    【解决方案1】:

    您在方法toGet() 中有错误的参数类型。他们应该是HttpServletRequestHttpServletResponse

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

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

    【讨论】:

      猜你喜欢
      • 2015-03-10
      • 2016-02-13
      • 2016-10-30
      • 1970-01-01
      • 2011-07-19
      • 2014-01-16
      • 2015-02-14
      • 2014-10-12
      相关资源
      最近更新 更多