jump to navigation

Application Lifecycle Events August 16, 2005

Posted by Coolguy in Servlets/Jsp, Web Applications.
  • The application events facility gives the Web Application Developer greater control over the lifecycle of the ServletContext and HttpSession and ServletRequest , allows for better code factorization, and increases efficiency in managing the resources that the Web application uses.
  • Application event listeners are classes that implement one or more of the servlet event listener interfaces.
  • They are instantiated and registered in the Web container at
    the time of the deployment of the Web application. They are provided by the Developer in the WAR.
  • Servlet event listeners support event notifications for state changes in the
    ServletContext , HttpSession and ServletRequest objects.
  • Servlet context listeners are used to manage resources or state held at a JVM level for the application.
  • HTTP session listeners are used to manage state or resources associated with a series of requests made into a Web application from the same
    client or user.
  • Servlet request listeners are used to manage state across the lifecycle of servlet requests.
  • 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 shut down. javax.servlet.ServletContextListener
    Changes to attributes

    Attributes on the servlet context have beenadded, removed, or replaced. javax.servlet.ServletContextAttributeListener
    HTTP Session Events

    An HttpSession has been created, invalidated,or timed out. javax.servlet.http.HttpSessionListener
    Changes to attributes

    Attributes have been added, removed, or replaced on an HttpSession . javax.servlet.HttpSessionAttributeListener
    Session migration

    HttpSession has been activated or passivated. javax.servlet.HttpSessionActivationListener
    Object binding

    Object has been bound to or unbound from Httpsession javax.servlet.HttpSessionBindingListener

Servlet Request Events

A servlet request has started being processed by Web components.


Changes to attributes

Attributes have been added, removed, orreplaced on an ServletRequest .. javax.servlet.ServletRequestAttributeListener

    An Example of Listener Use:

  1. When the application starts up, the listener class is notified. The application logs on to the database, and stores the connection in the servlet context.
  2. Servlets in the application access the connection as needed during activity in the Web application.
  3. When the Web server is shut down, or the application is removed from the Web server, the listener class is notified and the database connection is closed.

Listener Class Configuration

  • The Developer of the Web application provides listener classes implementing one or more of the listener classes in the javax.servlet API.
  • Each listener class must have a public constructor taking no arguments.
  • Listener classes are declared in the Web application deployment descriptor using the listener element. They are listed by class name in the order in which they are to be invoked.
  • The Web container creates an instance of each listener class and registers it for event notifications prior to the processing of the first request by the application.
  • During Web application execution, listeners are invoked in the order of their registration.
  • On application shutdown, listeners are notified in reverse order to their declarations with notifications to session listeners preceeding notifications to context listeners. Session listeners must be notified of session invalidations prior to context listeners being notified of application shutdown.
  • The following example is the deployment grammar for registering two servlet context lifecycle listeners and an HttpSession listener.
    Suppose that com.acme.MyConnectionManager and com.acme.
    MyLoggingModule both implement javax.servlet.ServletContextListener , and that com.acme.MyLoggingModule additionally implements
    javax.servlet.HttpSessionListener. Also, the Developer wants
    com.acme.MyConnectionManager to be notified of servlet context lifecycle events before com.acme.MyLoggingModule. Here is the deployment descriptor for this application:














  • The container is required to complete instantiation of the listener classes in a Web application prior to the start of execution of the first request into the application.
  • The container must maintain a reference to each listener instance until the last request is serviced for the Web application.
  • Application code inside a listener may throw an exception during operation.
  • The container must allow unhandled exceptions to be handled by the error page mechanism
  • In distributed Web containers, HttpSession instances are scoped to the particular JVM servicing session requests, and the ServletContext object is scoped to the Web container’s JVM.
  • Listener classes provide the Developer with a way of tracking sessions within a Web application. It is often useful in tracking sessions to know whether a session became invalid because the container timed out the session, or because a Web component within the application called the invalidate method.


No comments yet — be the first.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: