Servlet的过滤器Filter(一)

  1. 一、使浏览器不缓存页面的过滤器   
  2. import javax.servlet.*;   
  3. import javax.servlet.http.HttpServletResponse;   
  4. import java.io.IOException;   
  5.   
  6. /**
  7. * 用于使 Browser 不缓存页面的过滤器
  8. */  
  9. public class ForceNoCacheFilter implements Filter {   
  10.   
  11. public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException   
  12. {   
  13.     ((HttpServletResponse) response).setHeader("Cache-Control","no-cache");   
  14.     ((HttpServletResponse) response).setHeader("Pragma","no-cache");   
  15.     ((HttpServletResponse) response).setDateHeader ("Expires", -1);   
  16.     filterChain.doFilter(request, response);   
  17. }   
  18.   
  19. public void destroy()   
  20. {   
  21. }   
  22.   
  23. public void init(FilterConfig filterConfig) throws ServletException   
  24. {   
  25. }   
  26. }   
  27.   
  28. 二、检测用户是否登陆的过滤器   
  29.   
  30. import javax.servlet.*;   
  31. import javax.servlet.http.HttpServletRequest;   
  32. import javax.servlet.http.HttpServletResponse;   
  33. import javax.servlet.http.HttpSession;   
  34. import java.util.List;   
  35. import java.util.ArrayList;   
  36. import java.util.StringTokenizer;   
  37. import java.io.IOException;   
  38.   
  39. /**
  40. * 用于检测用户是否登陆的过滤器,如果未登录,则重定向到指定的登录页面
  41. * 配置参数
  42. * checkSessionKey 需检查的在 Session 中保存的关键字
  43. * redirectURL 如果用户未登录,则重定向到指定的页面,URL不包括 ContextPath
  44. * notCheckURLList 不做检查的URL列表,以分号分开,并且 URL 中不包括 ContextPath
  45. */  
  46. public class CheckLoginFilter   
  47. implements Filter   
  48. {   
  49.      protected FilterConfig filterConfig = null;   
  50.      private String redirectURL = null;   
  51.      private List notCheckURLList = new ArrayList();   
  52.      private String sessionKey = null;   
  53.   
  54. public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException   
  55. {   
  56.     HttpServletRequest request = (HttpServletRequest) servletRequest;   
  57.     HttpServletResponse response = (HttpServletResponse) servletResponse;   
  58.   
  59.      HttpSession session = request.getSession();   
  60.    if(sessionKey == null)   
  61.     {   
  62.      filterChain.doFilter(request, response);   
  63.     return;   
  64.     }   
  65.    if((!checkRequestURIIntNotFilterList(request)) && session.getAttribute(sessionKey) == null)   
  66.     {   
  67.      response.sendRedirect(request.getContextPath() + redirectURL);   
  68.     return;   
  69.     }   
  70.     filterChain.doFilter(servletRequest, servletResponse);   
  71. }   
  72.   
  73. public void destroy()   
  74. {   
  75.     notCheckURLList.clear();   
  76. }   
  77.   
  78. private boolean checkRequestURIIntNotFilterList(HttpServletRequest request)   
  79. {   
  80.     String uri = request.getServletPath() + (request.getPathInfo() == null ? "" : request.getPathInfo());   
  81.    return notCheckURLList.contains(uri);   
  82. }   
  83.   
  84. public void init(FilterConfig filterConfig) throws ServletException   
  85. {   
  86.    this.filterConfig = filterConfig;   
  87.     redirectURL = filterConfig.getInitParameter("redirectURL");   
  88.     sessionKey = filterConfig.getInitParameter("checkSessionKey");   
  89.   
  90.     String notCheckURLListStr = filterConfig.getInitParameter("notCheckURLList");   
  91.   
  92.    if(notCheckURLListStr != null)   
  93.     {   
  94.      StringTokenizer st = new StringTokenizer(notCheckURLListStr, ";");   
  95.      notCheckURLList.clear();   
  96.     while(st.hasMoreTokens())   
  97.      {   
  98.       notCheckURLList.add(st.nextToken());   
  99.      }   
  100.     }   
  101. }   
  102. }   

你可能感兴趣的:(java,浏览器,cache,servlet)