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

import java.io.*; 
 import javax.servlet.*; 
 import javax.servlet.http.*; 
 import java.util.*; 
 public class ResponseHeaderFilter implements Filter { 
   FilterConfig fc; 
   public void doFilter(ServletRequest req, ServletResponse res, 
                       FilterChain chain) throws IOException,ServletException { 
      HttpServletResponse response = (HttpServletResponse) res; 
      for (Enumeration e=fc.getInitParameterNames(); e.hasMoreElements();) { 
         String headerName = (String)e.nextElement(); 
         response.addHeader(headerName, fc.getInitParameter(headerName)); 
      } 
     chain.doFilter(req, response); 
   } 

   public void init(FilterConfig filterConfig) { 
     this.fc = filterConfig; 
   } 

   public void destroy() { 
     this.fc = null; 
   } 
 }

缓存

<filter> 
   <filter-name>ResponseHeaderFilter</filter-name> 
   <filter-class>com.jspbook.ResponseHeaderFilter</filter-class> 
   <init-param> 
     <param-name>Cache-Control</param-name> 
     <param-value>max-age=3600</param-value> 
   </init-param> 
 </filter> 
 <filter-mapping> 
   <filter-name>ResponseHeaderFilter</filter-name> 
   <url-pattern>/logo.png</url-pattern> 
 </filter-mapping>

不缓存

<filter> 
   <filter-name>ResponseHeaderFilter</filter-name> 
   <filter-class>com.jspbook.ResponseHeaderFilter</filter-class> 
   <init-param> 
     <param-name>Cache-Control</param-name> 
     <param-value>private,no-cache,no-store</param-value> 
    </init-param> 
 </filter>

相关推荐