response.setDateHeader() - 缓存不起作用

发布于 2024-10-20 23:19:30 字数 1678 浏览 5 评论 0 原文

我希望缓存网页中的 .png 文件。我在 web.xml 中添加了以下条目

 <filter>  
    <filter-name>ContentFilter</filter-name>  
    <filter-class>filters.ContentFilter</filter-class>  
   <init-param>  
       <description>Add an Expires Header</description>  
       <param-name>expiryDate</param-name>  
       <param-value>Fri, 30 Apr 2021 20:00:00 GMT</param-value>  
   </init-param>  
   </filter>
<filter-mapping>  
   <filter-name>ContentFilter</filter-name>  
  <url-pattern>*.png</url-pattern>  
</filter-mapping>

在 init() 中按以下方式设置 expiryDate 字段值

String expiryDateStr = filterConfig.getInitParameter("expiryDate");
    SimpleDateFormat format = new SimpleDateFormat(
            "EEE, d MMM yyyy HH:mm:ss Z");
    try {
        Date d = format.parse(expiryDateStr);
        expiryDate = d.getTime();
    } catch (ParseException e) {
        logger.error(e.getMessage(), e);
    }

doFilter() 是:

public void doFilter(ServletRequest req, ServletResponse res,
        FilterChain filChain) throws IOException, ServletException {
    logger.debug("doFilter()");
    logger.info(((HttpServletRequest)req).getRequestURL().toString());
    filChain.doFilter(req, res);
    if (res instanceof HttpServletResponse) {
        HttpServletResponse response = (HttpServletResponse) res;
        logger.info(((HttpServletRequest)req).getRequestURL().toString());
        response.setDateHeader("Expires", expiryDate); 
    }
}

我的问题是,每当我在浏览器中刷新网页时,客户端都会不断请求 .png 文件。猜猜我的过滤器不起作用。这个配置正确吗?

I want .png files in my web page to be cached. I added the following entry in web.xml

 <filter>  
    <filter-name>ContentFilter</filter-name>  
    <filter-class>filters.ContentFilter</filter-class>  
   <init-param>  
       <description>Add an Expires Header</description>  
       <param-name>expiryDate</param-name>  
       <param-value>Fri, 30 Apr 2021 20:00:00 GMT</param-value>  
   </init-param>  
   </filter>
<filter-mapping>  
   <filter-name>ContentFilter</filter-name>  
  <url-pattern>*.png</url-pattern>  
</filter-mapping>

Setting expiryDate field value in the following manner in init()

String expiryDateStr = filterConfig.getInitParameter("expiryDate");
    SimpleDateFormat format = new SimpleDateFormat(
            "EEE, d MMM yyyy HH:mm:ss Z");
    try {
        Date d = format.parse(expiryDateStr);
        expiryDate = d.getTime();
    } catch (ParseException e) {
        logger.error(e.getMessage(), e);
    }

The doFilter() is :

public void doFilter(ServletRequest req, ServletResponse res,
        FilterChain filChain) throws IOException, ServletException {
    logger.debug("doFilter()");
    logger.info(((HttpServletRequest)req).getRequestURL().toString());
    filChain.doFilter(req, res);
    if (res instanceof HttpServletResponse) {
        HttpServletResponse response = (HttpServletResponse) res;
        logger.info(((HttpServletRequest)req).getRequestURL().toString());
        response.setDateHeader("Expires", expiryDate); 
    }
}

My problem is, whenever i refresh the web page in the browser, the client keeps requesting the .png files. Guess my filter is not working. Is this configuration correct?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笔落惊风雨 2024-10-27 23:19:30

查看您的代码,可能的罪魁祸首是您在 servlet 处理请求之后设置响应标头。此时添加标头已经太晚了,响应数据已经发送。

response.setDateHeader 移至 filChain.doFilter 之前,并且应发送标头。

话虽如此,众所周知,这些东西很难做好。浏览器对 HTTP 缓存有各种不同的行为,发送您认为正确的标头并不总是能达到您想要的效果。

尝试使用 HTTP 标头嗅探工具(例如优秀的“Live HTTP headers” Firefox 插件)来查看实际来回的内容。

Looking at your code, a likely culprit is that you're setting the response header after the request has been processed by the servlet. It's too late to add a header at the point, the response data is already sent.

Move the response.setDateHeader to before the filChain.doFilter, and the header should be sent.

Having said that, this stuff is notoriously tricky to get right. Browsers have all sort of different behaviours for HTTP caching, and sending what you think is the right header doesn't always have the effect you're looking for.

Try using an HTTP header sniffing tool (like the excellent "Live HTTP Headers" plugin for firefox) to see what's actually going back and forth.

殊姿 2024-10-27 23:19:30

“到期”日期不应超过未来一年。
请参阅 http://www.w3.org/Protocols/rfc2616/ 中的第 14.21 节过期rfc2616-sec14.html

要将响应标记为“永不过期”,
源服务器发送过期日期
从那时起大约一年
响应已发送。 HTTP/1.1 服务器
不应发送更多过期日期
未来一年内。

'Expires' date should not be more than one year in future.
See Section 14.21 Expires in http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html

To mark a response as "never expires,"
an origin server sends an Expires date
approximately one year from the time
the response is sent. HTTP/1.1 servers
SHOULD NOT send Expires dates more
than one year in the future.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文