How to prevent a JSP/Sevlet output page from being cached by the browser?

Posted on Jan 16, 2009 | 4 Comments

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″;, 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.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.

Suvoraj Biswas

A seasoned Technical Consultant with 9+ years of hands on experiences in Business Analysis, Enterprise Technology Integration and core development. He holds the globally-recognized PMP® credential and is an active member of the Project Management Institute, Inc. Raj has proven consistent track record of success in crucial and technically challenging enterprise projects involving system development, system integration, IT migration and mobile solutions where he maintained a strong balance between client's business objectives and technical development in Offshore-Onshore model framework and contributed as and when needed as Technical Consultant, Lead, Business Analyst and Project Expediter. He holds technical expertise in middle-ware Solution Architecture, Web Service (SOAP & RESTful), Android Mobile development and Enterprise Web development with Spring Core & MVC, Struts2, Hibernate. Besides work, he loves doing sketches and listening to music and contribute articles at &


  1. sudhir says:

    Nice example, I have found one more example here

  2. Faubrinia says:

    interesting article, got to know a lot of things

  3. Anuj says:

    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?

  4. wow power leveling says:

    Great article, again. These informations are especially useful …

Leave a comment

1 × = six



You can subscribe by e-mail to receive news updates and breaking stories.