Home > Articles

The Servlet Container

This chapter is from the book

Terms you'll need to understand:

  • Context

  • Web app deployment descriptor

  • Initialization parameters

  • Context and attribute listener

  • Distributable

Techniques you'll need to master:

  • Retrieve servlet context-initialization parameters.

  • Use a servlet context listener.

  • Use a servlet context attribute listener.

  • Use a session attribute listener.

  • Distinguish the behavior of listeners in a distributable.

Context Architecture

You can see how Tomcat employs an architecture that implements Sun's specifications carefully. It is hard to understand context, though, if you don't know the overall architecture.

TIP

When you see the word application on the exam, think context. So, the application-initialization parameters are really those defined in the deployment descriptor with the context-param element and retrieved with ServletContext.getInitParameters.

A nice snapshot of the architecture is seen in Tomcat's primary configuration file server.xml (CATALINA_HOME\conf\server.xml). Other vendor containers also use configuration schemes, so if you aren't using Tomcat to study for the exam, look at your product's configuration files. The component elements shown in this chapter are nested corresponding to their parent-child relationships with each other. Descriptive comments are edits from comments in the sample server.xml file (ships with Tomcat). This file is not on the exam, but the architecture that Listing 3.1 defines helps give a big picture.

Listing 3.1 Sample Configuration Illustrates Architecture

<!--
A "Server" is a singleton element. It represents the
entire JVM. The server in turn may contain one or more "Service"
instances. The Server listens for a shutdown command 
on the indicated port. Notice: A "Server" is not 
itself a "Container".
 -->
<Server port="8005" shutdown="SHUTDOWN" debug="0">

<!-- 
A "Service" is a collection of one or more "Connectors"
that share a single "Container" (and therefore the web 
applications visible within that Container). Normally,
that Container is an "Engine". Notice: A "Service" is 
not itself a "Container".
-->

 <!-- Define the Tomcat Stand-Alone Service -->
 <Service name="Tomcat-Standalone">

<!-- A "Connector" represents an endpoint by which requests
are received and responses are returned. Each Connector 
passes requests on to the associated "Container" (normally
an Engine) for processing. By default, a non-SSL HTTP/1.1 
Connector is established on port 8080.

  <!-- non-SSL HTTP/1.1 Connector on port 8080 -->
  <Connector className=
   "org.apache.catalina.connector.http.HttpConnector"
   port="8080" minProcessors="5" maxProcessors="75"
   enableLookups="true" redirectPort="8443"
   acceptCount="10" debug="0" connectionTimeout="60000"/>

<!-- You can also define an SSL HTTP/1.1 Connector on port
8443, an AJP 1.3 Connector on port 8009, a Proxied HTTP/1.1 
Connector on port 8081, and non-SSL test connectors
on other ports such as 8082. -->


<!-- An Engine represents the entry point (within Catalina)
that processes every request. The Engine implementation 
for Tomcat stand alone analyzes the HTTP headers included 
with the request, and passes them on to the appropriate 
Host (virtual host). -->


   <!-- Define the default virtual host -->
   <Host name="localhost" debug="0" appBase="webapps"
    unpackWARs="true">


<!-- Logger shared by all Contexts related to this virtual 
host. By default (when using FileLogger), log files are 
created in the "logs" directory relative to $CATALINA_HOME.
If you wish, you can specify a different directory with the
"directory" attribute. Specify either a relative (to 
$CATALINA_HOME) or absolute path to the desired directory.
-->
    <Logger 
     className="org.apache.catalina.logger.FileLogger"
     directory="logs" prefix="localhost_log." 
     suffix=".txt" timestamp="true"/>

<!-- Define properties for each web application such as 
document roots in places other than the virtual host's 
appBase directory. -->

    <!-- Tomcat Examples Context -->
    <Context path="/examples" docBase="examples" debug="0"
         reloadable="true">
     <Logger className="org.apache.catalina.logger.FileLogger"
           prefix="localhost_examples_log." suffix=".txt"
    	 timestamp="true"/>
    </Context>
   </Host>
  </Engine>
 </Service>

TIP

The word context is really just a name that gets mapped to the document root of a Web application. For example, the context of the examples application is /examples. The request URL http://localhost:8080/examples/welcome.html retrieves the file welcome.html from CATALINA_HOME\webapps\examples\welcome.html.

Pearson IT Certification Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from Pearson IT Certification and its family of brands. I can unsubscribe at any time.