4

I want to disable the cache for a JSP file on my google app engine website.

I have this:

<%
        // Set to expire far in the past.
        response.setHeader("Expires", "Sat, 6 May 1995 12:00:00 GMT");

        // Set standard HTTP/1.1 no-cache headers.
        response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");

        // Set IE extended HTTP/1.1 no-cache headers (use addHeader).
        response.addHeader("Cache-Control", "post-check=0, pre-check=0");

        // Set standard HTTP/1.0 no-cache header.
        response.setHeader("Pragma", "no-cache");
%> 

But the JSP is still in cache. I need to kill the user session and login again in order to reload the JSP code.

How do I disable the cache for a app engine JSP?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Donalds
  • 83
  • 2
  • 5
  • Are you debugging on localhost? – systempuntoout Apr 25 '10 at 14:22
  • What do your headers look like from Firebug or Chrome Debugger? Are you sure the browser is caching them? I don't have this problem with my JSP pages. Is it possible they're being cached server-side? – jamesmortensen Jan 16 '11 at 02:28
  • Firebug is showing the above headers, but I determined it is an issue with Spring MVC, not caching. I'm updating an entity, then returning a Spring MVC REDIRECT:/some/page.html. In the handler for /some/page.html, my entity changes are not being picked up. Still debugging, but this is one of those blasted issues where I just can't seem to understand exactly what is going on and has me flustered beyond reason! – DustinB Jan 16 '11 at 02:59
  • @Dustin - Are you explicitly putting the object back in the model before the redirect? Just a thought. Hope it helps. – jamesmortensen Jan 16 '11 at 04:46
  • Bit of a hack... But you could just add a timestamp to the end of the URL for the script block. – eSniff Jan 19 '11 at 06:47
  • Well, what is odd is I just return a string such as "REDIRECT:/somepage.do" which in turn re-retrieves the object and puts it into the model... However, it seems to retrieve the old values of the object. To work around, what I did was flatten the object and just return string values in the model and it all works now (ie, instead of returning myUser and accessing in jsp as myUser.name, I return "user_name" in the model. Guess it may be a GAE caching issue. At least there's a workaround now. Thanks for all the ideas. @eSniff -- I tried that as well as putting it on the url, but didn't help. – DustinB Jan 20 '11 at 19:55
  • Isn't it: response.setDateHeader("Expires", -1); (I'm not an expert, just did some searching, maybe this can help: http://www.daniweb.com/forums/thread171801.html) – Nick Jan 21 '11 at 10:54

1 Answers1

3

It might at that point be too late to change the response headers. They will simply be ignored. You can verify the presence of response headers in a HTTP debugger tool like Firebug.


alt text


In the JSP, you need to ensure that they are set before the response is committed (i.e. all headers are already sent; you cannot send another headers afterwards). I.e. ensure that they are in the very top of the JSP file and that no template text is before that piece of scriptlet. Template text may cause a commit of the response. The normal practice, however, is to use a Filter for this.

Implement javax.servlet.Filter wherein doFilter() method look like follows:

HttpServletResponse httpResponse = (HttpServletResponse) response;
httpResponse.setHeader("Cache-Control", "no-cache", "no-store", "must-revalidate"); // HTTP 1.1
httpResponse.setHeader("Pragma", "no-cache"); // HTTP 1.0
httpResponse.setDateHeader("Expires", 0); // Proxies.
chain.doFilter(request, response);

Map this in web.xml on an url-pattern of *.jsp and it ought to work.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555