Developing Web Applications for WebLogic Server
The following sections discuss application events and event listener classes:
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 them in a Web application. The servlet container generates events that cause the event listener classes to do something. In other words, the servlet container calls the methods on a user's event listener class.
The following is an overview of this process:
For servlet context events, the event listener classes can receive notification when the Web application is deployed or 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 event listener classes to:
The following table lists the types of Servlet context events, the interface your event listener class must implement to respond to each Servlet context event, and the methods invoked when the Servlet context event occurs.
The following table lists the types of HTTP session events your event listener class must implement to respond to the HTTP session events and the methods invoked when the HTTP session events occur.
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 class:
web.xml
deployment descriptor of the Web application for which you are creating an event listener class in a text editor. The web.xml
file is located in the WEB-INF
directory of your Web application. <listener>
element. The event declaration defines the event listener class that is invoked when the event occurs. The <listener>
element must directly follow the <filter>
and <filter-mapping>
elements and directly precede the <servlet>
element. You can specify more than one event listener class for each type of event. WebLogic Server invokes the event listener classes in the order that they appear in the deployment descriptor (except for shutdown events, which are invoked in the reverse order). For example:<listener>
<listener-class>myApp.MyContextListenerClass</listener-class>
</listener>
<listener>
<listener-class>myApp.MySessionAttributeListenerClass</listener-class>
</listener>
To write an event listener class:
The following useful classes are passed into the methods in an event listener class:
The following examples provide some basic templates for event listener classes.
package myApp;
import javax.servlet.http.*;
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.
*/
}
}
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.
*/
}
}