Jump to Real's How-to Main page

Prevent caching of a JSP output

You will need to set the appropriate HTTP header attributes.
<%
response.setHeader("Cache-Control","no-cache"); 
response.setHeader("Pragma","no-cache"); 
response.setDateHeader ("Expires", -1); 
%>

However, cache handling is tricky with IE brower.

See http://support.microsoft.com/kb/q222064/.

By adding a second HEAD is supposed to solve the problem!

<%
response.setHeader("Cache-Control","no-cache"); 
response.setHeader("Pragma","no-cache"); 
response.setDateHeader ("Expires", -1);
%>
<HTML>
<HEAD> 
</HEAD> 
<BODY>
my page body

</BODY>
<HEAD> 
<META HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE"> 
<META HTTP-EQUIV="Expires" CONTENT="-1">
</HEAD> 
</HTML>
NOTE: Pragma: no-cache prevents caching only when used over a secure connection, Expires: -1 should do the job over unsecure conection.

See also this HowTo and this one.


If you find this article useful, consider making a small donation
to show your support for this Web site and its content.

Written and compiled by Réal Gagnon ©1998-2007
[ home ]