0

私は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>

これの原因は何ですか?また、この問題を解決するにはどうすればよいですか? 推測はありますか?

4

1 に答える 1

1

メソッドのパラメーターの型が間違っていますtoGet()。それらはHttpServletRequestおよびHttpServletResponseである必要があります。

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

このようなバグは、検出が容易ではないため、非常に不快です。したがって、注釈を無視しないでください@Override。メソッド名またはそのシグネチャーのさまざまな誤字やその他の不幸な誤解を避けるために使用してください。コンパイル時にこれらの間違いを見つけるのに役立ちます。

于 2012-12-30T01:41:55.927 に答える