Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Cannot get HttpSessionBindingListener method valueBound to trigger

 
Tony Bateman
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm reading the excellent 'Head First Servlets and JSP' book, and am trying to extend one class to implement HttpSessionBindingListener, but it's not working.

Here is the simple Dog class that I extended:



Here is the Context listener that sets the dog instance as a context attribute:

And here is the Servlet:


But for some reason, the valueBound method never seems to be triggered.

ANy clues why this is the case?

Kind regards,

Tony.
 
ramprasad madathil
Ranch Hand
Posts: 489
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A HttpSessionBindingListener causes an object to be notified when it is bound to or unbound from a session..

1. You dont need a ServletContextListener - binding the object to the context wouldnt fire the HttpSessionBindingEvent.

2. Change the code in your servlet to attach the 'dog' object to a HttpSession



Hope that helps,
Ram.
 
Tony Bateman
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Helps a lot! Thanks :-)

Kind regards,

Tony.
 
Tony Bateman
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK....went back this evening, re-read the section of the book, recoded my classes and still doesn't work.

Can somebody have a look:

Dog class (which now implements HttpSessionBindingListener):



And the servlet:


Still not triggering the Dogs valueBound or valueUnbound methods.

Can somebody please tell me why?

Kind regards,

Tony.
 
Tony Bateman
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fixed it. Got it working thanks.

- Tony.
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess that when the object is atached to the session or dismissed from the session..here by session i mean the user session (request.getSession())
this works...but in your case your are setting it to the ServletContext.

i have not tested this ...so , kindly let me know whether it works for you.

e-mail: rahul.rec.dgp@gmail.com

cheers!
 
Bosun Bello
Ranch Hand
Posts: 1510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rahul, this is a plain old Java class (Not a servlet). HtpSessionBindingListener causes an object to be notified when it is bound to or unbound from a session. And this happens when the object is added, replaced or removed from the session. As in SetAttribute, removeAttribute, replaceAttribute.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic