Many ranchers seem confused about which listeners to declare in web.xml.
The main point is :
Classes implementing interfaces
other than HttpSessionBindingListener and HttpSessionActivationListener need to be declared in DD.
A trick to remember which listeners to register in the deployment descriptor: Listeners can be classified in 2 categories:
Those that are interested in all events of one type (e.g. HttpSessionListener - interested in ALL HTTP session creation and destruction events, HttpSessionAttributeListener - interested in all session attribute events - any attribute added / removed / replaced), and all the listeners for ServletContext and ServletRequest. The container creates one instance of each of these listeners.
Those that are interested only in specific events of one type. There are 2 listeners of this type :- HttpSessionBindingListener : interested in knowing when it is itself bound / unbound to a HttpSession
- HttpSessionActivationListener : interested in knowing when only the session it is bound to is activated or passivated, so that it can prepare itself for activation / passivation)
We create the instances of these listeners and set them as attributes in specific http sessions.
The first type of listeners
are configured in the deployment descriptor - as their methods are ALWAYS called when the event occurs.
The second type of listeners
are not configured in the deployment descriptor.
Note: The HFSJ book is in error on this matter, as the errata has confirmed.
ServletsFaq