Using the Jakarta Response Tag Library
So, how do we go about turning off caching of our pages? Essentially, by setting various HTTP response
headers. Whilst browsers and caches vary in their interpretation of the headers, in general almost all
browsers and proxies will respond to setting the following three headers:
-
Cache-Control: no-cache
-
Expires: <a date in the past>
-
Pragma: No-cache
The Pragma: No-cache header is part of the HTTP 1.0 specification, while the Cache-Control: no-
cache is part of the HTTP 1.1 protocol. HTTP 1.1 clients ought to interpret Pragma: No-cache as
being equivalent to Cache-Control: no-cache; unfortunately, IE5 doesn't do this. The Expires:
header is used to give a time after which the content should be discarded. Any one of these headers
ought to do the trick. However, for maximum compatibility all three should be set.
So, how to set the headers? The most obvious way is to use the response object's setHeader() and
setDateHeader() methods, but perhaps a more JSP-friendly approach is to use a tag library. The
Jakarta Response Tag Library (freely available from http://jakarta.apache.org/taglibs/) does just that,
and so we can put together a simple JSP fragment, nocache.jspf, that does this:
<%@ taglib uri="http://jakarta.apache.org/taglibs/response-1.0"
prefix="response" %>
<response:setHeader name="Cache-Control">no-cache</response:setHeader>
<response:setDateHeader name="Expires">0</response:setDateHeader>
<response:setHeader name="Pragma">No-cache</response:setHeader>
The .jspf file extension is recommended in the JSP 1.2 specification for fragments of JSP code
that will be incorporated using a static include.
We can simple use include this fragment within our main JSP page, nocache-jakartatags.jsp, to
prevent caching of the page:
<%@ include file="nocache.jspf" %>
<% application.log("This is nocache-jakartatags.jsp"); %>
<html>
<head>
<title>Caching not allowed! (Using Jakarta tags)</title>
</head>
<body>
<h1>No caching! (Using Jakarta tags)</h1>
<p>The current date and time: <%= new java.util.Date() %></p>
</body>
</html>
Finally, we need to make the web container aware of the Response Tag Library:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_3.dtd">
<web-app>
<taglib>
<taglib-uri>http://jakarta.apache.org/taglibs/response-1.0</taglib-uri>
<taglib-location>/WEB-INF/response.tld</taglib-location>
</taglib>
</web-app>
As the log file shown below indicates, Internet Explorer does not cache this version of the page but
requests a fresh copy each time. The time when the page was fetched from the server is now different in
each window:
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.