Caches and Browsers
When it comes to creating web applications, we can't avoid the browser. We can write all the JSP pages,
servlets, filters, and tag libraries we like but their output still has to be understandable to the browser,
and a lot can happen between the web container and the end user. In this chapter we'll look at a couple
of the particular issues that arise with this:
-
Managing the browser cache, and any proxy caches that content may pass through on the way
to the user. We'll also see how we can implement caching within the web container.
-
Finding out about the browser that is being used and its capabilities, to allow us to make use
of features supported by advanced browsers (such as JavaScript or XML) whilst retaining
compatibility with older ones.
Along the way we'll make quite a lot of use of filters and tag libraries, as these make it much easier to
provide advanced features whilst keeping life easy for the page designer.
Managing Caches
Often, when an operation is expensive, we choose to cache its result to avoid incurring the expense
repeatedly. For example, if a calculation is computationally expensive but its result remains valid for a
significant period of time we may choose to recalculate only infrequently. Similarly, if a network
operation is costly (for example, if we are aggregating and sorting stock quotes from a different site), we
could cache the results so that they are recalculated only every 10 minutes.
There are three situations where content from our web sites might be cached:
-
By the client (the browser)
-
By the server (by the server-side code within our web application)
-
By something in between (such as a proxy server)
The only one of these three we can reliably control is the caching on the server. HTTP headers can indicate
to the client and to proxies whether the content should be cached, but precisely how this works varies
between implementations. Browsers such as Internet Explorer also provide a variety of possible users
settings to control this (under Tools | Internet Options | General | Temporary Internet Files | Settings):
Sometimes we don't want our content to be cached for example, if it is frequently changing or if it
depends on the user's session state. In the next chapter, for instance, we will be creating a photo gallery
site where the result presented by the view pages will depend on the user's session data, without any
difference in the URL to indicate this. We will therefore want to disable browser caching to ensure that
the view seen by the user is always accurate.
It's instructive to explore the different ways we can implement this functionality in a Java-based web
application, and to do this we'll look at:
-
A simple page that can be cached freely by the client or any proxy
-
How to prevent the caching of the page, by using a ready-made tag library
-
How to prevent the caching of the page, using a custom-build tag library
-
How to create a servlet filter that can manage the caching of a page, without any change to the
page itself
A Simple Cached Page
As a baseline case, let's write a very simple JSP page, cache.jsp, that makes no special provision
regarding caching and displays the current date and time. We arrange for it to make an entry in the
server log file each time it is accessed:
<% application.log("This is cache.jsp"); %>
<html>
<head>
<title>Caching allowed!</title>
</head>
<body>
<h1>Caching allowed!</h1>
<p>
The current date and time: <%= new java.util.Date() %>
</p>
</body>
</html>
On Tomcat 4, by default this log message will end up in %CATALINA_HOME%/logs/, in a file
whose name incorporates the current date, for example, localhost_log.2001-09-17.txt.
This JSP page is extremely simple, but is sufficient to demonstrate whether or not a particular page is
cached. We'll see some more concrete examples of the techniques shown here in the next chapter.
As we can see from the log, cache.jsp is only accessed once, even when we create a number of
browser windows pointing to it. Each window shows the same date and time:
That's often a good thing, but we may want to turn this functionality off.
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.