防止IE缓存

发布于 2024-09-01 19:25:35 字数 291 浏览 2 评论 0原文

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

我想到了两个解决方案:

  1. 编写一个拦截器(类似 servlet 过滤器)以添加到响应标头无缓存等。
  2. 或者在每个页面放置 标记。

我应该做哪一项?

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.

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.

Which one should I do?

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

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

发布评论

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

评论(5

御守 2024-09-08 19:25:35

而是在相关页面的 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.

这相当于在页面中设置以下元标头(s) 手动:

<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">

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

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

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

七七 2024-09-08 19:25:35

我发现以下内容运行良好:

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

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

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

Mark Nottingham 的 缓存教程 是我在网上看到的有关 HTTP 和缓存的最佳资源(如果您愿意的话)希望了解更多。

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

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 controller element:

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

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

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

被翻牌 2024-09-08 19:25:35

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

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

这里有更好的解释

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

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

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.

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

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

下雨或天晴 2024-09-08 19:25:35

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

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

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

Add tag type="button" into actual action 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.

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

归途 2024-09-08 19:25:35

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

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

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