首页 > 代码库 > JavaEE学习之路-Servlet Lifecycle
JavaEE学习之路-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
init
method (initialization is covered inCreating and Initializing a Servlet)
The container invokes the
service
method, passing request and response objects. Service methods are discussed inWriting Service Methods.
If it needs to remove the servlet, the container finalizes the servlet by calling the servlet‘sdestroy
method. For more information, see Finalizing a Servlet.
17.2.1 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.
17.2.1.1 Defining the Listener Class
You define a listener class as an implementation of a listener interface.Table 17-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 theHttpSessionListener
interface are passed an HttpSessionEvent
, which contains anHttpSession
.
Table 17-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 { ...
17.2.2 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.
JavaEE学习之路-Servlet Lifecycle