|
|
Re: Cache Control in servlets and jsp
|
Posted: Oct 29, 2003 11:29 AM
|
|
To prevent Web browsers and proxy servers from caching dynamically generated Web pages (meaning dynamic output that results from processing JSP files, SHTML files, and servlets), use the following code to set headers in the HTTP Response:
<% response.setHeader("Pragma", "No-cache"); response.setDateHeader("Expires", 0); response.setHeader("Cache-Control", "no-cache"); %>
Setting the HTTP headers is a more effective method of controlling browser caching than using the <META> tag equivalents. For example, <META HTTP-EQUIV="Pragma" CONTENT="No-cache">
is the equivalent of the first HTTP header setting. Setting the HTTP headers is the recommended method, because:
Some browsers do not treat the <META> tags in the same way as the equivalent HTTP header settings. On some browsers, the <META> tag equivalents do not work when the callPage() method is used to load a JSP file that contains the <META> tags. There may be instances in when you want to permit a page to be cached, but you do not want the proxy server to permit multiple users to have access to the cached page. For example, suppose your servlet does not use session tracking and it generates a Web page that contains user input. To maintain that personalization, you would want to prevent other users from having access to the cached page. To prevent the proxy server from sharing cached pages, use the following code:
<% response.setHeader("Cache-Control", "private"); %> This header can be combined with the three recommended headers for preventing caching
|
|