应用避免访问浏览器缓存
?
?
项目经常遇到这么个问题:程序做了改动之后,由于浏览器存在缓存,用户看到的依旧是程序未改动时的效果。
可以尝试的解决方案有:
a)url后添加随机数
b)修改html头标记
c)修改ie浏览器不查看缓存或者不保存历史记录
d)清理缓存
etc...
?
今天尝试了用过滤器的方法,好使,给力。
?
package ******;import java.io.*;import javax.servlet.*;import javax.servlet.http.HttpServletResponse;public class ForceNoCacheFilter implements Filter {public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {((HttpServletResponse) response).setHeader("Cache-Control", "no-cache");((HttpServletResponse) response).setHeader("Pragma", "no-cache");((HttpServletResponse) response).setDateHeader("Expires", -1);filterChain.doFilter(request, response);}public void destroy() {}public void init(FilterConfig filterConfig) throws ServletException {}}
?
<filter> <filter-name>NoCache</filter-name> <filter-class>过滤器的包名.类名</filter-class></filter><filter-mapping> <filter-name>NoCache</filter-name> <url-pattern>/*</url-pattern></filter-mapping>
?