java - 有什么方法可以阻止浏览器每秒请求相同的图像?

标签 java jsp google-chrome firefox jakarta-ee

我的 jsp 页面每秒刷新一次。页面上有两张图片。一个重 13 KB,另一个重 4 KB。 Firefox 不会一次又一次地加载图像,但 Chrome 会一次又一次地加载这两个图像。而且看起来很糟糕!有什么方法可以阻止浏览器一次又一次请求相同的图像?

最佳答案

您应该设置适当的Expire header 和Last-Modified header 。来自 the maker of Chrome :

Recommendations

  • Set caching headers aggressively for all static resources. For all cacheable resources, we recommend the following settings: Set Expires to a minimum of one month, and preferably up to one year, in the future. (We prefer Expires over Cache-Control: max-age because it is is more widely supported.) Do not set it to more than one year in the future, as that violates the RFC guidelines. If you know exactly when a resource is going to change, setting a shorter expiration is okay. But if you think it "might change soon" but don't know when, you should set a long expiration and use URL fingerprinting (described below). Setting caching aggressively does not "pollute" browser caches: as far as we know, all browsers clear their caches according to a Least Recently Used algorithm; we are not aware of any browsers that wait until resources expire before purging them.

  • Set the Last-Modified date to the last time the resource was changed. If the Last-Modified date is sufficiently far enough in the past, chances are the browser won't refetch it.

如果您在 HTTPS 下提供服务,则需要在服务器上拥有正确签名的证书,否则 Chrome 不会缓存任何内容。

关于java - 有什么方法可以阻止浏览器每秒请求相同的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14051338/

相关文章:

css - Chrome 浏览器中的自动宽度不起作用

java - 非法静态接口(interface)方法调用

java - 服务程序 : OutOfMemory Error

java - 创建多个 InternalResourceViewResolver

python - 在 Python 中截取 url 的更好方法

html - Chrome 上的 Css 溢出和绝对

java - 如何在 swing 中制作类似 youtube 的视频列表

java - 我该如何修复我的代码?二维数组

java - 将数据保存到 CouchDB 服务器的 HTTP PUT 请求 - Java Android?

javascript - 如果 session 值为 null,则更改 JavaScript 中的下拉值