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

Questins related to Session Listeners

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have few qoestions -

(1) When we implement a listener interface there are various methods in it. (for ex - HttpSessionAttributeListener has attributeAdded,attributeRemoved and attributeReplaced)

How are the individual methods invoked? Does container invokes them? If yes, how?


(2) HFSJ syas that some of the listeners are implemented my attribute classes and some by other classes. What is the difference between the two classes?



Also if anyone give a real life example for the listener classes then it would be great...



Thanks
 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

How are the individual methods invoked? Does container invokes them? If yes, how?



Yes!

What is the difference between the two classes?


1. Listeners can be configured in web.xml; HttpSessionAttributeListener, for example. The specified methods on an object of an implementing class will be invoked when attributes are added/replaced/removed from the session.

2. Some Listeners are implemented by normal java classes and not configured in web.xml. Specified methods will be invoked in them when an event occurs. For instance, HttpSessionBindingListener will be implemented by an attribute object that will be bound/unbound from the session. This object will be notified accordingly. This will help the objects to take some action on themselves or others when an event occurs.
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) As you know, Servlet specification mostly defines interfaces of components - Request, HttpServletRequest, HttpSession etc. And container itself has implementations of these interfaces. We can't know for sure, how it implements them, but we can imagine something like this (for example, HttpSession)
a) HttpSession implementation class has a map of listeners
b) when something happens (for example, user calls session.setAttribute), there is an iteration through this map, that calls appropriate method of each listener.
If you want to know the details, you can look in some open source servlet container, for example, Tomcat.
 
Mannu hak
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the methods in the class implementing listeners are invoked my the container, then why isn't this provided as default functionality of the container. for ex, you said -

For instance, HttpSessionBindingListener will be implemented by an attribute object that will be bound/unbound from the session. This object will be notified accordingly.



The container knows that attriute are moved from one VM to another so it will call the bound/unbound methods.

Is there any thumb rule about the listeners that we should implement and when?
 
When people don’t understand what you are doing they call you crazy. But this tiny ad just doesn't care:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic