File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Cannot get HttpSessionBindingListener method valueBound to trigger Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Cannot get HttpSessionBindingListener method valueBound to trigger" Watch "Cannot get HttpSessionBindingListener method valueBound to trigger" New topic
Author

Cannot get HttpSessionBindingListener method valueBound to trigger

Tony Bateman
Ranch Hand

Joined: Mar 21, 2005
Posts: 38
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

Joined: Jan 24, 2005
Posts: 489

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

Joined: Mar 21, 2005
Posts: 38
Hi,

Helps a lot! Thanks :-)

Kind regards,

Tony.
Tony Bateman
Ranch Hand

Joined: Mar 21, 2005
Posts: 38
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

Joined: Mar 21, 2005
Posts: 38
Fixed it. Got it working thanks.

- Tony.
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
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!


Rahul Bhattacharjee
LinkedIn - Blog
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
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.


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Cannot get HttpSessionBindingListener method valueBound to trigger