Question

I've just tried to add session listeners in my test web app the HttpSessionBindingListener and HttpSessionAttributeListener ones; They both implemented into my test object as :

public class MySessionListener implements HttpSessionBindingListener,HttpSessionAttributeListener 
{
//implemented methods...
}

The thing is...

I tried code like a

session.setAttribute("name",new TestValue());

as a result, I expected to call HttpSessionBindingListener event like valueBound(...) but I have called just the HttpSessionAttributeListener one as attributeAdded(...) only :(

Moreover, concerning the valueUnbound(...) method attitude is totally the same thing as I mentioned for I use code like

session.removeAttribute("name");

...but I get all the same attributeRemoved(...) being called only :S

I wanted to try putValue() method instead but right now I am watching HttpSession docs which says that "putValue is Deprecated. As of Version 2.2, this method is replaced by setAttribute(java.lang.String, java.lang.Object)"

So I don't get it why the valueBound(...) or valueUnbound(...) doesn't invoke and how to invoke them anyway?

Thanks

Était-ce utile?

La solution

Short answer

valueBound is not being called because MySessionListener is not the object being added to a session.

If TestValue implemented HttpSessionBindingListener it's valueBound method would be called.

Longer Explanation

HttpSessionBindingListener is used to notify the object that it is being added to a session.

HttpSessionAttributeListener is used (as you discovered) for attributes being added/removed from a session.

With the HttpSessionBindingListener here is what the container is saying: "Hey TestValue, you were added to a session"

With the HttpSessionAttributeListener here is what the container is saying: "Hey MySessionListener, an attribute was added to a session"

So, if TestValue implements HttpSessionBindingListener, it would have valueBound called when added to a session:

session.setAttribute("name",new TestValue());

In your case, if you added an instance of MySessionListener to a session (not that you'd want to, but because it implements HttpSessionBindingListener) it would call the valueBound method when added to a session:

session.setAttribute("name", new MySessionListener());

Also see: Practical Usage of HttpSessionBindingListener And HttpSessionAttributeListener

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top