• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Questins related to Session Listeners

 
Mannu hak
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
 
Sravan Kumar
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.
 
Sergey Tyulkin
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic