Chinaunix首页 | 论坛 | 博客
  • 博客访问: 70376
  • 博文数量: 21
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 14
  • 用 户 组: 普通用户
  • 注册时间: 2013-03-27 11:09
文章分类

全部博文(21)

文章存档

2015年(1)

2013年(20)

我的朋友

分类: 系统运维

2013-12-09 13:08:13

   使用java开发,一般我们可以通过filter来控制某些内容添加不同的响应头。
   以下的示例示例了如何为不同类型的文件添加不同的Cache-Control。
 

    public class ResponseHeaderFilter implements Filter {  
        FilterConfig fc;   
      
        public void doFilter(ServletRequest req, ServletResponse res,  
                FilterChain chain) throws IOException, ServletException {  
            HttpServletResponse response = (HttpServletResponse) res;  
            // set the provided HTTP response parameters  
            for (Enumeration e = fc.getInitParameterNames(); e.hasMoreElements();) {  
                String headerName = (String) e.nextElement();  
                response.addHeader(headerName, fc.getInitParameter(headerName));  
            }  
            // pass the request/response on  
            chain.doFilter(req, response);  
        }   
      
        public void init(FilterConfig filterConfig) {  
            this.fc = filterConfig;  
        }   
      
        public void destroy() {  
            this.fc = null;  
        }   
      
    }  


web.xml中的配置

     
            NoCache  
            apis.server.common.util.ResponseHeaderFilter  
             
                Cache-Control  
                no-cache, must-revalidate  
           
 
       
 
         
            CacheForWeek  
            apis.server.common.util.ResponseHeaderFilter  
             
                Cache-Control  
                max-age=604800, public  
           
 
       
 
      
     
            NoCache  
            *.do  
       
 
         
            CacheForWeek  
            /images/*  
       
 
         
            CacheForWeek  
            /img/*  
       
 
         
            CacheForWeek  
            /icons/*  
       
 
         
            CacheForWeek  
            /ext/*  
       
 
         
            CacheForWeek  
            *.js  
       
 
         
            CacheForWeek  
            *.css  
       
   






阅读(1572) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~