Java代码
NoCacheFilter.class
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class NoCacheFilter implements Filter {
public void destroy() {
}
//浏览器不缓存页面
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException{
//设置禁止缓存的消息头
((HttpServletResponse)response).setHeader("Pragma","No-cache");
((HttpServletResponse)response).setHeader("Cache-Control","no-cache");
((HttpServletResponse)response).setHeader("Expires","0");//禁止缓存
chain.doFilter(request, response);
System.out.print("NoCacheFilter");
}
public void init(FilterConfig arg0) throws ServletException {
}
}
web.xml配置
<filter>
<filter-name>NoCacheFilter</filter-name>
<filter-class>com.huadi.tools.NoCacheFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>NoCacheFilter</filter-name>
<url-pattern>/servlet/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>NoCacheFilter</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>