Servlet Lifecycle
The lifecycle of a servlet is controlled by the container in which the servlet has been deployed. When a request is mapped to a servlet, the container performs the following steps.
-
If an instance of the servlet does not exist, the web container:
-
Loads the servlet class
-
Creates an instance of the servlet class
-
Initializes the servlet instance by calling the
initmethod (initialization is covered in Creating and Initializing a Servlet) -
The container invokes the
servicemethod, passing request and response objects. Service methods are discussed in Writing Service Methods.
If it needs to remove the servlet, the container finalizes the servlet
by calling the servlet’s destroy method. For more information, see
Finalizing a Servlet.
Handling Servlet Lifecycle Events
You can monitor and react to events in a servlet’s lifecycle by defining listener objects whose methods get invoked when lifecycle events occur. To use these listener objects, you must define and specify the listener class.
Defining the Listener Class
You define a listener class as an implementation of a listener
interface. Table 18-1 lists the events that can be
monitored and the corresponding interface that must be implemented. When
a listener method is invoked, it is passed an event that contains
information appropriate to the event. For example, the methods in the
HttpSessionListener interface are passed an HttpSessionEvent, which
contains an HttpSession.
Table 18-1 Servlet Lifecycle Events
Object |
Event |
Listener Interface and Event Class |
Web context |
Initialization and destruction |
|
Web context |
Attribute added, removed, or replaced |
|
Session |
Creation, invalidation, activation, passivation, and timeout |
|
Session |
Attribute added, removed, or replaced |
|
Request |
A servlet request has started being processed by web components |
|
Request |
Attribute added, removed, or replaced |
|
Use the @WebListener annotation to define a listener to get events for
various operations on the particular web application context. Classes
annotated with @WebListener must implement one of the following
interfaces:
javax.servlet.ServletContextListener
javax.servlet.ServletContextAttributeListener
javax.servlet.ServletRequestListener
javax.servlet.ServletRequestAttributeListener
javax.servlet..http.HttpSessionListener
javax.servlet..http.HttpSessionAttributeListener
For example, the following code snippet defines a listener that implements two of these interfaces:
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
@WebListener()
public class SimpleServletListener implements ServletContextListener,
ServletContextAttributeListener {
...
Handling Servlet Errors
Any number of exceptions can occur when a servlet executes. When an exception occurs, the web container generates a default page containing the following message:
A Servlet Exception Has Occurred
But you can also specify that the container should return a specific error page for a given exception.