Thursday, May 15, 2008

HttpSessionBindingListener vs. HttpSessionAttributeListener

If an object implements HttpSessionBindingListener, it is notified when it is bound to or unbound from a session. For example,

MyObject implements HttpSessionBindingListener
// class definition

If I call

session.setAttribute ("Object", MyObject)


session.removeAttribute ("Object", MyObject)

and so on, methods valueBound and/or valueUnbound (defined in HttpSessionBindingListener, implemented in MyObject are called)

When any class implements HttpSessionAttributeListener interface it is notified when any change in the attribute list of session occurs. For example

MyClass implements HttpSessionAttributeListener
// implementations of methods

session.setAttribute ("anything", AnyObjectNotOnlyMyClass);


session.removeAttribute ("anything", AnyObjectNotOnlyMyClass);

indicates change in the list of attributes and hence appropriate method is called

Implementing HttpSessionBindingListener works only for the object that implements it whereas implementing HttpSessionAttributeListener listens to any attribute added, removed or replaced.

See:Difference between HttpSessionBindingListener and HttpSessionAttributeListener.

No comments: