3.18. Defeating Browser Caching

Problem

You want to force the browser to display an up-to-date JSP page instead of showing the page from the browser's cache.

Solution

Set the nocache attribute to true for the controller element in your struts-config.xml file.

<controller nocache="true"/>

Discussion

To speed processing, browsers frequently keep a copy of a visited page on the client's local system. If an identical URL for the original page is requested and that page hasn't expired, the browser may display the page from the local cache instead of issuing a new request. This caching reduces network traffic and improves the user experience significantly. However, this can cause problems for dynamically generated pages. Consider a JSP page that renders data retrieved from the HTTP session. If data stored in the session changes, the browser won't be aware of the change. When the browser receives a new request for the page, it serves up the old page instead.

The easiest means of solving this problem for a Struts application is to configure the Struts RequestProcessor to generate a nocache header entry for every generated HTTP response. Set the nocache attribute to true on the controller element, as shown in the Solution. If the nocache attribute is not specified, the default value is false.

While this solves the problem, a consequence of its use is that every page accessed through a Struts Action results in a new request being sent to the server, even if the data haven't changed. One alternative ...

Get Jakarta Struts Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.