valueUnbound method of HttpSessionBindindListener

When is the valueUnbound method of HttpSessionBindindListener called and what is the purpose of using it.can someone give example

thnx in advance

SCJP 1.4, SCWCD1.4, OCA(1Z0-007)

Bimal Patel

Ranch Hand

Posts: 130

posted 11 years ago

Hi N Chaurasia,

As per my knowledge, the method valueUnbound is called exactly before removal of the object from the session. If you've a class which implements HttpSessionBindingListener, while removing an object of that class from the session, valueUnbound method will be called. So, this is from object to object i.e. one can track for a particular object and while binding or unbinding, allocate resources or deallocate the same.

There is another listener you may aware of i.e. HttpSessionAttributeLisener by which the tracking of all the sessions' attributes is possible. One more point to note. There is no need to configure for HttpSessionBindingListener. But for HttpSessionAttributeListener it is mendatory to do so. [ October 03, 2005: Message edited by: Bimal Patel ]

Hi Bimal, I have another query. You said : one can track for a particular object and while binding or unbinding, allocate resources or deallocate the same.

Is there any specific advantage to deallocate resource after/before the object if unbound from the session. We could be it before or after. Will it make any difference.

SCJP 1.4, SCWCD1.4, OCA(1Z0-007)

Bimal Patel

Ranch Hand

Posts: 130

posted 11 years ago

Hi N Charasia,

Well, one thing is for sure that whenever that object is being garbage collected, objects inside that will also be garbage collected. And in that way, from the first sight, we don't need to write those things in valueUnbound method. Actually I've given you only one example. There may be many other things to be performed like if you are writing to a file IO, you better need to close the stream after flushing. Right? So, such kind of operation you can perform in valueUnbound method. And in valueBound method, you may intialise certain objects with proper parameters. One more example is a JDBC connection object. Or there may be some session specific things, I can't specify because I've not used HttpSessionBindingListener, you can perform in valueBound and valueUnbound methods.

I think there are very few instances when you need to use this listener. But if you really want to track whether a particular object is removed from the session, you can do that with this listener.