标签:
过滤器,在执行request前或者response后进行过滤处理。
request前过滤举例:在要求登录的网站,在未登录的状态下,除了登录界面,访问其他然都会自动转到登录界面。登录可以访问控制例子:
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest)request; if(req.getServletPath().equals("/login.jsp") || req.getServletPath().equals("/doLogin.jsp")) { chain.doFilter(request, response); //这两个页面不进行过滤,跳到下一个过滤器执行 return; } //request.getSeesion可以获取当前的Session if(req.getSession().getAttribute("isLogin") != null && (boolean)req.getSession().getAttribute("isLogin")){ chain.doFilter(request, response);//如果满足条件,则继续执行,否则报错。 return; }else { request.setAttribute("errorMessage", "You did not login!"); request.getRequestDispatcher("NewFile.jsp").forward(request, response); return; } }
标签:
原文地址:http://blog.csdn.net/deargua/article/details/51346613