Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
  • Piet Souris
  • Frits Walraven
  • Carey Brown

HttpSessionBindingListener & HttpSessionAttributeListener

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

Two things I want to discuss about:

1) In the first test of 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:

attributeAdded() value=Fido

attributeReplaced() value=Fido

attributeRemoved() value=NewFido

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

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

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

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 ]
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 ]
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)
This will take every ounce of my mental strength! All for a tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic