Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

HttpSessionXXXListener question  RSS feed

 
Forest Huge
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
there are two listeners,
HttpSessionAttributeListener
HttpSessionBindingListener
Can someone tell me what is the difference between them and how to use them by example?
Thanks
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The two interfaces are concerned with the same type of event, but "as seen" from the two perspectives involved.

HttpSessionBindingListener is from the object's perspective (the object being placed into or removed from the session). The two methods are valueBound and valueUnbound. So basically this is a way for your objects to be 'aware' if they are in a session or not.

If your object implements that interface and you code something like
session.setAttribute("foo", myObject);
Then myObject will receive a HttpSessionBindingEvent and valueBound will be called)

The other interface, HttpSessionAttributeListener is from the perspective of the session object itself. As a very simple example, if your application keeps track of 'logged in users' by placing their username in the session, then you could code a class that implements this interface and then it would receive notifications of objects being placed into the session.

The HttpSessionBindingEvent that each method of this interface receives gives you the information you need.

session.setAttribute("user", userList);

the attributeReplaced method would be called with an HttpSessionBindingEvent object. You would use this object's getName() and getValue() methods to determine if the object just bound was "user" and then if one of the values was the username of interest.
 
Paul Stevens
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
forestever
Welcome to the JavaRanch! Please adjust your display name to meet
the JavaRanch Naming Policy. You can change it here.
Thanks!
and welcome to the JavaRanch!
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!