栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

在您的过滤器中有以下行:

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

响应包装如下所示:

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);    }}

简而言之,这将创建一个响应包装,该包装在设置内容类型时会添加expires标头。(如果需要,还可以添加所需的其他任何标题)。我一直在使用这个过滤器+包装器,并且可以正常工作。

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



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/463628.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号