Thursday, May 15, 2008

HttpSessionBindingListener vs. HttpSessionAttributeListener

HttpSessionBindingListener:
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)

or

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);

or

session.removeAttribute ("anything", AnyObjectNotOnlyMyClass);


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

Differences:
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: