此URL不支持HTTP方法GET

use*_*652 6 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();
    }
}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

的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>
Run Code Online (Sandbox Code Playgroud)

Jon*_*eet 12

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

public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException {
Run Code Online (Sandbox Code Playgroud)

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

这就是@Override注释如此重要的原因 - 它允许您在编译时找到这样的错误.如果你装饰了你的方法用@Override,编译器会发现你试图重写并不存在的方法签名.