Prevent Client-Side Caching for Dynamic Pages
Pages that are very dynamic—for
instance, a page showing the result of a search based on user
input—must not be cached anywhere. As described in Chapter 3, there’s a set of headers you
can use to prevent caching of such a response. The problem for JSP
pages is the same as described in the previous section, but luckily,
the solution is the same as well: let the Controller set the headers
for the requests it handles; let JSP pages that are invoked directly
set their own headers; and for included pages, let the top-level page
set them. Building on the example of a solution for the layout page
scenario from the previous section, Example 9-10
shows a variable-setter page that adds caching headers to
HashMap collections created by the layout page.
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %> <c:set var="subTitle" scope="request" value="Page 1" /> <c:set target="${exp}" property="page1" value="0" /> <c:set target="headers" property="Cache-Control" value="no-store, no-cache, must-revalidate, post-check=0, pre-check=0" /> <c:set target="headers" property="Pragma" value="no-cache" />
The page in this example adds the value 0 to the
collection for expiration dates to ensure that the layout page sets
it to a date far in the past. Even if another variable-setter page
adds an expiration value for a date in the future, the value
0 is still used because it’s
lower. The