在asp里我曾告訴過朋友如何讓asp程序不從緩存里讀取數據,同樣在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選項/常規/設置/的檢察所存頁面的較新版本,設為每次訪問該頁時都檢查.
新聞熱點
疑難解答