e-docs > WebLogic Server > Assembling and Configuring Web Applications > Application Events and Listeners |
Assembling and Configuring Web Applications |
Application Events and Listeners
The following sections describe how to configure and use Web Application events and listeners:
Overview of Application Events and Listeners
Application events provide notifications of a change in state of the servlet context (each Web Application uses its own servlet context) or of an HTTP session object. You write event listener classes that respond to these changes in state and you configure and deploy Application event and listener classes in a Web Application.
For servlet context events, the event listener classes can receive notification when the Web Application is deployed or is being undeployed (or when WebLogic Server shuts down), and when attributes are added, removed, or replaced.
For HTTP session events, the event listener classes can receive notification when an HTTP session is activated or is about to be passivated, and when an HTTP session attribute is added, removed, or replaced.
Use Web Application events to:
The following table lists the types of Servlet context events, the interface your event listener class must implement to respond to the event, and the methods invoked when the event occurs.
The following table lists the types of HTTP session events, the interface your event listener class must implement to respond to the event, and the methods invoked when the event occurs.
Note: The Servlet 2.3 specification also contains the javax.servlet.http.HttpSessionBindingListener and the javax.servlet.http.HttpSessionActivationListener interfaces. These interfaces are implemented by objects that are stored as session attributes and do not require registration of an event listener in web.xml. For more information, see the Javadocs for these interfaces.
To configure an event listener:
<listener>
<listener-class>myApp.myContextListenerClass</listener-class>
</listener>
<listener>
<listener-class>myApp.mySessionAttributeListenerClass</listener-class>
</listener>
The following useful classes are passed into the listener methods in a listener class:
Templates for Listener Classes
The following examples provide some basic templates for listener classes.
Servlet Context Listener Example
package myApp;
import javax.servlet.*;
public final class myContextListenerClass implements
ServletContextListener {
public void contextInitialized(ServletContextEvent event) {
/* This method is called when the servlet context is
initialized(when the Web Application is deployed).
You can initialize servlet context related data here.
*/
}
public void contextDestroyed(ServletContextEvent event) {
/* This method is invoked when the Servlet Context
(the Web Application) is undeployed or
WebLogic Server shuts down.
*/
}
}
HTTP Session Attribute Listener Example
package myApp;
import javax.servlet.*;
public final class mySessionAttributeListenerClass implements
HttpSessionAttributeListener {
public void attributeAdded(HttpSessionBindingEvent sbe) {
/* This method is called when an attribute
is added to a session.
*/
}
public void attributeRemoved(HttpSessionBindingEvent sbe) {
/* This method is called when an attribute
is removed from a session.
*/
}
public void attributeReplaced(HttpSessionBindingEvent sbe) {
/* This method is invoked when an attibute
is replaced in a session.
*/
}
}