Java Reference
In-Depth Information
Servlet Initialization and Persistence:
A Counter Servlet
Example 18-2 is a listing of Counter.java , a servlet that maintains any number of
named counters. Each time the value of one of these counters is requested, the
servlet increments the counter and outputs its new value. The servlet is suitable for
use as a simple hit counter for multiple web pages but can also count any other
type of event.
This servlet defines init() and destroy() methods and saves its state to a file, so
it does not lose count when the web server (or servlet container) shuts down. To
understand init() and destroy() , you have to understand something about the
servlet life cycle. Servlet instances are not usually created anew for each client
request. Instead, once a servlet is created, it can serve many requests before it is
destroyed. A servlet such as Counter is typically not shut down unless the servlet
container itself is shutting down, or the servlet is inactive and the container is try-
ing to free up memory to make room for other servlets.
The init() method is invoked when the servlet container first instantiates the
servlet, before any requests are serviced. The first thing this method does is look
up the value of two initialization parameters : the filename of the file that contains
the saved state and an integer value that specifies how often to save the state back
into that file. Once the init() method has read these parameters, it reads the
counts (using object serialization) from the specified file and is ready to begin
serving requests. The values of the initialization parameters come from the WEB-
INF/web.xml deployment file. Before running this example, you probably need to
to edit that file to tell the servlet where to save its state. Look for these lines:
<init-param>
<param-name>countfile</param-name> <!-- where to save state -->
<param-value>/tmp/counts.ser</param-value> <!-- adjust for your system-->
</init-param>
If the filename /tmp/counts.ser does not make sense on your system, replace it
with a filename that does.
The destroy() method is the companion to init() ; it is invoked after the servlet
has been taken out of service and there are no more requests being processed by
the servlet. The Counter servlet uses this method to save its state, so it can be cor-
rectly restored when the servlet container starts the servlet up again. Note, how-
ever, that the destroy() method is invoked only when the servlet is shut down in
a controlled way. In the case of a server crash or power outage, for example, there
is no opportunity to save state. Thus, the Counter servlet also periodically saves it
state from the doGet() method, so it never loses more than a small amount of
data.
The doGet() method must first determine the name of the counter whose value is
to be displayed. Since the Counter servlet is designed for use in a variety of ways,
doGet() uses three techniques for obtaining the counter name. First, it checks for a
parameter sent with the HTTP request. Next, it checks for a request attribute,
which is a named value associated with the request by the servlet container or by
another servlet that has invoked the Counter servlet. (You'll see an example of this
Search WWH ::




Custom Search