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.
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.
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?
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?
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.