將HTTP響應頭信息中的緩存參數設置爲不進行緩存。html
在 java web 項目的 web.xml 文件中添加以下代碼。java
<!--設置不使用緩存的過濾器配置 開始 --> <filter> <filter-name>ClearCacheFilter</filter-name> <filter-class>com.hmw.filter.ClearCacheFilter</filter-class> </filter> <filter-mapping> <filter-name>ClearCacheFilter</filter-name> <url-pattern>*.do</url-pattern> </filter-mapping> <filter-mapping> <filter-name>ClearCacheFilter</filter-name> <url-pattern>*.jsp</url-pattern> </filter-mapping> <!--設置不使用緩存的過濾器配置 結束 -->
package com.hmw.filter; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletResponse; /** * 使瀏覽器不緩存頁面信息的過濾器 * 建立日期:2012-01-10 * * @author */ public class ClearCacheFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException { HttpServletResponse httpResponse = (HttpServletResponse) response; httpResponse.setHeader("Cache-Control", "no-cache"); httpResponse.setHeader("Pragma", "no-cache"); httpResponse.setDateHeader("Expires", -1); filterChain.doFilter(request, response); } @Override public void destroy() { } }