You can develop event listeners and have them registered to listen to various life-cycle events of your Web applications. In this way, you can keep track of events such as application context initialization or destruction, session creation, and so on, and can react accordingly to them.
You have to perform two basic tasks so that your event listener is ready to handle events. These are:
...
1. Develop an event listener class that implements the appropriate listener interface. Java Servlet 2.3 API defines the following listener interfaces and type of events:
Application Context and Session Events
Scope of Event |
Event |
Event Type |
Event Interface |
ServletContext |
Initialization or destruction of a servlet context. |
ServletContextEvent |
ServletContextListener |
ServletContext |
Add, replace or remove context attribute(s). |
ServletContextAttributeEvent |
ServletContextAttributeListener |
Session |
Create, invalidate, passivate or activate a session. |
HttpSessionEvent, HttpSessionActivationListener |
HttpSessionListener |
Session |
Add, replace or remove session attribute(s). |
HttpSessionBindingEvent |
HttpSessionAttributeListener |
2. You must declare the listener class in the deployment descriptor of your Web application.