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 ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Doubt in Listener?

 
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.
 
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 ^^.

 
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic