此URL不支持HTTP方法GET

时间:2011-05-04 06:40:53

标签: servlets

我收到了错误,请你帮忙

的servlet

public class FirstClass extends HttpServlet {

    private static final long serialVersionUID = 1L;

    public void doGet(HttpServletResponse response, HttpServletRequest request) throws IOException, ServletException {
        PrintWriter out = response.getWriter();
        out.println("this is a sample");
        out.flush();
    }

    public void doPost(HttpServletResponse response, HttpServletRequest request) throws IOException, ServletException {
        PrintWriter out = response.getWriter();
        out.println("this is a sample");
        out.flush();
    }
}

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>hii</display-name>

    <servlet>
        <servlet-name>First</servlet-name>
        <servlet-class>test.FirstClass</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>First</servlet-name>
        <url-pattern>/first.do</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
</web-app>

的index.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <a href="first.do">Click Me</a>
</body>
</html>

2 个答案:

答案 0 :(得分:12)

你的参数轮询错误 - 首先是请求,然后是响应,如下所示:

public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException {

所以目前你实际上并没有覆盖超类方法。

这就是@Override注释如此重要的原因 - 它可以让你在编译时找到这样的bug。如果你用@Override修饰方法,编译器会发现你试图覆盖一个不存在的方法签名。

答案 1 :(得分:0)

POST也失败了吗?

不应该<servlet-class>test.FirstClass而是<servlet-class>FirstClass