HTTP 响应缓存 [英] HTTP response caching

查看:27
本文介绍了HTTP 响应缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

谢谢,唐

推荐答案

不,这不是正确的方法.正确方法如下:

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.

这篇关于HTTP 响应缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