要实现你描述的功能,我们可以按照以下步骤来设计你的Web应用程序。
1. 项目结构
假设你的项目结构如下:
web2024/
│
├── WEB-INF/
│ ├── userinfo.txt // 存放用户名和密码
│ ├── web.xml // 配置文件
│
├── hw/
│ ├── login.jsp // 登录页面
│ ├── failed.jsp // 登录失败页面
│ └── 404.html // 404错误页面
│ └── 500.html // 500错误页面
└── servlet/
├── FirstServlet.java // 第一个Servlet处理登录逻辑
└── SecondServlet.java // 第二个Servlet显示用户名信息
2. userinfo.txt
文件格式
假设userinfo.txt
文件内容如下,格式为“用户名:密码”的形式(每行一个):
user1:password1
user2:password2
3. web.xml
配置
在 WEB-INF/web.xml
中配置 Servlet 和错误处理:
<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>FirstServlet</servlet-name>
<servlet-class>com.example.FirstServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FirstServlet</servlet-name>
<url-pattern>/servlet/firstServlet.do</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>SecondServlet</servlet-name>
<servlet-class>com.example.SecondServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SecondServlet</servlet-name>
<url-pattern>/servlet/secondServlet.do</url-pattern>
</servlet-mapping>
<!-- Error handling -->
<error-page error-code="404">
<location>/hw/404.html</location>
</error-page>
<error-page error-code="500">
<location>/hw/500.html</location>
</error-page>
</web-app>
4. login.jsp
页面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Login Page</title>
</head>
<body>
<h2>Login Page</h2>
<form action="${pageContext.request.contextPath}/servlet/firstServlet.do" method="post">
Username: <input type="text" name="username"><br><br>
Password: <input type="password" name="password"><br><br>
<input type="submit" value="Login">
</form>
</body></html>
5. FirstServlet.java
package com.example;
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.FileReader;
import java.io.IOException;
@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 = validateUser(username, password);
if (isValidUser) {
response.sendRedirect(request.getContextPath() + "/servlet/secondServlet.do?username=" + username);
} else {
response.sendRedirect(request.getContextPath() + "/hw/failed.jsp");
}
}
private boolean validateUser(String username, String password) throws IOException {
try (BufferedReader reader = new BufferedReader(new FileReader(getClass().getClassLoader().getResource("userinfo.txt").getFile()))) {
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(":");
if (parts.length == 2 && parts[0].equals(username) && parts[1].equals(password)) {
return true; // Valid user found.
}
}
}
return false; // Invalid user.
}
}
6. SecondServlet.java
package com.example;
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.IOException;
@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>");
}
}
7. failed.jsp
页面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Login Failed</title></head><body>
<h2>Login Failed!</h2><p>Please check your username and password.</p><a href="/web2024/hw/login.jsp">Try Again?</a>
</body></html>`
错误处理页面 (404.html
, 500.html
)
确保存在这两个静态 HTML 文件,用于展示相应的错误消息。
以上就是实现用户登录并根据结果跳转到不同页面的完整示例。通过此示例,你可以学习如何使用 JSP 和 Servlet 来管理会话和用户身份验证。
内容由零声教学AI助手提供,问题来源于学员提问