How are the individual methods invoked? Does container invokes them? If yes, how?
What is the difference between the two classes?
1. Listeners can be configured in web.xml; HttpSessionAttributeListener, for example. The specified methods on an object of an implementing class will be invoked when attributes are added/replaced/removed from the session.
2. Some Listeners are implemented by normal java classes and not configured in web.xml. Specified methods will be invoked in them when an event occurs. For instance, HttpSessionBindingListener will be implemented by an attribute object that will be bound/unbound from the session. This object will be notified accordingly. This will help the objects to take some action on themselves or others when an event occurs.
1) As you know, Servlet specification mostly defines interfaces of components - Request, HttpServletRequest, HttpSession etc. And container itself has implementations of these interfaces. We can't know for sure, how it implements them, but we can imagine something like this (for example, HttpSession) a) HttpSession implementation class has a map of listeners b) when something happens (for example, user calls session.setAttribute), there is an iteration through this map, that calls appropriate method of each listener. If you want to know the details, you can look in some open source servlet container, for example, Tomcat.
posted 14 years ago
If the methods in the class implementing listeners are invoked my the container, then why isn't this provided as default functionality of the container. for ex, you said -
For instance, HttpSessionBindingListener will be implemented by an attribute object that will be bound/unbound from the session. This object will be notified accordingly.
The container knows that attriute are moved from one VM to another so it will call the bound/unbound methods.
Is there any thumb rule about the listeners that we should implement and when?
SCJP 1.4, SCWCD 1.4, SCBCD <br />IBM 285
When people don’t understand what you are doing they call you crazy. But this tiny ad just doesn't care: