type=page
status=published
title=Servlet Lifecycle
next=servlets003.html
prev=servlets001.html
~~~~~~
= Servlet Lifecycle


[[BNAFI]]

[[servlet-lifecycle]]
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.

1.  If an instance of the servlet does not exist, the web container:
1.  Loads the servlet class
2.  Creates an instance of the servlet class
3.  Initializes the servlet instance by calling the `init` method
(initialization is covered in link:servlets004.html#BNAFU[Creating and
Initializing a Servlet])
2.  The container invokes the `service` method, passing request and
response objects. Service methods are discussed in
link:servlets005.html#BNAFV[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
link:servlets010.html#BNAGS[Finalizing a Servlet].

[[BNAFJ]]

[[handling-servlet-lifecycle-events]]
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.

[[BNAFK]]

[[defining-the-listener-class]]
Defining the Listener Class
^^^^^^^^^^^^^^^^^^^^^^^^^^^

You define a listener class as an implementation of a listener
interface. link:#BNAFL[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`.

[[sthref97]][[BNAFL]]

*Table 18-1 Servlet Lifecycle Events*

[width="90%",cols="15%,30%,45%"]
|=======================================================================
|*Object* |*Event* |*Listener Interface and Event Class*
|Web context |Initialization and destruction
|`javax.servlet.ServletContextListener` and `ServletContextEvent`

|Web context |Attribute added, removed, or replaced
|`javax.servlet.ServletContextAttributeListener` and
`ServletContextAttributeEvent`

|Session |Creation, invalidation, activation, passivation, and timeout
|`javax.servlet.http.HttpSessionListener`,
`javax.servlet.http.HttpSessionActivationListener`, and
`HttpSessionEvent`

|Session |Attribute added, removed, or replaced
|`javax.servlet.http.HttpSessionAttributeListener` and
`HttpSessionBindingEvent`

|Request |A servlet request has started being processed by web
components |`javax.servlet.ServletRequestListener` and
`ServletRequestEvent`

|Request |Attribute added, removed, or replaced
|`javax.servlet.ServletRequestAttributeListener` and
`ServletRequestAttributeEvent`
|=======================================================================


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:

[source,oac_no_warn]
----
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:

[source,oac_no_warn]
----
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

@WebListener()
public class SimpleServletListener implements ServletContextListener,
        ServletContextAttributeListener {
    ...
----

[[BNAFN]]

[[handling-servlet-errors]]
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:

[source,oac_no_warn]
----
A Servlet Exception Has Occurred
----

But you can also specify that the container should return a specific
error page for a given exception.
