How to run arbitrary code at end of Java HTTP Servlet session?

Our application requires us to serialize some user session data in the file system if their session expires. We will serialize it as it happens, but it will be too expensive due to the nature of our application.

If we can figure out a way to bind some kind of 'onSessionEnd' method, we could run the cleanup code.

+3
source share
1 answer

You need to configure HttpSessionListenerfor your application.

The interface is called onSessionDestroy, which is called when the session ends, and you can serialize directly.

To configure the listener, add to web.xml:

<listener>
    <listener-class>com.example.app.session.MySessionListener</listener-class>
</listener>
+4

All Articles