10 Application Events and Event Listener Classes
This chapter includes the following sections:
- Overview of Application 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. - Servlet Context Events
Examine a listing of 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. - HTTP Session Events
The HTTP Session Events contains a list of event types, interfaces and methods that are used to indicate the activation and deactivation of a HTTP session along with the addition and removal of attributes during a HTTP session. - Servlet Request Events
Examine a listing of the types of servlet request events, the interface your event listener class must implement to manage state across the life cycle of servlet requests and the methods invoked when the request events occur. - Configuring an Event Listener Class
Learn how to configure an event listener class. - Writing an Event Listener Class
Learn how to write an event listener class. - Templates for Event Listener Classes
Examine examples that provide some basic templates for event listener classes. - Additional Resources
Overview of Application 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:
-
The user creates an event listener class that implements one of the listener interfaces.
-
This implementation is registered in the deployment descriptor.
-
At deployment time, the servlet container constructs an instance of the event listener class. (This is why the public constructor must exist, as discussed in Writing an Event Listener Class.)
-
At run time, the servlet container invokes on the instance of the listener class.
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:
-
Manage database connections when a Web application is deployed or shuts down
-
Create standard counter utilities
-
Monitor the state of HTTP sessions and their attributes
Parent topic: Application Events and Event Listener Classes
Servlet Context Events
Examine a listing of 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.
Table 10-1 Servlet Context Events
Type of Event | Interface | Method |
---|---|---|
Servlet context is created. |
javax.servlet.ServletContextListener |
contextInitialized() |
Servlet context is about to be shut down. |
javax.servlet.ServletContextListener |
contextDestroyed() |
An attribute is added. |
javax.servlet.ServletContextAttributesListener |
attributeAdded() |
An attribute is removed. |
javax.servlet.ServletContextAttributesListener |
attributeRemoved() |
An attribute is replaced. |
javax.servlet.ServletContextAttributesListener |
attributeReplaced() |
Parent topic: Application Events and Event Listener Classes
HTTP Session Events
The HTTP Session Events contains a list of event types, interfaces and methods that are used to indicate the activation and deactivation of a HTTP session along with the addition and removal of attributes during a HTTP session.
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.
Table 10-2 HTTP Session Events
Type of Event | Interface | Method |
---|---|---|
An HTTP session is activated. |
javax.servlet.http.HttpSessionListener |
sessionCreated() |
An HTTP session is about to be passivated. |
javax.servlet.http.HttpSessionListener |
sessionDestroyed() |
An attribute is added. |
javax.servlet.http.HttpSessionAttributeListener |
attributeAdded() |
An attribute is removed. |
javax.servlet.http.HttpSessionAttributeListener |
attributeRemoved() |
An attribute is replaced. |
javax.servlet.http.HttpSessionAttributeListener |
attributeReplaced() |
Note:
The Servlet 4.0 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
.
Parent topic: Application Events and Event Listener Classes
Servlet Request Events
Examine a listing of the types of servlet request events, the interface your event listener class must implement to manage state across the life cycle of servlet requests and the methods invoked when the request events occur.
Table 10-3 Servlet Request Events
Type of Event | Interface | Method |
---|---|---|
The request is about to go out of scope of the Web application. |
javax.servlet.ServletRequestListener |
requestDestroyed() |
The request is about to come into scope of the Web application. |
javax.servlet.ServletRequestListener |
requestInitialized() |
Notification that a new attribute was added to the servlet request. Called after the attribute is added. |
javax.servlet.ServletRequestAttributeListener |
attributeAdded() |
Notification that a new attribute was removed from the servlet request. Called after the attribute is removed. |
javax.servlet.ServletRequestAttributeListener |
attributeRemoved() |
Notification that an attribute was replaced on the servlet request. Called after the attribute is replaced. |
javax.servlet.ServletRequestAttributeListener |
attributeReplaced() |
Parent topic: Application Events and Event Listener Classes
Configuring an Event Listener Class
Learn how to configure an event listener class.
To configure an event listener class:
Parent topic: Application Events and Event Listener Classes
Writing an Event Listener Class
Learn how to write an event listener class.
To write an event listener class:
Parent topic: Application Events and Event Listener Classes
Templates for Event Listener Classes
Examine examples that provide some basic templates for event listener classes.
Parent topic: Application Events and Event Listener Classes
Servlet Context Event Listener Class Example
package myApp; import javax.servlet.http.*; public final class MyContextListenerClass implements ServletContextListener { public void contextInitialized(ServletContextEvent event) { /* This method is called prior to the servlet context being 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. */ } }
Parent topic: Templates for Event Listener Classes
HTTP Session Attribute Event Listener Class 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. */ } }
Parent topic: Templates for Event Listener Classes
Additional Resources
-
Servlet 4.0 specification at
https://jcp.org/en/jsr/detail?id=369
-
The Java EE tutorial at
https://javaee.github.io/tutorial/toc.html
Parent topic: Application Events and Event Listener Classes