Вопрос

I am writing jsp-servlet application and getting 405 http-status. I was looking for a long time but I cannot understand what I do wrong.

My application server is Apache Tomcat-7.0.25.

My JSP forward page

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

My servlet

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();
   }
  }
}

My deployment descriptor (web.xml) file

<?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>

What can be cause of this and how can I solve this problem? Do you have any supposes?

Это было полезно?

Решение

You have wrong types of parameters in the method toGet(). They should be HttpServletRequest and HttpServletResponse:

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

Such bugs are very unpleasant, because they aren't easy to detect. So don't disregard annotation @Override. Use it to avoid different misprint and other unfortunate misunderstandings in method name or its signature. It'll help you to find these mistakes at compile time.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top