Java HTTP 响应缓存

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3413036/
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-08-13 23:04:46  来源:igfitidea点击:

HTTP response caching

javahttpservletscaching

提问by Dónal

I want to ensure that my servet's response is never cached by the broswer, such that even if two identical requests are made (a nanosecond apart), the server is always contacted. Is this the correct way to achieve this:

我想确保我的服务器的响应永远不会被浏览器缓存,这样即使发出两个相同的请求(相隔一纳秒),服务器也总是被联系。这是实现这一目标的正确方法吗:

class MyServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) {
        response.setHeader("Cache-Control", "no-cache");
    }
}

Thanks, Don

谢谢,唐

采纳答案by BalusC

No, that's not the correct way. Here is the correct way:

不,这不是正确的方法。这是正确的方法:

response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
response.setHeader("Pragma", "no-cache"); // HTTP 1.0.
response.setDateHeader("Expires", 0); // Proxies.

You'll probably see someone else suggesting other entries/attributes, but those are completely irrelevant when at least the above are mentioned.

您可能会看到其他人建议使用其他条目/属性,但至少在提到上述内容时,这些完全无关紧要。

Don't forget to clear your browser cache before testing after the change.

不要忘记在更改后进行测试之前清除浏览器缓存。

See also:

也可以看看:

回答by Kurt Du Bois

According to microsoft, these headers are required for IE:

根据微软的说法,IE 需要这些标头:

  • Cache-Control;
  • Pragma;
  • Expires(that should be negative);
  • 缓存控制
  • 编译指示
  • 到期(应该是负数);

Example:

例子:

Pragma: no-cache
Cache-Control: no-cache
Expires: -1

回答by rsp

We use:

我们用:

    // HTTP 1.1
    response.setHeader("Cache-Control", "private, no-store, no-cache, must-revalidate");
    // HTTP 1.0
    response.setHeader("Pragma", "no-cache");