Describe the Web container life cycle event model for requests, sessions, and web applications; create and configure listener classes for each scope life cycle; create and configure scope attribute listener classes; and given a scenario, identify the proper attribute listener to use.

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.

There may be multiple listener classes listening to each event type, and the Developer may specify the order in which the container invokes the listener beans for each event type.

Servlet Context Events and Listeners.

Implementations of the following interface receive notifications about changes to the servlet context of the web application they are part of. To receive notification events, the implementation class must be configured in the deployment descriptor for the web application. All ServletContextListeners are notified of context initialization before any filter or servlet in the web application is initialized. All servlets and filters have been destroyed before any ServletContextListeners are notified of context destruction.

package javax.servlet;

public interface ServletContextListener extends java.util.EventListener {

	public void contextDestroyed(ServletContextEvent sce);
	public void contextInitialized(ServletContextEvent sce);

}
					
This is the event class for notifications about changes to the servlet context of a web application:
package javax.servlet;

public class ServletContextEvent extends java.util.EventObject {

	public ServletContext getServletContext();

}
					

Implementations of the following interface receive notifications of changes to the attribute list on the servlet context of a web application. To receive notification events, the implementation class must be configured in the deployment descriptor for the web application.

package javax.servlet;

public interface ServletContextAttributeListener extends java.util.EventListener {

	public void attributeAdded(ServletContextAttributeEvent scae);
	public void attributeRemoved(ServletContextAttributeEvent scae);
	public void attributeReplaced(ServletContextAttributeEvent scae);

}
					
This is the event class for notifications about changes to the attributes of the servlet context of a web application:
package javax.servlet;

public class ServletContextAttributeEvent extends javax.servlet.ServletContextEvent {

	public java.lang.String getName();
	public java.lang.Object getValue();

}
					

HTTP Session Events and Listeners.

Implementations of the following interface are notified of changes to the list of active sessions in a web application. To receive notification events, the implementation class must be configured in the deployment descriptor for the web application.

package javax.servlet.http;

public interface HttpSessionListener extends java.util.EventListener {
	
	public void sessionCreated(HttpSessionEvent hse);
	public void sessionDestroyed(HttpSessionEvent hse);

}
					

This is the class representing event notifications for changes to sessions within a web application:

package javax.servlet.http;

public class HttpSessionEvent extends java.util.EventObject {

	public HttpSession getSession();

}
					

Following listener interface can be implemented in order to get notifications of changes to the attribute lists of sessions within this web application:

package javax.servlet.http;

public interface HttpSessionAttributeListener extends java.util.EventListener {

	public void attributeAdded(HttpSessionBindingEvent hsbe);
	public void attributeRemoved(HttpSessionBindingEvent hsbe);
	public void attributeReplaced(HttpSessionBindingEvent hsbe);

}
					

Events of the following type are either sent to an object that implements HttpSessionBindingListener when it is bound or unbound from a session, or to a HttpSessionAttributeListener that has been configured in the deployment descriptor when any attribute is bound, unbound or replaced in a session. The session binds the object by a call to HttpSession.setAttribute and unbinds the object by a call to HttpSession.removeAttribute. The getValue function returns the value of the attribute that has been added, removed or replaced. If the attribute was added (or bound), this is the value of the attribute. If the attribute was removed (or unbound), this is the value of the removed attribute. If the attribute was replaced, this is the OLD value of the attribute.

package javax.servlet.http;

public class HttpSessionBindingEvent extends javax.servlet.http.HttpSessionEvent {
	
	public java.lang.String getName();
	public HttpSession getSession();
	public java.lang.Object getValue(); // returns :
		// new value for added (bounded) attributes,
		// old value for replaced and removed (unbounded) attributes

}
					

When container migrates a session between VMs in a distributed container setting, all session attributes implementing the HttpSessionActivationListener interface are notified.

Objects that are bound to a session may listen to container events notifying them that sessions will be passivated and that session will be activated. A container that migrates session between VMs or persists sessions is required to notify all attributes bound to sessions implementing HttpSessionActivationListener.

package javax.servlet.http;

public interface HttpSessionActivationListener extends java.util.EventListener {

	public void sessionDidActivate(HttpSessionEvent hse);
	public void sessionWillPassivate(HttpSessionEvent hse);

}
					

