此URL不支持HTTP方法GET。

6
我遇到了这个错误,请你帮忙解决一下:

“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个回答

12

你把参数顺序搞反了 - 应该是先请求,然后是响应,像这样:

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

目前你并没有真正覆盖超类方法。

这就是为什么 @Override 注解如此重要 - 它让你能在编译时发现这样的错误。如果你使用 @Override 装饰了你的方法,编译器会发现你正在尝试覆盖一个不存在的方法签名。


是的...例如在Java Google AppEngine上调用的是doGet(Request,Response)。 - Paul

0

对于POST请求也会失败吗?

<servlet-class>test.FirstClass 应该改为 <servlet-class>FirstClass,不是吗?


测试是定义类的包。 - user1448652
嗯,那部分应该没问题。我也怀疑Jon提到的调用顺序。如果调用顺序不正确,POST就无法工作。 - Paul

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接