Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in Listener?

 
Peter Venkat
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to know the difference between HttpSessionAttirbuteListener and HttpSessionBindingListener.

My understanding is:

HttpSessionAttirbuteListener: when an attribute is added to the session it is notified.

HttpSessionBindingListener: when an object is added to the session the instances of the class will be notified.

Please help.
 
Tom Kowalski
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I will give an example if you don't mind ^^. Let's assume that you have got class Dog, which implements HttpSessionBindingListener


And you have got also second class:


The diffrence is simply: List will hear everything, whatever you will add to session, this listener will do his job. But Dog Listener will work only when you will add a Dog to a session.

Example:


(You should also remember, that if you want to use HttpSessionAttributeListener, you should mark it in web.html. You don't need to do it with HttpSessionBindingListener ^^.

 
Vishwanath Krishnamurthi
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Peter,

I thought of how I could use these listeners in my mock application and this might help you:

(I havent tried yet, so I'm not too sure if it will work. ranchers, correct me if im wrong)

My application is one where a user can buy sell etc.. and well there is a shopping cart too.

Whenever a user visits my site, a session would get created.

This, my HttpSessionListener would come to know and in its sessionCreated() method I would have a
noOfGuestsOnline++;

If that user signs in, I would set an attribute like "signedIn" with a value say "true" in the session.
The attributeAdded() method in HttpSessionAttributeListener would check if the added attribute is "signedIn" and in that case do

noOfGuestsOnline--;
noOfSignedInUsersOnline++;

When the user session expires or invalidates, I probably would want all his shoppingCart details to be persisted in the database.
So the valueUnbound() method in HttpSessionBindingListener would come in handy here.

Vishwa
 
Vishwanath Krishnamurthi
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
found this thread pretty useful
 
Peter Venkat
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tomasz:

Thanks a LOT for your reply. It was really really helpful. I've finished my certification and got 98%
I owe a LOT to this forums.

long live Javaranch

Naveen. V
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic