设置JSP不做浏览器缓存

CacheFilter代码:

package com.my.filter;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;

public class CacheFilter implements Filter {
    public CacheFilter() {}
    
    public void init(FilterConfig config) throws ServletException {
    }
    
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
        HttpServletResponse response = (HttpServletResponse)resp;
        response.setDateHeader("Expires",-1);
        response.setHeader("Cache-Control","no-cache");
        response.setHeader("Pragma","no-cache");
        chain.doFilter(req, response);
    }
    
    public void destroy() {
    }
}

 

web.xml中加入<filter/>

    <filter>
        <filter-name>cahceFilter</filter-name>
        <filter-class>com.my.filter.CacheFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>cahceFilter</filter-name>
        <url-pattern>*.jsp</url-pattern>  
    </filter-mapping>

 

posted @ 2014-03-27 17:00  横渡  阅读(1093)  评论(0编辑  收藏  举报