Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I run arbitrary code when a Java HTTP Servlet session ends?

Our application requires that we serialize some of the user's session data to the filesystem if their session expires. We would serialize it as is coming in but that would be too expensive due to the nature of our application.

If we can figure out a way to hook some sort of 'onSessionEnd' method we could run the cleanup code then.

like image 431
benstpierre Avatar asked May 14 '26 00:05

benstpierre


1 Answers

You need to setup a HttpSessionListener for your application.

There is a method on the interface called onSessionDestroy that shall be called when the session ends and you can perform serialization right then.

To setup the listener, you add into web.xml:

<listener>
    <listener-class>com.example.app.session.MySessionListener</listener-class>
</listener>
like image 167
Pablo Santa Cruz Avatar answered May 15 '26 13:05

Pablo Santa Cruz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!