用户自动登录使用过滤器来实现。
1.当用户勾选自动登录时,在登录的servlet中将用户名密码记录到cookie。
2.新建filter在其中判断浏览器是否带着用户名和密码的cookie
3.验证cookie,将正确获得的用户实体存入到session
bean,service和dao就不传了都一样的代码
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta charset="UTF-8">
<title>login</title>
</head>
<body>
<h3>请登录:</h3>
<h4><%=request.getAttribute("msg")!=null?request.getAttribute("msg"):""%></h4>
<form action="${pageContext.request.contextPath }/loginServlet" method="post">
<input type="text" name="username" /><br/>
<input type="password" name="password" /><br/>
是否自动登录:<input type="checkbox" name="autoLogin"><br/>
<input type="submit" value="登录" /><br/>
</from>
</body>
</html>