java 用于浏览器缓存的 Servlet 过滤器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3374703/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 01:33:38  来源:igfitidea点击:

Servlet filter for browser caching?

javaservletscachingservlet-filters

提问by Shadowman

Does anyone know how to go about coding a servlet filter that will set cache headers on a response for a given file/content type? I've got an app that serves up a lot of images, and I'd like to cut down on bandwidth for hosting it by having the browser cache the ones that don't change very often. Ideally, I'd like to be able to specify a content type and have it set the appropriate headers whenever the content type matches.

有谁知道如何编写 servlet 过滤器,该过滤器将在给定文件/内容类型的响应上设置缓存标头?我有一个提供大量图像的应用程序,我想通过让浏览器缓存不经常更改的图像来减少托管它的带宽。理想情况下,我希望能够指定内容类型,并在内容类型匹配时设置适当的标题。

Does anyone know how to go about doing this? Or, even better, have sample code they'd be willing to share? Thanks!

有谁知道如何去做?或者,更好的是,有他们愿意分享的示例代码?谢谢!

回答by Bozho

In your filter have this line:

在您的过滤器中有这一行:

chain.doFilter(httpRequest, new AddExpiresHeaderResponse(httpResponse));

Where the response wrapper looks like:

响应包装器如下所示:

class AddExpiresHeaderResponse extends HttpServletResponseWrapper {

    public static final String[] CACHEABLE_CONTENT_TYPES = new String[] {
        "text/css", "text/javascript", "image/png", "image/jpeg",
        "image/gif", "image/jpg" };

    static {
        Arrays.sort(CACHEABLE_CONTENT_TYPES);
    }

    public AddExpiresHeaderResponse(HttpServletResponse response) {
        super(response);
    }

    @Override
    public void setContentType(String contentType) {
        if (contentType != null && Arrays.binarySearch(CACHEABLE_CONTENT_TYPES, contentType) > -1) {
            Calendar inTwoMonths = GeneralUtils.createCalendar();
            inTwoMonths.add(Calendar.MONTH, 2);

            super.setDateHeader("Expires", inTwoMonths.getTimeInMillis());
        } else {
            super.setHeader("Expires", "-1");
            super.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
        }
        super.setContentType(contentType);
    }
}

In short, this creates a response wrapper, which, on setting the content type, adds the expires header. (If you want, you can add whatever other headers you need as well). I've been using this filter + wrapper and it works.

简而言之,这将创建一个响应包装器,它在设置内容类型时添加 expires 标头。(如果你愿意,你也可以添加你需要的任何其他标题)。我一直在使用这个过滤器 + 包装器,它有效。

See this questionon one specific problem that this solves, and the original solution by BalusC.

请参阅有关此问题解决的一个特定问题的问题,以及 BalusC 的原始解决方案。

回答by Nikita Koksharov

Here is the ready solution for this https://github.com/samaxes/javaee-cache-filter

这是此https://github.com/samaxes/javaee-cache-filter的现成解决方案