Filter指定浏览器来缓存或不缓存服务器数据

转载:

http://elan1986.iteye.com/blog/1409504

Java代码    收藏代码
  1. import java.io.*;   
  2. import javax.servlet.*;   
  3. import javax.servlet.http.*;   
  4. import java.util.*;   
  5. public class ResponseHeaderFilter implements Filter {   
  6.   FilterConfig fc;   
  7.   public void doFilter(ServletRequest req, ServletResponse res,   
  8.                       FilterChain chain) throws IOException,ServletException {   
  9.      HttpServletResponse response = (HttpServletResponse) res;   
  10.      for (Enumeration e=fc.getInitParameterNames(); e.hasMoreElements();) {   
  11.         String headerName = (String)e.nextElement();   
  12.         response.addHeader(headerName, fc.getInitParameter(headerName));   
  13.      }   
  14.     chain.doFilter(req, response);   
  15.   }   
  16.   
  17.   public void init(FilterConfig filterConfig) {   
  18.     this.fc = filterConfig;   
  19.   }   
  20.   
  21.   public void destroy() {   
  22.     this.fc = null;   
  23.   }   
  24. }   


缓存 
Java代码    收藏代码
  1. <filter>   
  2.    <filter-name>ResponseHeaderFilter</filter-name>   
  3.    <filter-class>com.jspbook.ResponseHeaderFilter</filter-class>   
  4.    <init-param>   
  5.      <param-name>Cache-Control</param-name>   
  6.      <param-value>max-age=3600</param-value>   
  7.    </init-param>   
  8.  </filter>   
  9.  <filter-mapping>   
  10.    <filter-name>ResponseHeaderFilter</filter-name>   
  11.    <url-pattern>/logo.png</url-pattern>   
  12.  </filter-mapping>  

不缓存 
Java代码    收藏代码
  1. <filter>   
  2.   <filter-name>ResponseHeaderFilter</filter-name>   
  3.   <filter-class>com.jspbook.ResponseHeaderFilter</filter-class>   
  4.   <init-param>   
  5.     <param-name>Cache-Control</param-name>   
  6.     <param-value>private,no-cache,no-store</param-value>   
  7.    </init-param>   
  8. </filter>  

 

你可能感兴趣的:(filter)