wood burning stoves 2.0*
The moose likes Servlets and the fly likes SessionBindingListener Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "SessionBindingListener" Watch "SessionBindingListener" New topic
Author

SessionBindingListener

Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

What servlet should implement SessionBindingListener? I have read on how to use this class and it is exactly what I need. But I have yet to find out what Servlet is supposed to implement this class. I have a lot of servlets in my web app.

Thanks.


GenRocket - Experts at Building Test Data
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
You don't usually have a servlet implement HttpSessionBindingListener.
A typical use would be a "helper" class object that holds all information related to a single user. If you have that class implement the listener, your object will be notified when the session is about to be destroyed because the servlet engine un-binds all the attached objects. You can use that call to valueUnbound to save the user information in a database or serialized to a file.
Another use might be a class that tracks all user sessions in a web application.
Bill
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Thanks William. Right now, I just have a very simple UserBean that holds all my user information. Would it be a bad idea to also include methods to manipulate the session and the database from within this bean and implement the Listener? Or should I make a completely seperate class that does the DB calls and implements the listener and just call my UserBean from that class?
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Ok, so I am making a helper class right now. I know I can pass the request into one of my methods so that I can get the session. For example, I will have a method that will create the UserBean and add all the data to it, and then write some info to the Database about when the user logged in, etc.
So I have
public void setUser(HttpServletRequest request)
{
}
But in the valueBound() and valueUnbound() methods, how do I get the request and/or session objects from those methods? I can't pass those parameters to those methods.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Ok, I found this post and got the answer.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Everything started out good. I implemented the Binding Listener in my class. Retrieved the session and when the session timed out, I updated the database. I also was able to do this when I actually clicked on logout and the only thing that action does is invalidate the session.
AND THEN.....
I ran it all again, and I get an Exception everytime that says my session has already been invalidated. I get this exception on the line where I do:
UserBean userBean = (UserBean)session.getAttribute("userBean");
So it worked once, and now it is not working at all. And I haven't changed a bit of code. Anyone have similar problems?
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

I have worked around my issue. But I still have a question as to why this would happen.
I was under the impression that when you invalidate a session, the valueUnbound() method gets called BEFORE the session actually is invalidated. Then, after that method is left, the session actually invalidates.
Is this not the case?
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

I guess what I am wondering is if the valueUnbound method gets called when the session is invalidate, what is the point in being able to retrieve the session from the HttpSessionBindingEvent? What the heck are you going to do with a null session?
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
It is true that when your valueUnbound method is called, you can't use the session for anything - the servlet engine is in the middle of disassembling it. Therefore, your utility class should be the holder for everything you are going to need to save.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Originally posted by William Brogden:
It is true that when your valueUnbound method is called, you can't use the session for anything - the servlet engine is in the middle of disassembling it. Therefore, your utility class should be the holder for everything you are going to need to save.

That's what I had assumed and that is how I worked around the issue to begin with.
Thanks.
Yatin Dhingra
Greenhorn

Joined: May 06, 2008
Posts: 5
session object doesn not becomes null untill all the values get unbinded from it. if you will print session object in your valueunbound() it willl not print null... actually when you try to get your user bean from session chances are that it already got unbinded before your listner class object's gets unbinded.. hence best practice is to specifically removethe listner object(session.removeattribute(listnerclassobjKey)) before sayin session.invalidate(). this way you will be sure that your user bean is still binded to the session.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: SessionBindingListener
 
Similar Threads
Error Instantiating Web App - Listener
Session Listener
ActionServlet destroy()
Clean up after sessions end
HttpSession