Consider Enabling Client-Side Caching for Semistatic Pages

Chapter 3 describes how you can set the Last-Modified and Expires headers to allow clients and proxies to cache a response. In an enterprise application, most JSP pages are invoked through a Controller servlet, so the servlet can set these headers for appropriate client-side caching. You must decide which values to use on a case-by-case basis—for instance, using timestamps associated with all data managed by the application, combined with input received with the request.

The Controller can’t be in charge of the headers for all JSP pages, though. Some JSP page requests don’t go through the Controller at all—for instance, a request for a company info page implemented as a JSP page just to get the common look and feel. The Controller might also not be aware of caching details for nonessential parts of the pages, such as the “news flash” section mentioned earlier. In cases such as this, the JSP page must manage the headers by itself to enable caching.

It’s hard to use the Last-Modified header for JSP page caching because the JSP specification does not include a mechanism for asking the JSP page when it was last modified (i.e., there’s no standard way to override the getLastModified( ) method in a JSP page). The Expires header can be put to good use, though. You can use a scriptlet or a custom action that sets this header just as you would in a servlet.

What you need to be aware of is that headers cannot be set by a web ...

Get Java Enterprise Best Practices now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.