日期:2014-05-16  浏览次数:20312 次

按IE后退按钮时让JSP不读缓存

1,使用java提供的方法,在jsp或者servlet中都可以

<%
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires",0);
%>

2,使用HTML标记,如下面:

<HEAD>

<METAHTTP-EQUIV="Pragma"CONTENT="no-cache">

<METAHTTP-EQUIV="Cache-Control"CONTENT="no-cache">

<METAHTTP-EQUIV="Expires"CONTENT="0">

</HEAD>

附加说明:

HTTP头信息“Expires”和“Cache-Control”为应用程序服务器提供了一个控制浏览器和代理服务器上缓存的机制。HTTP头信息Expires告诉代理服务器它的缓存页面何时将过期。HTTP1.1规范中新定义的头信息Cache-Control可以通知浏览器不缓存任何页面。当点击后退按钮时,浏览器重新访问服务器已获取页面。如下是使用Cache-Control的基本方法:

  1) no-cache:强制缓存从服务器上获取新的页面

  2) no-store: 在任何环境下缓存不保存任何页面

  HTTP1.0规范中的Pragma:no-cache等同于HTTP1.1规范中的Cache-Control:no-cache,同样可以包含在头信息中。

  通过使用HTTP头信息的cache控制,第二个示例应用logoutSampleJSP2解决了logoutSampleJSP1的问题。logoutSampleJSP2与logoutSampleJSP1不同表现在如下代码段中,这一代码段加入进所有受保护的页面中:

//...
response.setHeader("Cache-Control","no-cache"); //Forces caches to obtain a new copy of the page from the origin server
response.setHeader("Cache-Control","no-store"); //Directs caches not to store the page under any circumstance
response.setDateHeader("Expires", 0); //Causes the proxy cache to see the page as "stale"
response.setHeader("Pragma","no-cache"); //HTTP 1.0 backward compatibility
String userName = (String) session.getAttribute("User");
if (null == userName) {
 request.setAttribute("Error", "Session has ended. Please login.");
 RequestDispatcher rd = request.getRequestDispatcher("login.jsp");
 rd.forward(request, response);
}
//...
方法二:
?? 在IE中也可通过设置实现:把/工具/INTERNET选项/常规/设置/的检察所存页面的较新版本,设为每次访问该页时都检查.