假设用户登录页面login.jsp在web工程web2022的hw目录下,即访问login.jsp的URL地址是http://localhost:8080/web2023/hw/login.jsp,将login.jsp提交的用户名和密码由FirstServlet(映射地址为/servlet/firstServlet.do)获取进行判断,如果登录成功,则跳转至SecondServlet(映射地址为/servlet/secondServlet.do)显示用户名信息,如果登录不成功,则跳转至登录失败页面hw/failed.jsp。 要求:用户名和密码存在WEB-INF/userinfo.txt文件中,若页面出现404错误则统一跳转至404.html页面,500错误则统一跳转至500.html页面,并提示出错信息。
时间: 2023-05-30 21:06:14 浏览: 166
JSP——登录页面login.jsp
首先需要在web2023工程下创建以下目录结构:
```
web2023
├── WEB-INF
│ ├── classes
│ ├── lib
│ └── userinfo.txt
├── hw
│ ├── login.jsp
│ └── failed.jsp
├── servlet
│ ├── FirstServlet.java
│ └── SecondServlet.java
├── 404.html
└── 500.html
```
其中,userinfo.txt文件用于存储用户名和密码,格式为:
```
username1=password1
username2=password2
```
FirstServlet的实现如下:
```java
package servlet;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class FirstServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 获取用户名和密码
String username = request.getParameter("username");
String password = request.getParameter("password");
// 读取userinfo.txt文件
String path = getServletContext().getRealPath("/WEB-INF/userinfo.txt");
File file = new File(path);
BufferedReader reader = new BufferedReader(new FileReader(file));
// 验证用户名和密码是否正确
String line;
boolean found = false;
while ((line = reader.readLine()) != null) {
String[] parts = line.split("=");
if (parts.length == 2 && parts[0].equals(username) && parts[1].equals(password)) {
found = true;
break;
}
}
reader.close();
// 根据验证结果跳转页面
if (found) {
HttpSession session = request.getSession();
session.setAttribute("username", username);
response.sendRedirect(request.getContextPath() + "/servlet/secondServlet.do");
} else {
response.sendRedirect(request.getContextPath() + "/hw/failed.jsp");
}
}
}
```
SecondServlet的实现如下:
```java
package servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class SecondServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 获取用户名
HttpSession session = request.getSession();
String username = (String) session.getAttribute("username");
// 显示用户名信息
response.setContentType("text/html;charset=UTF-8");
response.getWriter().println("<h1>欢迎您," + username + "!</h1>");
}
}
```
login.jsp的实现如下:
```html
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/servlet/firstServlet.do" method="post">
<label>用户名:</label>
<input type="text" name="username" /><br />
<label>密码:</label>
<input type="password" name="password" /><br />
<input type="submit" value="登录" />
</form>
</body>
</html>
```
failed.jsp的实现如下:
```html
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录失败</title>
</head>
<body>
<h1>登录失败,请检查用户名和密码是否正确!</h1>
</body>
</html>
```
web.xml的实现如下:
```xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
id="WebApp_ID" version="4.0">
<display-name>web2023</display-name>
<error-page>
<error-code>404</error-code>
<location>/404.html</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/500.html</location>
</error-page>
<servlet>
<servlet-name>firstServlet</servlet-name>
<servlet-class>servlet.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>servlet.SecondServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>secondServlet</servlet-name>
<url-pattern>/servlet/secondServlet.do</url-pattern>
</servlet-mapping>
</web-app>
```
在以上实现中,使用了ServletContext的getRealPath方法获取userinfo.txt文件的绝对路径;使用HttpSession对象在FirstServlet和SecondServlet之间传递数据;使用EL表达式获取web应用的上下文路径;使用了web.xml的error-page配置实现404和500错误页面的统一处理。
阅读全文