1
如何定義頁面緩存內容?我使用谷歌鉻合金審計功能,它告訴我:在JSP中啓用瀏覽器緩存
The following resources are explicitly non-cacheable. Consider making them cacheable if possible.
我創建了一個新的Filter
被稱爲每個請求:
public class CachingFilter implements Filter {
public void init(FilterConfig filterConfig) {
}
public void destroy() {
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletResponse httpResponse = (HttpServletResponse) response;
httpResponse.setHeader("Cache-Control", "private");
httpResponse.setDateHeader("Expires", System.currentTimeMillis() + 604800000L); // one week
httpResponse.setDateHeader("Max-Age", System.currentTimeMillis() + 604800000L);
chain.doFilter(request, response);
}
}
這Filter
作品所有的JavaScript文件,因爲他們不」 t再次出現在Google審覈工具中。但是我仍然得到一張圖像列表和主要的JSP文件。我還需要設置哪些其他頭文件才能爲它們啓用緩存?
這是一個圖像的Request-Header
:
Accept:*/*
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Cache-Control:max-age=0
Connection:keep-alive
Cookie:entries=20; JSESSIONID=fSSOjMOknqW4yk0bH1Nxy2Ea.undefined
Host:localhost:8080
If-Modified-Since:Mon, 13 Aug 2012 12:52:32 GMT
If-None-Match:W/"1685-1344862352945"
Referer:http://localhost:8080/SSIS2_JBoss7/View/PackageManagement/Packages.jsp
User-Agent:Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.79 Safari/537.1
而且圖像的Response-Header
:
Cache-Control:private
Date:Thu, 16 Aug 2012 08:33:05 GMT
ETag:W/"1685-1344862352945"
Expires:Thu, 23 Aug 2012 08:33:05 GMT
Max-Age:Thu, 23 Aug 2012 08:33:05 GMT
Pragma:No-cache
您確定Chrome審覈是將JSP文件本身引用d不是由JSP生成的HTML輸出引用的CSS/JS /圖像資源?如果是後者,那麼你顯然需要在這些資源的響應上設置標題,而不是在JSP的響應上。 – BalusC 2012-08-15 13:36:36
是的,Chrome審覈是指JSP文件,圖像(png,gif),JavaScript文件和CSS文件。 – doonot 2012-08-16 07:02:34