the CGI programs were not platform-independent. Therefore, other techniques were
introduced. Among these are servlets.
Servlets offer several advantages in comparison with CGI. First, performance is significantly
better. Servlets execute within the address space of a web server. It is not necessary to create
a separate process to handle each client request. Second, servlets are platform-independent
because they are written in Java. Third, the Java security manager on the server enforces a
set of restrictions to protect the resources on a server machine. Finally, the full functionality
of the Java class libraries is available to a servlet. It can communicate with applets, databases,
or other software via the sockets and RMI mechanisms that you have seen already.
The Life Cycle of a Ser vlet
Three methods are central to the life cycle of a servlet. These are init( ), service( ), and destroy( ).
They are implemented by every servlet and are invoked at specific times by the server. Let
us consider a typical user scenario to understand when these methods are called.
First, assume that a user enters a Uniform Resource Locator (URL) to a web browser.
The browser then generates an HTTP request for this URL. This request is then sent to the
Second, this HTTP request is received by the web server. The server maps this request to
a particular servlet. The servlet is dynamically retrieved and loaded into the address space
of the server.
Third, the server invokes the init( ) method of the servlet. This method is invoked only
when the servlet is first loaded into memory. It is possible to pass initialization parameters
to the servlet so it may configure itself.
Fourth, the server invokes the service( ) method of the servlet. This method is called to
process the HTTP request. You will see that it is possible for the servlet to read data that has
been provided in the HTTP request. It may also formulate an HTTP response for the client.
The servlet remains in the server 's address space and is available to process any other
HTTP requests received from clients. The service( ) method is called for each HTTP request.
Finally, the server may decide to unload the servlet from its memory. The algorithms by
which this determination is made are specific to each server. The server calls the destroy( )
method to relinquish any resources such as file handles that are allocated for the servlet.
Important data may be saved to a persistent store. The memory allocated for the servlet and
its objects can then be garbage collected.
Using Tomcat for Ser vlet Development
To create servlets, you will need access to a servlet development environment. The one used
by this chapter is Tomcat. Tomcat is an open-source product maintained by the Jakarta Project
of the Apache Software Foundation. It contains the class libraries, documentation, and run-
time support that you will need to create and test servlets. At the time of this writing, the
current version is 5.5.17, which supports servlet specification 2.4. You can download Tomcat
The examples in this chapter assume a Windows environment. The default location for
Tomcat 5.5.17 is
C:\Program Files\Apache Software Foundation\Tomcat 5.5\
Search WWH :