根据你提供的需求,下面是一个实现示例,包括 login.jsp
、FirstServlet
和 SecondServlet
的代码,以及如何处理错误页面。
1. 文件结构
web2024/
│
├── hw/
│ ├── login.jsp
│ ├── failed.jsp
│
├── WEB-INF/
│ ├── userinfo.txt
│ └── web.xml
├── 404.html
└── 500.html
2. userinfo.txt
在 WEB-INF/userinfo.txt
中存储用户信息,例如:
username1:password1
username2:password2
3. login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
</head>
<body>
<h2>Login Page</h2>
<form action="<%= request.getContextPath() %>/servlet/firstServlet.do" method="post">
Username: <input type="text" name="username"/><br/>
Password: <input type="password" name="password"/><br/>
<input type="submit" value="Login"/>
</form>
</body>
</html>
4. FirstServlet.java
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
@WebServlet("/servlet/firstServlet.do")
public class FirstServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
// 验证用户名和密码是否正确。
boolean isValidUser = false;
try (BufferedReader reader = new BufferedReader(new InputStreamReader(getServletContext().getResourceAsStream("/WEB-INF/userinfo.txt")))) {
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(":");
if (parts.length == 2 && parts[0].equals(username) && parts[1].equals(password)) {
isValidUser = true;
break;
}
}
}
if (isValidUser) {
response.sendRedirect(request.getContextPath() + "/servlet/secondServlet.do?username=" + username);
} else {
response.sendRedirect(request.getContextPath() + "/hw/failed.jsp");
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 通常不支持 GET 请求,但可以重定向到登录页。
response.sendRedirect(request.getContextPath() + "/hw/login.jsp");
}
}
5. SecondServlet.java
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/servlet/secondServlet.do")
public class SecondServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
response.setContentType("text/html;charset=UTF-8");
response.getWriter().println("<h1>Welcome, " + username + "!</h1>");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 不支持POST请求,这里可以重定向到其他地方或返回错误。
doGet(request, response);
}
}
6. web.xml
<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">
<!-- 定义Error Pages -->
<error-page error-code="404">
<location>/404.html</location>
</error-page>
<error-page error-code="500">
<location>/500.html</location>
</error-page>
</web-app>
7. 错误页面 (404.html
和 500.html
)
404.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>404 Not Found</title>
</head>
<body>
<h1>Error 404 - Page Not Found</h1>
<p>The page you are looking for does not exist.</p>
<a href="/web2024/hw/login.jsp">Go to Login Page</a>
</body>
</html>
500.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>500 Internal Server Error</title>
</head>
<body>
<h1>Error 500 - Internal Server Error</h1>
<p>An unexpected error occurred on the server.</p>
// 可考虑输出异常信息,但需谨慎以免泄露敏感数据。
<a href="/web2024/hw/login.jsp">Go to Login Page</a>
<!-- 提示出错信息 -->
<%
String errorMessage = (String)request.getAttribute("javax.servlet.error.message");
if(errorMessage != null){
out.println("<p>Error details: " + errorMessage + "</p>");
}
%>
<!-- 提示用户联系管理员等 -->
<p>If the problem persists, please contact support.</p>
</body></html>
以上代码实现了你所要求的功能,包括用户登录验证、成功与失败的跳转、错误处理等。确保在Tomcat服务器中正确部署该Web应用程序,并根据需要调整路径和内容。
内容由零声教学AI助手提供,问题来源于学员提问