防止IE缓存

Gor*_*Ape 17 java internet-explorer jsp struts browser-cache

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

我想到了两个解决方案:

  1. 编写一个Interceptor(像servlet过滤器一样)来添加到响应头no-cache等.
  2. 或者<meta>在每个页面上放置标签.

我应该做哪一个?

Bal*_*usC 29

而是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.
Run Code Online (Sandbox Code Playgroud)

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

<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">
Run Code Online (Sandbox Code Playgroud)

另见这个答案.在测试之前不要忘记清除浏览器缓存;)


laz*_*laz 5

我发现以下内容效果很好:

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);
Run Code Online (Sandbox Code Playgroud)

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

<controller processorClass="org.apache.struts.tiles.TilesRequestProcessor" nocache="true" />
Run Code Online (Sandbox Code Playgroud)

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

话虽如此,根据您遇到的问题,它可能是浏览器历史记录问题.有关详细信息,请参见此处.