1

如何定义可缓存页面的内容?我正在使用 Google Chrome 审核功能,它告诉我:

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 文件,因为它们不再出现在 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
4

1 回答 1

0

我必须添加这一行以缓存所有图像和主 JSP 文件:

httpResponse.setHeader("Pragma", "private");
于 2012-08-16T08:51:52.470 回答