本文章要实现的功能如下:
1、首页未登录状态:
2、首页已登录状态:
(这篇文章先不涉及下方图书信息的显示,该部分内容将在后续文章进行)
3、修改密码功能
4、注销用户功能
一、编写主界面
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!--导入JSTL标签库 -->
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>首页</title>
</head>
<body>
<br />
<center><h3>欢迎进入图书管理系统</h3></center>
<br /><br />
<c:choose> <%-- 判断用户信息是否存在在session中,如果不存在,则显示登录页面链接 --%>
<c:when test="${sessionScope.user==null}">
<a href="${pageContext.request.contextPath}/login.jsp">用户登录</a>
</c:when>
<c:otherwise> <%-- 如果存在,则直接显示用户信息 --%>
欢迎你,${
sessionScope.user.username }!
<a href="${pageContext.request.contextPath}/updatePwd.jsp">修改密码</a>
<a href="${pageContext.request.contextPath}/LogoutServlet">注销</a>
<hr /><br />
</c:otherwise>
</c:choose>
</body>
</html>
二、编写自动登录过滤器
新建Filter类文件,起名为AutoLoginFilter
为该文件添加Filter接口
在该文件的doFilter()方法里添加过滤操作
public void doFilter(ServletRequest req, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest request=(HttpServletRequest) req;
//获取cookie对象
Cookie[] cookies=request.getCookies();
String autoLogin=null;
//遍历cookie对象的所有信息,判断用户是否选择了自动登录选项
for(int i=0;cookies!=null&&i<cookies.length;i++) {
if("autologin".equals