JSP Summary of Common Methods of Clearing Page Cache

  • 2021-08-28 20:49:32
  • OfStack

This paper summarizes the common methods of clearing page cache in JSP. Share it for your reference, as follows:

1. Clear the page cache

In the jsp page


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

On the html page:


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

2. Clear cookie


<% 
  Cookie killMyCookie = new Cookie("mycookie", null); 
  killMyCookie.setMaxAge(0); 
  killMyCookie.setPath("/"); 
  response.addCookie(killMyCookie); 
%>

3. Clear session

Clear session method


<%@ page language="java" %> 
<% 
session.invalidate(); 
%>

To clear session when the page is closed, you need to capture the windows. onclose event and then call the clear session method

Add: How to clear the cache using JavaScript code?

If you cannot generate random numbers at the server level, you can use JavaScript code to generate a string at the client (browser) level. The following example illustrates how to perform cache cleanup using standard image code or Iframe/JavaScript code.

These are examples only. If you want to use this code, adjust the example for specific HTML code. You must paste the actual code into the appropriate position in these examples.

Standard image code example:


<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 code example:


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

I hope this article is helpful to everyone's JSP programming.


Related articles: