本文實例總結了JSP清除頁面緩存常用方法。分享給大家供大家參考,具體如下:
一、清除頁面緩存
在jsp頁裡
<%response.setHeader("Pragma","No-cache"); response.setHeader("Cache-Control","no-cache"); response.setDateHeader("Expires", 0); response.flushBuffer();%>
在html頁裡:
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <META HTTP-EQUIV="Pragma" CONTENT="no-cache"> <META HTTP-EQUIV="Cache-Control" CONTENT="no-cache"> <META HTTP-EQUIV="Expires" CONTENT="0">
二、清除cookie
<% Cookie killMyCookie = new Cookie("mycookie", null); killMyCookie.setMaxAge(0); killMyCookie.setPath("/"); response.addCookie(killMyCookie); %>
三、清除session
清除session方法
<%@ page language="java" %> <% session.invalidate(); %>
在頁面關閉時清除session,需要捕獲windows.onclose事件,再調用清除session方法
補充:如何使用 JavaScript 代碼清除緩存?
如果在服務器級不能生成隨機數,您可以使用 JavaScript 代碼在客戶端(浏覽器)級生成一個字符串。以下示例圖解說明如何使用標准圖像代碼或 Iframe/JavaScript 代碼執行緩存清除。
這些僅為示例。如果要使用該代碼,請為特定 HTML 代碼對示例作相應調整。您必須將實際代碼粘貼到這些示例中的相應位置。
標准圖像代碼示例:
<SCRIPT LANGUAGE="JavaScript"> <!== hide from non-JavaScript browsers var axel = Math.random() + ""; var num = axel * 1000000000000000000; document.writeln('<A HREF="http://ad.doubleclick.net/jump/N409.somesite/B470;sz=468x60;ord='+ num +'?"><IMG SRC="http://ad.doubleclick.net/ad/N409.somesite/B470;sz=468x60;ord='+ num +'?" BORDER=0 WIDTH=468 HEIGHT=60 ALT="Click Here!"> Click Here!</A>'); // end hide from browsers ==> </SCRIPT> <NOSCRIPT> <A HREF="http://ad.doubleclick.net/jump/N409.somesite/B470;sz=468x60;ord=[timestamp]?"> <IMG SRC="http://ad.doubleclick.net/ad/N409.somesite/B470;sz=468x60;ord=[timestamp]?" BORDER=0 WIDTH=468 HEIGHT=60 ALT="Click Here!"> Click Here!</A> </NOSCRIPT>
Iframe/JavaScript 代碼示例:
<SCRIPT LANGUAGE="JavaScript"> <!== hide from non-JavaScript browsers var axel = Math.random() + ""; var num = axel * 1000000000000000000; document.writeln('<IFRAME SRC="http://ad.doubleclick.net/adi/N409.somesite/B470;sz=468x60;ord='+ num +'?" WIDTH=470 HEIGHT=62 MARGINWIDTH=0 MARGINHEIGHT=0HSPACE=0 VSPACE=0 FRAMEBORDER=0 SCROLLING=no BORDERCOLOR="#000000">'); document.writeln('<SCR'+'IPT language="JavaScript1.1" SRC="http://ad.doubleclick.net/adj/N409.somesite/B470;abr=!ie;sz=468x60;ord='+ num +'?"></SCR'+'IPT></IFRAME>'); // end hide from browsers ==> </script> <noscript> <A HREF="http://ad.doubleclick.net/jump/N409.somesite/B470;abr=!ie4;abr=!ie5;sz=468x60;ord=?"> <IMG SRC="http://ad.doubleclick.net/ad/N409.somesite/B470;abr=!ie4;abr=!ie5;sz=468x60;ord=?" BORDER=0 WIDTH=468 HEIGHT=60 ALT="Click Here!"> Click Here!</A> <noscript>
希望本文所述對大家JSP程序設計有所幫助。