How do I get a session object to perform a task before it is invalidated?

Denis Navarre

Hi Tony,

You must implement the javax.servlet.http.HttpSessionBindingListener interface in your object stored in session.

Two methods must be added to your object when you implement this interface:

public void valueBound(HttpSessionBindingEvent arg1)
public void valueUnbound(HttpSessionBindingEvent arg1)

These methods are called when the object is put in session (valueBound) and when the object is removed from the session (valueUnbound).

To answer to your question: you have to put your code in the valueUnbound method to do some actions just before the object is removed (or replaced!) from the session.

If the session expired or is invalidated, all objects are unbound from the session, and the method valueUnbound is called.

I use this interface to delete temporary files and it works fine.

Denis Navarre