写Filter一定要知道该Filter过滤哪个或哪些资源,不是所有的Filter都过滤/*的资源。
logn.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body> <form action="/day19/LoginServlet" method="post"> <table border="1" align="center"> <caption><br>用户自动登录</caption> <tr> <th>用户名</th> <td><input type="text" name="username"/></td> </tr> <tr> <th>密码</th> <td><input type="password" name="password"/></td> </tr> <tr> <th>时间</th> <td> <input checked name="time" type="radio" value="60"/>1分钟 <input name="time" type="radio" value="180"/>3分钟 <input name="time" type="radio" value="300"/>5分钟 </td> </tr> <tr> <td colspan="2" align="center"> <input type="submit" value="提交"/> </td> </tr> </table> </form> </body></html>
login.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body> <form action="/day19/LoginServlet" method="post"> <table border="1" align="center"> <caption><br>用户自动登录</caption> <tr> <th>用户名</th> <td><input type="text" name="username"/></td> </tr> <tr> <th>密码</th> <td><input type="password" name="password"/></td> </tr> <tr> <th>时间</th> <td> <input checked name="time" type="radio" value="60"/>1分钟 <input name="time" type="radio" value="180"/>3分钟 <input name="time" type="radio" value="300"/>5分 </td> </tr> <tr> <td colspan="2" align="center"> <input type="submit" value="提交"/> </td> </tr> </table> </form> </body></html>
welcome.jsp
<%@ page language="java" pageEncoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <body> <%-- 用户名->Filter->welcome.jsp --%> 欢迎${!empty sessionScope.username?username:'游客'}光临 </body></html>
autoLoginFilter(过滤器)
import java.io.IOException;import javax.servlet.Filter;import javax.servlet.FilterChain;import javax.servlet.FilterConfig;import javax.servlet.ServletException;import javax.servlet.ServletRequest;import javax.servlet.ServletResponse;import javax.servlet.http.Cookie;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;//对敏感页面或目录进行认证public class AutoLoginFilter implements Filter { public void init(FilterConfig filterConfig) throws ServletException { } public void doFilter(ServletRequest req, ServletResponse res,FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) res; //取得浏览器的Cookie Cookie[] cookies = request.getCookies(); //记录cookie值 Cookie userCookie = null; if(cookies!=null){ for(Cookie c : cookies){ if(c.getName().equals("usernameAndPassword")){ userCookie = c; break; } } //找到对应的Cookie if(userCookie!=null){ String usernameAndPassword = userCookie.getValue(); String[] both = usernameAndPassword.split("_"); String username = both[0]; String password = both[1]; if(username.equals("jack") && password.equals("123")){ request.getSession().setAttribute("username",username); } } } //发行资源 chain.doFilter(request,response); } public void destroy() { }}
LoginServlet
import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.Cookie;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;//用户自动登录public class LoginServlet extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); String username = request.getParameter("username"); String password = request.getParameter("password"); if(username!=null && password!=null && username.trim().length()>0 && password.trim().length()>0){ if(username.equals("jack") && password.equals("123")){ //登录成功,将信息绑定到HttpSession域对象中 request.getSession().setAttribute("username",username); request.getSession().setAttribute("password",password); //向浏览器写入Cookie Cookie cookie = new Cookie("usernameAndPassword",username+"_"+password); int time = Integer.parseInt(request.getParameter("time")); cookie.setMaxAge(time); response.addCookie(cookie); //重定向到welcome.jsp页面 response.sendRedirect(request.getContextPath()+"/welcome.jsp"); } } }}
web.xml
<filter> <filter-name>AutoLoginFilter</filter-name> <filter-class>cn.itcast.web.filter.AutoLoginFilter</filter-class> </filter> <filter-mapping> <filter-name>AutoLoginFilter</filter-name> <url-pattern>/welcome.jsp</url-pattern> </filter-mapping>