码迷,mamicode.com
首页 > 其他好文 > 详细

登陆的过滤器

时间:2015-06-24 10:55:48      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:

 1 public class LoginFilter implements Filter {
 2     public void destroy() {
 3 
 4     }
 5 
 6     public void doFilter(ServletRequest request, ServletResponse response,
 7             FilterChain chain) throws IOException, ServletException {
 8         /*
 9          * 1. 获取session中的user
10          * 2. 判断是否为null
11          *   > 如果为null:保存错误信息,转发到msg.jsp
12          *   > 如果不为null:放行
13          */
14         HttpServletRequest req = (HttpServletRequest) request;
15         Object user = req.getSession().getAttribute("sessionUser");
16         if(user == null) {
17             req.setAttribute("code", "error");//为了显示X图片
18             req.setAttribute("msg", "您还没有登录,请先登录");
19             req.getRequestDispatcher("你要跳转的登陆页面").forward(req, response);
20         } else {
21             chain.doFilter(request, response);//放行
22         }
23     }
24 
25     public void init(FilterConfig fConfig) throws ServletException {
26 
27     }
28 }
web.xml配置如下
<
filter> <display-name>LoginFilter</display-name> <filter-name>LoginFilter</filter-name> <filter-class>过滤器类路径</filter-class> </filter> <filter-mapping> <filter-name>LoginFilter</filter-name> <url-pattern>你要过滤的页面</url-pattern> </filter-mapping> <filter-mapping> <filter-name>LoginFilter</filter-name> <servlet-name>你要过滤的servlet</servlet-name> </filter-mapping> </filter-mapping>

 

 

登陆的过滤器

标签:

原文地址:http://www.cnblogs.com/wenjie123/p/4596992.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!