Filter實現使用者自動登入login.jsp和index.jsp
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 http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body style="text-align:center;"> <form action="${pageContext.request.contextPath}/LoginServlet " method="post"> <table border="1" width="600px" cellpadding="0" cellspacing="0" align="center"> <tr> <td height="30" align="center" >使用者名稱:</td> <td><input type="text" name="username" />${errerMsg} </td> </tr> <tr> <td height="30" align="center" >密 碼:</td> <td> <input type="password" name="password" /> </td> </tr> <tr> <td height="30" align="center" >自動登陸時間</td> <td><input type="radio" name="autologin" value="${60*60*24*31}"/>一個月 <input type="radio" name="autologin" value="${60*60*24*31*3}"/>三個月 <input type="radio" name="autologin" value="${60*60*24*31*6}"/> 六個月 <input type="radio" name="autologin" value="${60*60*24*31*12}"/> 一年 </td> </tr> <tr> <td height="30" colspan="2" align="center"> <input type="submit" value="登入"/> <input type="reset" value="重置"/> </td> </tr> </table> </form> </body> </html>
index.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" import="java.util.* "%> < %@taglib prefix="c" uri="http://java.sun.com/jsp/jst1/core" %> <html> <head> <title>顯示使用者的登入資訊</title>
</head> <body> <br> <center> <h3>歡迎光臨</h3> </center> <br> <br> <c:choose> <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}/LogoutServlet">登出</a> </c:otherwise> </c:choose> <hr> </body> </html>