How to get list of active sessions in java with HttpSessionListener?

In this post, let us see how to track the list of  sessions that are active in a web application.  For this we need to create a class that implements HttpSessionListener which extends java.util.EventListener. A class that implements this interface gets notified when a new session is created or an existing session is expired or invalidated.Also any class that impliments this interface HttpSessionListener, should implement the two methods sessionCreated & sessionDestroyed as given below.

Let us now see when  the  sessionCreated method is executed.                  1. When new session is created i.e  when request.getSession(true) or request.getSession(false) is executed.
2. When a JSP page is called (session is a implicit object). .Let us see when the  sessionDestroyed method is executed.

1. When the session timeout occurs
2. When session.invalidate() method is called.

Here is the code for capturing the live sessions in a table.  Here the session created after successful authentication is captured and removed from the table when the session is expired.


login class

SessionUpdate class

Leave a Reply

Be the First to Comment!