Local database reading edition

How to Prevent JSP or Servlet pages being cached ?

Reconstructed from database records, not a captured original forum layout. Dates and display names are retained as recorded; timezone and real-world identity are not inferred. Historical advice, contact details and offers are not current. Original markup is shown as inert text.

Forum index · Java - GUI/JSP/Servlet

Opening post

administrator · 2001-02-12 18:58:31

How do I prevent the output of my JSP or Servlet pages from being cached by the browser? You will need to set the appropriate HTTP header attributes to prevent the dynamic content output by the JSP page from being cached by the browser. Just execute the following scriptlet at the beginning of your JSP pages to prevent them from being cached at the browser. You need both the statements to take care of some of the older browser versions. <% response.setHeader("Cache-Control","no-store"); //HTTP 1.1 response.setHeader("Pragma","no-cache"); //HTTP 1.0 response.setDateHeader ("Expires", 0); //prevents caching at the proxy server %> However, please note that there are some problems with disabling page caching under IE 5.0 due to the unique buffering requirements of the browser. Please see the Microsoft knowledge base for details: http://support.microsoft.com/support/kb/articles/Q222/0/64.ASP http://support.microsoft.com/support/kb/articles/Q234/2/47.ASP -------------feedback-------- I used the above code and it worked for correctly by not caching the html page. But it still cached the image in the page. This is what i am doing: The user will be able to upload a image using a page that i am currently constructing. I am using the "file" input component. As soon as the user finishes the selection of the image and presses "ok" the form will be submitted to the jsp that produced this page (using "onpropertychange" or "onchange" event). So the same page will be generated again but this time with the new image. I tried using this page a couple of times.. But i am still getting one of the previously selected images sometimes (it might work the first time i submit or sometimes even the second time bu eventually it gives me an old image)... and when i see the info of that page (in netscape - right click and select "info") it shows me that the image is cached. --------- feedback -------------- I think it's a Netscape bug... Netscape's caching has always been really lame. Another technique that can help is to add a bogus, random parameter to the image URL, like <img src="image.gif?bogus=<%=System.currentTimeMillis()%>"> or, in a Servlet, out.print("<img src='image.gif?bogus=" + System.currentTimeMillis() + "'>"); ------ feedback------------ I suspect that the first header shown above would be better as "no-cache" instead of "no-store". I haven't tested this, but is based on info from O'Reilly's "Webmaster in a Nutshell" ------ feedback------------ The image is a separate request from the JSP page. I.e., you need to specify the headers when returning the actual image file. I too have always used "no-cache". Basically, "no-cache" in the response tells the cache that it should still validate the submission before releasing a cached copy. The 'no-store' seems to say that the response should never be stored at all. So, "no-store" is supposed to be a more extreme version of "no-cache" but since it's relatively new (in web time anyways :-), I'm not sure how widely it is supported. Check out section 14.9 in RFC 2616 for more details. ------ feedback------------ I quite understand when you say that "The image is a separate request from the JSP page" since the browser did not cache the html page (which i had expired) but still cached the image! But now, how do i specify the headers for the image alone, in the jsp? Btw... i did solve the problem but writing another servlet that would read the image from the hard disk and stream it out to the jsp. So where my image was previously, now is a servlet that returns the image. ------ feedback------------ I believe you can add NOSAVE to the <IMG> tag to have it not be cached. ------ feedback------------ The solution doesn't seem to work for me. I have the following situation. This is a multi-part transaction spanning 3 pages. The information from one page to another is incrementally populated in a Bean. Some summary information is displayed on each page. When the user first enters the form, all information is blank. When she goes to the second form, and for some reason presses the back button, the first page is displayed but the info is all blank. In other words, the browser seems to have cached the page (it is not displaying the info from the populated bean) ------ feedback------------ Apache has a configuration option allowing you to create a file containing header information that will be added to the response for a static file. I can't remember what the naming convention is, but it's something like "foo.html" -> "foo.meta" ------ feedback------------ Is there a solution to disable page caching in IE 5 browsers? <% response.setHeader("Cache-Control","no-store"); response.setHeader("Pragma","no-cache"); response.setDateHeader ("Expires", 0); %> I've included the above scriplet in my JSP pages. It works fine on Netcape 4.7 but it does NOT work at all on IE 5.5 , once the user has logged off from my JSP applications, without closing the IE5.5 browser , the visited page still can be viewed by clicking the "back" button of IE 5.5. Does anybody have any solution on this? My JSP applications are critical since they are reporting some accounting data and it's very important not to let others view the data by simply clicking back once the user has logged off without closing the browser. Ahimanikya Satapathy http://www.ammu.org/kuku

Source notes

MyCommunity/nandighosha.org.zip!/forum/tools/snitz_forums_2000.mdb
Table: FORUM_TOPICS + FORUM_REPLY · record IDs: 18