gpt4 book ai didi

servlets - 此URL不支持HTTP方法GET

转载 作者:行者123 更新时间:2023-12-04 13:43:45 24 4
gpt4 key购买 nike

我遇到了错误,请您帮忙

小服务程序

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>

最佳答案

您以错误的方式获取了参数-应该首先是请求,然后是响应,如下所示:

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

因此,当前您实际上并没有覆盖父类(super class)方法。

这就是 @Override 批注如此重要的原因-它使您可以在编译时查找此类bug。如果您使用 @Override装饰了方法,则编译器会发现您正在尝试覆盖不存在的方法签名。

关于servlets - 此URL不支持HTTP方法GET,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5879529/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com