只需要两步:
1、定义一个Filter:
/** * 防止浏览器缓存页面或请求结果 * @author XuJijun * */ public class NoCacheFilter implements Filter { @Override public void destroy(){ } @Override public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException { HttpServletResponse response = (HttpServletResponse)resp; response.setDateHeader("Expires", -1); //告诉浏览器数据可以缓存多长时间,-1或0表示不缓存 response.setHeader("Cache_Control", "no-cache"); //支持HTTP 1.1,告诉浏览器要不要缓存数据,如“no-cache” response.setHeader("Pragma", "no-cache"); //支持HTTP 1.0,告诉浏览器要不要缓存数据,如“no-cache” chain.doFilter(req, resp); } @Override public void init(FilterConfig arg0) throws ServletException { } }
<!-- 防止浏览器缓存页面或请求结果 --> <filter> <filter-name>NoCacheFilter</filter-name> <filter-class>com.xjj.web.filter.NoCacheFilter</filter-class> </filter> <filter-mapping> <filter-name>NoCacheFilter</filter-name> <url-pattern>/*</url-pattern> <!-- 根据需要调整url-pattern --> </filter-mapping>
(原创文章,转载请注明转自Clement-Xu的csdn博客:http://blog.csdn.net/clementad/article/details/46763669)