gpt4 book ai didi

java - Servlet 响应发送重定向错误

转载 作者:太空宇宙 更新时间:2023-11-04 12:46:23 25 4
gpt4 key购买 nike

我刚刚收到错误 404:请求的资源不可用。我不明白这里出了什么问题:

这是 xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<servlet>
<servlet-name>servlet</servlet-name>
<servlet-class>controller.Servlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servlet</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
</web-app>

servlet:

[@WebServlet(name = "controller.Servlet")
public class Servlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String id = request.getParameter("id");
String password = request.getParameter("password");

if (LoginService.authentication(id, password)) {
response.sendRedirect("succes.jsp");
} else {
response.sendRedirect("index.jsp");
}
return;
}][1]

index.jsp 文件:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Login Page</title>
</head>
<body>
<form action="/login" method="post">
Username: <input type="text" name="id" >
<br>
Password: <input type="password" name="password">
<br>
<input type="submit" value="submit">
</form>
</body>
</html>

成功.jsp:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Login Succesfully</title>
</head>
<body>
<h1>Login succesfully</h1>
</body>
</html>

这是文件夹结构: /image/ZGMH4.png

最佳答案

更新 web.xml:

 <servlet>
<servlet-name>controller.Servlet</servlet-name>
<servlet-class>com..Servlet(replace with your servlet class namespace)</servlet-class>
</servlet>

或者,您可以像这样在 @WebServlet 中定义 urlPatterns,并从 web.xml 中删除 servlet 定义和 url 模式:

@WebServlet(name="controller.Servlet", urlPatterns={"/login"})
public class Servlet extends HttpServlet{
.......
}

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">

</web-app>

关于java - Servlet 响应发送重定向错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36289287/

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