How to prevent a JSP/Sevlet output page from being cached by the browser?
Sometimes we faced the caching problem of Internet Explorer. Some of the pages of any site were unable to show the updated data because the browser displays the old cached pages stored in the memory. There are couple of solutions to prevent browser caching-
JavaScript solution- If the url of the page which we don’t want to be cached becomes unique every time, then browser treats those pages as new pages every time and doesn’t cache them. So whenever there is link to this page we can use JavaScript time function to append a unique time variable after the url of the page. Below is the source of the JavaScript:
|
function time()
{
var hhmmss = new Date();
var hh= hhmmss.getHours();
var mm= hhmmss.getMinutes();
var ss= hhmmss.getSeconds();
var timeval = hh*10000+mm*100+ss;
return timeval;
}
function printPreview() {
var forwardPage = “displayresults.jsp”+“?”+time();
var printPageStyle = “status=no,menubars=no,scrollbars=yes,resize=no,
left=60,top=40,width=700,height=450″;
window.open(forwardPage, printPageStyle);
}
|
The first JavaScript generates a unique string based on the current time and the next JavaScript opens a print preview page by appending the unique string at the end of the URL to be opened. This will always generate a new link and browser will not cache this.
Java Solution- Developer needs to set the HTTP headers in order to prevent the browser to cache the jsp pages or servlet output. The code snippet is given below:
|
<%
response.setHeader(“Cache-Control“,”no-store”); response.setHeader(“Pragma\“,”no-cache”);
response.setDateHeader (“Expires“, 0);
//prevents caching at the proxy server
%> |
This scriplet should be placed at the very start of the Jsp pages and in servlet before outputting the content.
So, happy coding and provide me your feedback. Do post your comments if you’ve any questions. I’ll reply you back.
TechcubeTalk Staff
This post/article/tutorial is written and published by TechCubeTalk.com staff (Blog editors, paid contributors or a guest contributors). TechCubeTalk.com reserves all rights and no part of this article can be published anywhere without prior written permission.
Latest posts by TechcubeTalk Staff (see all)
- Workshop: Developing a personal mini Photo Gallery application using Struts2, Hibernate and MySQL BLOB – Part 1 (developing admin panel) - May 12, 2013
- Step by step auto code generation for POJO domain java classes and hbm using Elipse Hibernate plugin - April 23, 2013
- Workshop on Creating a Personal MusicManager Web Application with Struts2, Hibernate and MySQL - April 22, 2013


Feb 10, 2009 @ 12:32:32
Nice example, I have found one more example here jsptube.com/examples/jsp-no-cache.html
Mar 08, 2009 @ 01:01:28
interesting article, got to know a lot of things
Jun 15, 2009 @ 17:38:43
i am using netbeans 6.5 ans IE 6.0. i tried for java solution but that is not working. even i tried for meta tag of jsp that is also not working. i have created bar chat using JFreeChart and saving as *.png… please suggest me what should i do?
Jun 16, 2009 @ 11:43:34
Great article, again. These informations are especially useful …