6

In my web application there is some problem due to browser cache.

How to clear the browser cache when loading my jsp page?

How can i include clear cache code in jsp page?

5 Answers 5

17

You can do this way in your JSP to prevent from caching,not able to clear the cache programatically:

 <% response.setHeader("Cache-Control","no-cache"); //HTTP 1.1 
 response.setHeader("Pragma","no-cache"); //HTTP 1.0 
 response.setDateHeader ("Expires", 0); //prevents caching at the proxy server  
%>
5
  • 1
    add the above code snippet in a jsp file and include that jsp in all other jsps. Or another easy way is to add this code while sending the response back in a filter so that you can makes sure that the Browser is not going to cache the data.
    – UVM
    Commented Oct 9, 2012 at 4:53
  • 1
    I tried with the following code, its is not caching the page but when I hit browser back button, browser says this document no more exist , reload the page. Why it is not reloading the page automatically ? <% response.setHeader("Cache-Control", "no-cache"); response.setHeader("Cache-Control", "no-store"); response.setHeader("Pragma", "no-cache"); response.setDateHeader("Expires", 0); %>
    – Sadanand
    Commented Apr 30, 2014 at 9:08
  • Do we need to use this with request.setHeader or response.setHeader in jsp page? I also required to disable caching in my web application
    – srinivas
    Commented Aug 24, 2015 at 13:00
  • @Sadanand, did you find the answer to your question? If not, try asking as a separate question here.
    – Barett
    Commented Sep 21, 2016 at 17:01
  • @Barett no I haven't .
    – Sadanand
    Commented Sep 23, 2016 at 5:36
2
 <%
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Cache-Control", "no-store");
response.setHeader("Pragma", "no-cache");
response.setDateHeader("Expires", 0);
 %>

work perfect in jsp and remember to check session

<%

User u = (User) request.getSession().getAttribute("user");
if (u != null ) {
//show page
}else{
// redirect to login page
}

%>
0
<%
response.addHeader("Cache-Control", "no-cache, no-store, must-revalidate");
response.addHeader("Pragma", "no-cache");
response.setDateHeader("Expires", 0);
%>

This worked for me. Reference

0

How to disable browser caching for a specific JSP? It is possible to keep the browser from caching a JSP page response. The following hints added to the response header seem to prevent most modern browsers from pulling pages out of cache when the same URL is "hit":

<%
   response.setHeader( "Pragma", "no-cache" );
   response.setHeader( "Cache-Control", "no-cache" );
   response.setDateHeader( "Expires", 0 );
%>

The same effect can be achieved by using meta tags in the HTML header:

<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Cache-Control" content="no-cache">
<meta http-equiv="Expires" content="Sat, 01 Dec 2001 00:00:00 GMT">
-1

The same effect can be achieved by using meta tags in the HTML header:

<meta http-equiv="Pragma" content="no-cache"> 
 <meta http-equiv="Cache-Control"      content="no-cache"> 
<meta http-equiv="Expires" content="Sat, 01 Dec 2012 00:00:00 GMT">

The Cache-Control header was added in HTTP 1.1, while the other two were also present in HTTP 1.0.

1
  • Not recommended, unless you have no other choice. (Not using JSP, or can't modify the response headers at the time your JSP is running -- perhaps due to an inflexible framework you're using.)
    – Barett
    Commented Sep 21, 2016 at 16:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.