FAQ

Java

JSP

Servlet


Advertisement



What is difference between HttpSessionAttributeListener and HttpSessionBindingListener?

HttpSessionBindingListener interface is implemented by objects that want to be notified when they are bound to a session or unbound from a session when they bound or unbound to the session. HttpSessionBindingListeners implement the valueBound and valueUnbound methods.

For HttpSessionAttributeListener, the class that implements this interface must be specified in the Deployment Descriptor. Whenever any session attributes are added, removed and replaced, it will be notified. HttpSessionAttributeListeners implement the attributeAdded, attributeRemoved and attributeReplaced methods. A web application which has a HttpSessionAttributeListener would normally have one such listener which would handle all attributes and all sessions.

Conclusion

HttpSessionBindingListener deals with one session object and HttpSessionBindingListeners are unique to particular objects. If two classes of objects implement this listener, the implementations could be completely different. One class could implement an empty valueBound() method and the other class could implement a functional valueBound() method. If an object implements HttpSessionBindingListener, its valueBound and valueUnbound methods are only invoked when that particular object is bound to a session or unbound from a session.

HttpSessionAttributeListener instance is created by Web-Container, based on <listern> element in web.xml. For each <listern-class>, one listern instance is created. HttpSessionAttributeListener deals with all available session objects.

Basically, HttpSessionAttributeListener is implemented by an object that is interested in receiving events from all the sessions belonging to the application, while HttpSessionBindingListener is implemented by the object attributes for the particular session to which they are added or removed.

References

When do I use HttpSessionAttributeListener?
When do I use HttpSessionBindingListener?


Printer-friendly version Printer-friendly version | Send this 
article to a friend Mail this to a friend

Previous Next vertical dots separating previous/next from contents/index/pdf Contents

  |   |