The following interface causes an OBJECT to be notified when it is bound to or unbound from a session. The object is notified by an HttpSessionBindingEvent object. This may be as a result of a servlet programmer explicitly unbinding an attribute from a session, due to a session being invalidated, or due to a session timing out.

package javax.servlet.http;

public interface HttpSessionBindingListener extends java.util.EventListener {

	public void valueBound(HttpSessionBindingEvent hsbe);
	public void valueUnbound(HttpSessionBindingEvent hsbe);

}
					

Servlet Request Events and Listeners.

A ServletRequestListener can be implemented by the developer interested in being notified of requests coming in and out of scope in a web component. A request is defined as coming into scope when it is about to enter the first servlet or filter in each web application, as going out of scope when it exits the last servlet or the first filter in the chain.

package javax.servlet;

public interface ServletRequestListener {

	requestInitialized(ServletRequestEvent sre);
	requestDestroyed(ServletRequestEvent sre);

}
					

Events of this kind indicate lifecycle events for a ServletRequest. The source of the event is the ServletContext of this web application.

package javax.servlet;

public class ServletRequestEvent extends java.util.EventObject {

	public ServletRequest getServletRequest();
	public ServletContext getServletContext();

}
					

A ServletRequestAttributeListener can be implemented by the developer interested in being notified of request attribute changes. Notifications will be generated while the request is within the scope of the web application in which the listener is registered. A request is defined as coming into scope when it is about to enter the first servlet or filter in each web application, as going out of scope when it exits the last servlet or the first filter in the chain.

package javax.servlet;

public interface ServletRequestAttributeListener {
	public void attributeAdded(ServletRequestAttributeEvent srae);
	public void attributeRemoved(ServletRequestAttributeEvent srae);
	public void attributeReplaced(ServletRequestAttributeEvent srae);

}
					

The following is the event class for notifications of changes to the attributes of ServletRequest in an application. The getValue() function returns the value of the attribute that has been added, removed or replaced. If the attribute was added, this is the value of the attribute. If the attribute was removed, this is the value of the REMOVED attribute. If the attribute was replaced, this is the OLD value of the attribute.

package javax.servlet;

public class ServletRequestAttributeEvent extends ServletRequestEvent {

	public java.lang.String getName();
	public java.lang.Object getValue();

}
					

Table 3.1. Events and Listener Interfaces

ScopeEventListener Interface and Event Class
Servlet ContextInitialization and destructionServletContextListener, ServletContextEvent
Attribute added, removed, or replacedServletContextAttributeListener, ServletContextAttributeEvent
HTTP SessionCreated and destroyedHttpSessionListener, HttpSessionEvent
Activated and passivated (migrated)HttpSessionActivationListener, HttpSessionEvent
Attribute added, removed, or replacedHttpSessionAttributeListener, HttpSessionBindingEvent (note the class name !)
Object bound or unboundHttpSessionBindingListener (note, interface must be implemented by attribute class !), HttpSessionBindingEvent
Servlet RequestInitialized and destroyedServletRequestListener, ServletRequestEvent
Attribute added, removed, or replacedServletRequestAttributeListener, ServletRequestAttributeEvent

The Developer of the Web application provides listener classes implementing one or more of the listener classes in the servlet API. Each listener class must have a public constructor taking NO arguments. The listener classes are packaged into the WAR, either under the WEB-INF/classes archive entry, or inside a JAR in the WEB-INF/lib directory.

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. 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.


<web-app>
	...
	<listener>
		<listener-class>listeners.ContextListener</listener-class>
	</listener>
	...
</web-app>

					

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. Distributed containers are not required to propagate either servlet context events or HttpSession events to other JVMs. Listener class instances are scoped to one per deployment descriptor declaration per Java Virtual Machine.

Professional hosting     Belorussian informational portal         Free SCBCD 1.3 Study Guide     Free SCDJWS 1.4 Study Guide     SCDJWS 1.4 Quiz     Free IBM Certified Associate Developer Study Guide     Free SCJP 5.0 (Tiger) Study Guide     Free Mock Exam Engine     IBM Test 000-287. Enterprise Application Development with IBM WebSphere Studio, V5.0 Study Guide