The lifecycle of a servlet is controlled by the container in which the servlet has been deployed. When a request was mapped to a servlet, the container performs the following steps.
If an instance of the Servlets 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 Serv Let
The container invokes service
the method, passing request and response objects. Service methods is discussed inwriting 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.
17.2.1Handling Servlet Lifecycle Events
You can monitor and react the events in a servlet's lifecycle by defining listener objects whose methods get invoked when L Ifecycle events occur. To use these listener objects, you must define and specify the listener class.
17.2.1.1Defining the Listener Class
You define a listener class as an implementation of a listener interface. Table 17-1 lists the events that can is monitored and the corresponding interface that must be implemented. When a listener method is invoked, it's passed an event that contains information appropriate to the event. For example, the methods in the HttpSessionListener
interface is passed an HttpSessionEvent
, and which contains an HttpSession
.
Table 17-1 Servlet Lifecycle Events
Object |
Event |
Listener Interface and Event Class |
Web context |
Initialization and destruction |
javax.servlet.ServletContextListener andServletContextEvent
|
Web context |
Attribute added, removed, or replaced |
javax.servlet.ServletContextAttributeListener andServletContextAttributeEvent
|
Session |
Creation, invalidation, activation, passivation, and timeout |
javax.servlet.http.HttpSessionListener , javax.servlet.http.HttpSessionActivationListener , andHttpSessionEvent
|
Session |
Attribute added, removed, or replaced |
javax.servlet.http.HttpSessionAttributeListener andHttpSessionBindingEvent
|
Request |
A servlet request has started being processed by Web Components |
javax.servlet.ServletRequestListener andServletRequestEvent
|
Request |
Attribute added, removed, or replaced |
javax.servlet.ServletRequestAttributeListener andServletRequestAttributeEvent
|
Use the @WebListener
annotation to define a listener to get events for various operations on the particular Web application conte Xt. Classes annotated with @WebListener
must implement one of the following interfaces:
Javax.servlet.ServletContextListenerjavax.servlet.ServletContextAttributeListenerjavax.servlet.ServletRequestListenerjava X.servlet.servletrequestattributelistenerjavax.servlet. http. Httpsessionlistenerjavax.servlet. http. Httpsessionattributelistener
For example, the following code snippet defines a listener that implements, the these interfaces:
Import Javax.servlet.servletcontextattributelistener;import Javax.servlet.servletcontextlistener;import Javax.servlet.annotation.WebListener, @WebListener () public class Simpleservletlistener implements Servletcontextlistener, servletcontextattributelistener { ...
17.2.2Handling 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 can also specify so the container should return a specific error page for a given exception.
Java EE learning path-servlet Lifecycle