Servlet 2.3 API Features Exposedby Stephanie Fesler
"It's a bird, it's a plane; no, it's the Servlet 2.3 specification"
Sun Microsystems released the proposed final draft of the Servlet 2.3 specification on October 20, 2000. There have been many changes in the specification between the 2.2 final version and the 2.3 proposed final draft. Some of the changes include
- incorporating Javadoc API definitions into the specification,
- making the J2SE a requirement for the underlying platform for web containers,
- internationalization fixes, and
- incorporation of many of the Servlet 2.2 errata and other clarifications.
Along with the fixes and clarifications, the Servlet 2.3 proposed final draft specification adds new features -- for example, application lifecycle events and filtering -- to the Servlet arsenal.
Although the Servlet 2.3 specification is not yet in its final version, we do not have to wait any longer to play with the exciting new features defined by it. Apache's Tomcat has released a build, version 4.0 beta 1, that fully supports the Servlet 2.3 proposed final draft specification.
This article begins with an introduction to Servlet concepts (for those new to this area) and then takes an overview of each of the new features, the application lifecycle events, and Servlet filters.
Introduction to Servlet Concepts
The following are terms used throughout this article and are offered to the new arrival on the Servlet scene.
Servlet -- A Servlet is a Java class that resides on a Web server that accepts requests and generates responses. They can accept requests and generate responses over different communication protocols, but the most common type of Servlet is an HTTP Servlet, which is implemented by the
javax.servlet.http.HttpServletJava class. The HTTP Servlet accepts HTTP requests and generates HTTP responses. Since a Servlet is a server resource, it has access to other server resources: other Servlets, EJBs, JSPs, and databases. The purpose of a Servlet is to generate a dynamic response.
Servlet container -- The Servlet container is the environment within which Servlets execute, whether built into the Web server, an add-on component to the Web server, or built into an application server. The Servlet container is responsible for managing the lifecycle of Servlets, providing network services over which the requests and responses are sent, and decoding and formatting MIME type requests and responses.
Web Application -- Web Applications were first defined in the Servlet 2.2 specification. They are a collection of Servlets, JSPs, static documents, and other utility classes that make up an application. Web Applications can be deployed as a directory structure or as a single Web archive (
.war) file on any Servlet compatible Web server or application server.
Session -- A session is a series of requests from the same user or client. It is important to be able to associate requests from the same user so that you can maintain information on behalf of the client. Since HTTP is a stateless protocol it's hard to track client requests and maintain stateful information about them. However, the Servlet specification has defined an interface,
javax.servlet.http.HttSession, that programmers can use to easily track client requests.
Servlet Context -- The Servlet context defines a Servlet's vision within the Web Application the Servlet is executing in. The container is responsible for instantiating one instance of the
javax.servlet.ServletContextinterface for each Web Application. Therefore, programmers can use the
ServletContextobject to make resources available to all Servlets within a Web Application.
Since a Servlet is a Java class, it gets compiled to platform
independent bytecode in the
.class file. After the
Servlet is deployed and before any requests are made to it, the
container is responsible for instantiating a Servlet object and
initializing the Servlet. Once the Servlet has been initialized, it is
ready to service client requests. At some point, the container can
destroy the Servlet object and calls the destroy method on that
Servlet object, freeing it up for garbage collection.
Overview of Application Lifecycle Events
The Servlet 2.3 proposed final draft specification defines
application lifecycle events to provide Web Application developers
more interaction with the
ServletContext object and
HttpSession objects. Web Application developers write
event listeners so they can now be notified when lifecycle events
happen (such as creation or destruction) or when attributes are
modified in the
ServletContext object or
Event listeners are Java classes that follow the JavaBeans design
and are provided by the Web Application developer in the Web archive
.war) file. There are two types of event listeners, and
both types apply to the
ServletContext object and
HttpSession objects. The two types are lifecycle events
and changes to attributes events. Table 1 shows the types of events, a
brief description, and the listener interface to implement. This table
is taken from the Servlet 2.3 proposed final draft specification.
|Table 1. Supported Event Types|
|Event Type||Description||Listener Interface|
|Servlet Context Events|
The Servlet context has just been created and is available to service its first request, or the Servlet context is about to be shutdown.
|Changes to Attributes||
Attributes on the Servlet context has been added, removed, or replaced.
|Http Session Events|
An HttpSession has just been created, or has been invalidated or timed out.
|Changes to Attributes||
Attributes have been added, removed or replaced in an HttpSession object.
An interesting point is that there can be multiple listener classes listening to each event type and the Web application developer has the flexibility to define the order the event listener objects are invoked.
Pages: 1, 2