Tuesday, September 20, 2005

SERVLET LIFE CYCLE LISTENERS AND EVENTS

The Java Servlet 2.4 specification provides mechanisms for notifying application code about events in the Web application life cycle. Servlet life cycle events include creation and destruction of a servlet context or an HTTP session, and creation, modification, or removal of servlet context attributes and HTTP session attributes. Furthermore, any object may be notified when it is bound to or unbound from an HTTP session attribute.

Application life cycle listeners are often used to provide consistent, managed access to other resources. For example, consider a set of servlets within the same Web application that share a database connection on a single server. To improve performance, some servlets save a database connection as a servlet context attribute, and reuse it across multiple servlet service invocations for multiple clients. When the application is shut down, the database connection should be closed. To manage the database connection, an application developer creates a servlet context event listener. The listener class opens the database and initializes a context attribute with a reference when the application is deployed. The listener class also closes the database when the application is undeployed.

To create a life cycle listener, an application developer writes classes that implement listener interfaces for life cycle events, and the deployer(web.xml) configures these classes to be used by a Web application. The class files for the listeners are configured using the deployment descriptor web.xml. Each Web application can have multiple listeners of each type.

The interfaces provided for listening to Web application life cycle events are:

  • javax.servlet.ServletContextListener. The methods in this interface are called when a new servlet context(application) is created or just before the servlet context is destroyed. All ServletContextListeners are notified of context initialization before any filter or servlet in the web application is initialized. All servlets have been destroy()ed before any ServletContextListeners are notified of context destruction.
  • javax.servlet.http.HttpSessionListener. The methods in this interface are called when an HTTP session is created or destroyed. The call to the method occurs before the session is invalidated.
  • javax.servlet.http.HttpRequestListener. The methods in this interface are called when a request is made to a servlet in the web application.
  • javax.servlet.ServletContextAttributeListener. The methods in this interface are called when a servlet context attribute is created, modified, or removed from a servlet context.
  • javax.servlet.http.HttpSessionAttributeListener. The methods in this interface are called when a session attribute is created, modified, or removed from an HTTP session.
  • javax.servlet.ServletRequestAttributeListener. The methods in this interface are called when a servlet request attribute is created, modified, or removed from a servlet context.
  • javax.servlet.http.HttpSessionBindingListener. The methods in this interface are called when an object of an attribute class is bound or unbound from an HTTP session.
  • javax.servlet.http.HttpSessionActivationListener. The methods in this interface are called when an object of an attribute class has to be notified when the session to which they’re bound is migrated to and from another session.

NOTE: To receive notification of Life Cycle Events, the implementation class must be configured in the deployment descriptor for the web application, barring(except) HttpSessionBindingListener and HttpSessionActivationListener.

Configuring Listener Classes

At deploy time, listener classes are declared using the listener tag in the deployment descriptor file, web.xml. The Web container is responsible for creating and providing callbacks to instances of the listener classes.

web-app

display-name JunWebApp /display-name

description

Demonstrate resource listing and life cycle listeners

/description

listener

listener-class

com.edu.LifeCycleServletContextListener

/listener-class

/listener

listener

listener-class

com.edu.LifeCycleHttpSessionListener

/listener-class

/listener

...


Scope

Event

Listener Interface and Event Class

Servlet Context

contextInitialized()

contextDestroyed()

ServletContextListener, ServletContextEvent

attributeAdded()

attributeRemoved()

attributeReplaced()

ServletContextAttributeListener, ServletContextAttributeEvent

HTTP Session

sessionCreated()

sessionDestroyed()

HttpSessionListener, HttpSessionEvent

sessionDidActivate()

sessionWillPassivate()

HttpSessionActivationListener, HttpSessionEvent

attributeAdded()

attributeRemoved()

attributeReplaced()

HttpSessionAttributeListener, HttpSessionBindingEvent (note the class name !)

valueBound()

valueUnbound()

HttpSessionBindingListener (note, interface must be implemented by attribute class !), HttpSessionBindingEvent

Servlet Request

requestInitialized() requestDestroyed

ServletRequestListener, ServletRequestEvent

attributeAdded()

attributeRemoved()

attributeReplaced()

ServletRequestAttributeListener, ServletRequestAttributeEvent

0 Comments:

Post a Comment

<< Home