File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes HTML, CSS and JavaScript and the fly likes Prevent servlet response from being cache Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Reply locked New topic

Prevent servlet response from being cache

Angel J Gama
Ranch Hand

Joined: Jun 28, 2007
Posts: 36
This might be an old-school question, but I've just got stuck with this issue, and I'm not sure what would be the best practice for my scenario :S

Here is the thing:
1) I've a main page where the user can check his records ("/panel.jsp"), but the link to retrieve those records before accessing that page is "/servlet/"
2) So, this servlet retrieves the records from the database and save them into a request attribute. Therefore, I use to goto the main page an populate it with the records in the request attribute.

But whenever I goto the servlet link, I get the same old data, no matter if the user inserted new info (the same old-school cache problem)

- I know that a solution would be to add the timestamp in the servlet link (ex: /servlet/ But I use this link in many pages. So, what would happen if, later on, I add this link in more pages and I forgot to add/create this timestamp param? I'll get the same old data. That's why I think this is not the way to do it in this scenario. I think that the timestamp practice is good for ajax stuff, isn't it?

- Another solution for this scenario would be to add tag in every page I don't want to be cache, but I've read that firefox has some problems with this tag. I'm not sure about safari. Am I wrong?

- Since I'm using a requestDispatcher, I can't use . Is there any other thing I can do in the servlet to prevent from being cache?

What would be the best practice to avoid/prevent the response from being cache?
Thanks a lot!
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15302

The timestamp or random number is a well known hack. And this is really just an issue for IE. I'm not aware of firefox having issues with no-cache however. Do you have a reference to this info? Make sure you include all of these:

Note the pragma is IE only.

GenRocket - Experts at Building Test Data
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63548

Please do not cross-post the same question in multiple forums. It wastes people's time when multiple redundant conversations take place. Please read this for more information.

[Asking smart questions] [About Bear] [Books by Bear]
I agree. Here's the link:
subject: Prevent servlet response from being cache
It's not a secret anymore!