• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HttpSessionBindingListener & HttpSessionAttributeListener

 
Gab Buda
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While implementing some examples for understanding listeners, I did the following test:

- a Dog class implementing HttpSessionBindingListener; its methods valueBound() and valueUnbound() display the name of the method and the event.getValue()

- a HttpSessionAttributeListener with attributeAdded, attributeRemoved, attributeReplaced dispaying the same thing (the name of the method and event.getValue())

- a TestServlet doing:

HttpSession session = req.getSession();
System.out.print("\n\n setting");
session.setAttribute("dog",new Dog("Fido"));

System.out.print("\n\n replacing");
session.setAttribute("dog",new Dog("NewFido"));

System.out.print("\n\n removing");
session.removeAttribute("dog");

Two things I want to discuss about:

1) In the first test of jdiscuss.com there is a question about listeners being notified when an attribute is replaced, and the HttpSessionBindingListener is not supposed to be notified (in their answer) but in my example it is notified; is this Tomcat-specific or it is normal since a replacement = old valueUnbound + new valueBound? (not found in the spec)

2) The HFSJ(p.263) and the api say that HttpSessionBindingEvent.getValue() returns the OLD object value. In my example, this is true only if I comment out "implements HttpSessionBindingListener" in the Dog's class declaration (i.e. the only listener is now the HttpSessionAttributeListener). In this case, the output is, as expected:

setting
attributeAdded() value=Fido

replacing
attributeReplaced() value=Fido

removing
attributeRemoved() value=NewFido

BUT if I let the Dog class implement the HttpSessionBindingListener, the output becomes:

setting
valueBound() value=Fido
attributeAdded() value=Fido

replacing
valueBound() value=NewFido
valueUnbound() value=null
attributeReplaced() value=NewFido

removing
valueUnbound() value=NewFido
attributeRemoved() value=NewFido

:roll: which means that the call of event.getValue() in the attributeReplaced() method returns the NEW value of the attribute!!!

If someone has an explanation...
Thank you

[ March 26, 2006: Message edited by: gabb buda ]
[ March 26, 2006: Message edited by: gabb buda ]
 
Mahendar Reddy
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Gabb...

It is a good question actually...

But u have to understand well...

The result depends on the time when the 'HttpSessionBindingEvent' object is created by the container...

In the second case it is happening little late for attributeReplaced method, mean time the 'value' property of the event is updated for 'valueBound' method.
There r lot of issues effecting this...but sorry,I can't explain all

This is just my observation...I hope u understand this...




[ March 26, 2006: Message edited by: Mahendar Reddy ]
 
Matthijs Wensveen
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then that's either a bug in the documentation OR a bug in the implementation.

Normally adding different listeners to anything shouldn't change the behaviour of an event generating class.

just my 2c

(Yes, I will file a bug report)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic