Hi Friends......
How do I prevent the output of my JSP or Servlet pages from being cached by the browser?
Thanks............
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Chintan RathodPosted Jan 2, 2012, 1:57 PM
setHeader(String name, String value)Sets a response header with the given name and value.
Cache-Control
The Cache-Control header can be used to expire content immediately or disable caching altogether. The value of this header determines whether cached pagelet/portlet content can be shared among different users.
The Cache-Control header can contain the following values:
In JSP, use the setHeader method to configure the Cache-Control header:
<%
response.setHeader("Cache-Control","public");
%>
The JSP example below expires the content immediately using the maximum age header.
<%
response.setHeader("Cache-Control","max-age=0");
%>
Following will not "store" or "cache"
<%
response.setHeader("Cache-Control","no-store");
%>
In .NET, the Cache-Control header is accessed through the System.Web.HttpCachePolicy class. To set the header to public, private or no-cache, use the Response.Cache.SetCacheability method.
Response.Cache.SetCacheability(HttpCacheability.Public);
Pragma:
The Pragma general-header field is used to include implementation- specific directives that might apply to any recipient along the request/response chain. All pragma directives specify optional behavior from the viewpoint of the protocol; however, some systems MAY require that behavior be consistent with the directives.
HTTP/1.1 caches SHOULD treat "Pragma: no-cache" as if the client had sent "Cache-Control: no-cache". No new Pragma directives will be defined in HTTP.2) response.setDateHeader ("Expires", long value);
Expires:
An application can also set the Expires header to enable caching, when the expiration date is a specific time instead of an interval. For heavily loaded pages, even setting short expires times can significantly improve performance. Sessions should be disabled for caching.
Vikas MishraPosted Jan 2, 2012, 4:04 PM
Satyapriya NayakPosted Jan 2, 2012, 12:02 PM
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
%>
Refer
http://www.jguru.com/faq/view.jsp?EID=377
Thanks