File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes how to disable browser caching with jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "how to disable browser caching with jsp" Watch "how to disable browser caching with jsp" New topic

how to disable browser caching with jsp

Stephen Peterson
Ranch Hand

Joined: Dec 25, 2000
Posts: 33
In a class, I was told to execute the following code in order
to disable browser caching:
res.setHeader("Pragma", "no-cache");
res.setHeader("Cache-Control", "no-cache");
res.setdateHeader("Expires", 0);
I can see how I could do this if programming with servlets.
But how is this done when using JSP pages?
The reason I want to disable caching is to prevent the user from
getting into an incorrect application state by using the back-button to post a request from a stale browser page. I've seen jsp applications that always say the page has expired when the backbutton is pushed.
United Health Group
Hartford, CT, USA

United Health Group<BR>Hartford, CT, USA
David O'Meara

Joined: Mar 06, 2001
Posts: 13459

There is an implicit response object in JSPs, so just add this:

[This message has been edited by David O'Meara (edited December 10, 2001).]
Stephen Peterson
Ranch Hand

Joined: Dec 25, 2000
Posts: 33
Hi Dave,
Thanks for your reply. I tried your suggestion. I updated
my JSP file, between the <head> and </head> tags. The only change I needed was to change
response.setdateHeader("Expires", 0);
response.setDateHeader("Expires", 0);
before it was compiled okay.
However, the browser back-button still brings back the prior page content, just as before. I'm certain I'm using the updated JSP page because I made a small change in the title string which is showing up in the browser. What should I look at next?
What do people usually do to solve this problem?
thanks in advance!
[This message has been edited by Stephen Peterson (edited December 10, 2001).]
James Hobson
Ranch Hand

Joined: Aug 28, 2001
Posts: 140
This has been discussed at length in the HTML forum on javaranch.
I believe the best solution is to accept that back does whatever the browser wants, and simply assign each page view a transaction id so that you can track where the user should be (so that, for example, they cannot submit the same order twice).
Stephen Peterson
Ranch Hand

Joined: Dec 25, 2000
Posts: 33
james, you are right. In the html and javascript forum I did a search for back button and got 29 hits. My apologies to all, and thanks for the suggestion to look there.
The transaction id idea sounds intriguing. Since you mentioned it, can you point me to any good sources for examples of implementing this?
I agree. Here's the link:
subject: how to disable browser caching with jsp
It's not a secret anymore!