Filter&&Listener 比眉伴天荒 2024-03-27 12:40 77阅读 0赞 **目录** 一、Filter快速入门 二、Filter的执行流程 三、Filter使用细节 3.1 Filter拦截路径配置 3.2 过滤器链 四、案例——登录验证 五、Listener(监听器) -------------------- ![a25f1734e84849a2921bf5cb0224bbd8.png][] ## 一、Filter快速入门 ## ## ![1007b8469c104c179ac2d372fc64f11e.png][] ## ## 二、Filter的执行流程 ## ![6925af864f8c451da703d55d55e82ffc.png][] ## 三、Filter使用细节 ## ### 3.1 Filter拦截路径配置 ### ![e80c3cf5942640c3803484be61090d7e.png][] ### 3.2 过滤器链 ### ### ![f777cf237aea4d6a8637ceb9075c4248.png][] ### ## ![eb10dee8a88540959525e8b075a6ee0f.png][] 四、案例——登录验证 ## ![52dc5fa85e394724b6f68d72c75cc41b.png][] package com.itheima.web.fiter; import javax.servlet.*; import javax.servlet.annotation.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import java.io.IOException; /** * 登录验证的过滤器 */ @WebFilter( "/*") public class LoginFilter implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException { //判断访问资源路径是否和登录注册相关 String[] urls = {"/login.jsp","/imgs/","/css/","/loginServlet","/register.jsp","/register","/checkCodeServlet"}; HttpServletRequest req = (HttpServletRequest) request; //获取当前访问的资源路径 String url = req.getRequestURL().toString(); //循环判断iter for (String u : urls) { if (url.contains(u)){ //找到了 //放行 chain.doFilter(request, response); return; } } //1.判断session中是否有user对象 HttpSession session = req.getSession(); Object user = session.getAttribute("user"); //2.判断user是否为Null if (user != null){ //登陆过 //放行 chain.doFilter(request, response); }else{ //没有登录,存储提示信息,拦截,并跳转到登陆界面 req.setAttribute("login_msg", "您尚未登录!"); req.getRequestDispatcher("/login.jsp").forward(request, response); } } public void init(FilterConfig config) throws ServletException { } public void destroy() { } } ## 五、Listener(监听器) ## ![569fddfb84584df69446610c568bec35.png][] ![c6d491ebc0024cbc848a1a5d5c469474.png][] [a25f1734e84849a2921bf5cb0224bbd8.png]: https://image.dandelioncloud.cn/pgy_files/images/2024/03/27/fc273d78e8024b1fa6c7ef89c6d2af96.png [1007b8469c104c179ac2d372fc64f11e.png]: https://image.dandelioncloud.cn/pgy_files/images/2024/03/27/1436069201664c408029c343c8deb500.png [6925af864f8c451da703d55d55e82ffc.png]: https://image.dandelioncloud.cn/pgy_files/images/2024/03/27/9ef98ffd81ea4cc9a7341b8af0f84996.png [e80c3cf5942640c3803484be61090d7e.png]: https://image.dandelioncloud.cn/pgy_files/images/2024/03/27/653d75b9c8844a6288721d6d85a9feb2.png [f777cf237aea4d6a8637ceb9075c4248.png]: https://image.dandelioncloud.cn/pgy_files/images/2024/03/27/0101d291880345cd942cb547079a161c.png [eb10dee8a88540959525e8b075a6ee0f.png]: https://image.dandelioncloud.cn/pgy_files/images/2024/03/27/5babc0afb3e74a738483b0f15ba28e4b.png [52dc5fa85e394724b6f68d72c75cc41b.png]: https://image.dandelioncloud.cn/pgy_files/images/2024/03/27/8b035bc999c94d51ab5d945e1647771f.png [569fddfb84584df69446610c568bec35.png]: https://image.dandelioncloud.cn/pgy_files/images/2024/03/27/fb7a1cf17e284d5d9bb31b59470b5346.png [c6d491ebc0024cbc848a1a5d5c469474.png]: https://image.dandelioncloud.cn/pgy_files/images/2024/03/27/f394b25464e84d1b89792ce6eb9db646.png
还没有评论,来说两句吧...