4

I understand there is a HTTP response header directive to disable page caching:

Cache-Control:no-cache

I can modify the header by "hand":

 <%response.addHeader("Cache-Control","no-cache");%>

But is there a "nice" way to make the JSP interpreter return this header line in the server response?

(I checked the <%@page ...%> directive. It seems there is nothing like that.)

Swati
  • 50,291
  • 4
  • 36
  • 53
Eduard Wirch
  • 9,785
  • 9
  • 61
  • 73

4 Answers4

3

Also add

response.addHeader("Expires","-1");
response.addHeader("Pragma","no-cache");

to your headers and give that a shot.

Eric Wendelin
  • 43,147
  • 9
  • 68
  • 92
  • I tried with the following code, its is not caching the page but when I hit browser back button, browser says this document no more exist , reload the page. Why it is not reloading the page automatically ? `<% response.setHeader("Cache-Control", "no-cache"); response.setHeader("Cache-Control", "no-store"); response.setHeader("Pragma", "no-cache"); response.setDateHeader("Expires", 0); %>` – Sadanand Apr 30 '14 at 09:06
0

If you were using a servlet, then I believe what you posted in the question would be the correct approach. I'm not aware of any way to do this in the JSP.

pkaeding
  • 36,513
  • 30
  • 103
  • 141
0
<?xml version="1.0"?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.0"> 
 <jsp:scriptlet><![CDATA[
   response.setHeader("Cache-Control", "no-cache");
 ]]></jsp:scriptlet>
</jsp:root>

You must put the response header inside <jsp:root />. Also, I would instead recommend it sending this from your servlet instead of JSP page.

Swati
  • 50,291
  • 4
  • 36
  • 53
0

IIRC some browsers may ignore the cache control settings in some contexts. The 'safe' workaround for this was to always get a page (even an AJAX chunk) with a new query string variable (like the time.)

Kev
  • 15,899
  • 15
  • 79
  • 112