Java 防止 IE 缓存

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

Prevent IE caching

javainternet-explorerjspstrutsbrowser-cache

提问by GorillaApe

I am developing a Java EE web application using Struts. The problem is with Internet Explorer caching. If an user logs out he can access some pages because they are cached and no request is made. If I hit refresh it works fine. Also if an user goes to login page again it won't redirect him because that page is also cached.

我正在使用 Struts 开发 Java EE Web 应用程序。问题在于 Internet Explorer 缓存。如果用户注销,他可以访问某些页面,因为它们被缓存并且没有发出请求。如果我点击刷新它工作正常。此外,如果用户再次访问登录页面,它不会重定向他,因为该页面也已缓存。

Two solutions come to my mind:

我想到了两个解决方案:

  1. Writing an Interceptor (servlet filter like) to add to response header no-cache etc.
  2. Or or put <meta>tags at each page.
  1. 编写一个拦截器(类似于 servlet 过滤器)以添加到响应头 no-cache 等。
  2. 或或<meta>在每个页面上放置标签。

Which one should I do?

我应该做哪一个?

采纳答案by BalusC

Rather set the following headers on the HttpServletResponseof the page(s) in question so that you don't need to copypaste it over all pages manually:

而是在有HttpServletResponse问题的页面上设置以下标题,这样您就不需要手动将其复制粘贴到所有页面上:

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.

This is equivalent to setting the following meta headers in the page(s) manually:

这相当于在页面中手动设置以下元标题:

<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Expires" content="0">

Also see this answer. Don't forget to clear browser cache before testing ;)

另请参阅此答案。测试前不要忘记清除浏览器缓存;)

回答by Femaref

Modify the headers with no-cache etc. It is the usual way.

使用 no-cache 等修改标题。这是通常的方法。

回答by laz

I've found the following to work well:

我发现以下工作正常:

response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate, max-age=0, proxy-revalidate, no-transform, pre-check=0, post-check=0, private");
response.setHeader("Pragma", "no-cache");
response.setDateHeader("Expires", 0);

From the tags on this question it looks like you are using Struts. Struts 1.x allows you to do this through configuration in struts-config.xml by setting nocache="true"on the controllerelement:

从这个问题的标签来看,您似乎正在使用 Struts。Struts 1.x 允许您通过在元素nocache="true"上设置 struts-config.xml 中的配置来执行此操作controller

<controller processorClass="org.apache.struts.tiles.TilesRequestProcessor" nocache="true" />

Mark Nottingham's caching tutorialis the best resource I've seen on the web about HTTP and caching if you are looking to understand more.

如果您想了解更多信息,Mark Nottingham 的缓存教程是我在网络上看到的关于 HTTP 和缓存的最佳资源。

That being said, depending on the problem you are seeing it might be a browser history issue. See herefor more information about that.

话虽如此,根据您所看到的问题,它可能是浏览器历史记录问题。有关更多信息,请参见此处

回答by Andy N

Looks like IE < 9 will still cache even if you have pragma: no-cache in the head and set browser to refresh on each page load. You need to add the meta tags again in a second head section before close of the html. This is right from MS itself.

看起来 IE < 9 仍然会缓存,即使你有 pragma: no-cache 在头部并设置浏览器在每个页面加载时刷新。您需要在关闭 html 之前在第二个头部部分再次添加元标记。这正是来自 MS 本身。

http://support.microsoft.com/kb/222064/

http://support.microsoft.com/kb/222064/

little better explanation here

更好的解释在这里

http://www.htmlgoodies.com/beyond/reference/article.php/3472881/So-You-Dont-Want-To-Cache-Huh.htm

http://www.htmlgoodies.com/beyond/reference/article.php/3472881/So-You-Dont-Want-To-Cache-Huh.htm

From testing you also need the Expires: -1 meta tag to make it work. It is recommended to use Expires: -1 and not 0.

从测试中,您还需要 Expires: -1 元标记才能使其工作。建议使用 Expires: -1 而不是 0。

回答by Alex.Zhe.Han

Add tag type="button" into actual action button.

将标签 type="button" 添加到实际操作按钮中。

The default value of the type attribute depends on the current document compatibility mode. The default value is submit. In other compatibility modes the default value is button. When the BUTTON element is submitted in a form, the value depends on the current document compatibility mode. Windows Internet Explorer 8 and later. The default value of the type attribute depends on the current document compatibility mode. In IE8 Standards mode, the default value is submit. In other compatibility modes and earlier versions of Windows Internet Explorer, the default value is button. Internet Explorer 8 and later. When the BUTTON element is submitted in a form, the value depends on the current document compatibility mode. In IE8 mode, the value attribute is submitted. In other document modes and earlier versions of Internet Explorer, the innerText value is submitted.

type 属性的默认值取决于当前的文档兼容模式。默认值为提交。在其他兼容模式下,默认值为按钮。在表单中提交 BUTTON 元素时,该值取决于当前的文档兼容模式。Windows Internet Explorer 8 及更高版本。type 属性的默认值取决于当前的文档兼容模式。在 IE8 标准模式下,默认值为提交。在其他兼容模式和早期版本的 Windows Internet Explorer 中,默认值为按钮。Internet Explorer 8 及更高版本。在表单中提交 BUTTON 元素时,该值取决于当前的文档兼容模式。在 IE8 模式下,提交 value 属性。在其他文档模式和早期版本的 Internet Explorer 中,

http://msdn.microsoft.com/en-us/library/ie/ms535211(v=vs.85).aspx

http://msdn.microsoft.com/en-us/library/ie/ms535211(v=vs.85).aspx