Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Hooking Up HTTPSessionListener with Tomcat

DZone's Guide to

Hooking Up HTTPSessionListener with Tomcat

· Integration Zone
Free Resource

Modernize your application architectures with microservices and APIs with best practices from this free virtual summit series. Brought to you in partnership with CA Technologies.

We have got a use case in project where we need to identify the time when Tomcat expires any user’s session. Basically we need to flush some persisted values of that user from DB.

For that i have hooked up sessionListener at application load(web.xml).

Web.xml

<listener>
    <display-name>sessionListener</display-name>
    <listener-class>com.javapitshop.SessionListener</listener-class>
</listener>

In web.xml file we are telling the server that it should intimate that class at the time of session creation and invalidation.
Server will automatically calls methods of this class if session of any user expires or developer himself invalidates any session.

SessionListener.java

package com.vdi.servlet;
 
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
 
/**
 * @author Javapitshop
 *
 */
public class SessionListener implements HttpSessionListener {
 
 
    @Override
    public void sessionCreated( HttpSessionEvent arg0 ) {
     
    }
 
    @Override
    public void sessionDestroyed( HttpSessionEvent sessionEvent ) {
         
    }
  }

In above code we simply have to implement HttpSessionListener interface and override its methods. Methods are self descriptive so you can provide your implementation in any or both cases depending upon your usecase.

Below is my implementation how i have provided implementation of one of those overridden methods.

@Override
public void sessionDestroyed( HttpSessionEvent sessionEvent ) {
        synchronized ( this ) {
            HttpSession session = sessionEvent.getSession();
 
            if ( session != null ) {
                UserSessions sessions = userDao.getUserSession( session.getId() );
                if ( sessions != null ) {
 
                    userDao.deleteUserSessionByUserId( sessions.getUserId() );
                    UtilityLogger.logInfo( "UserSession Released from an expired login of User : " + sessions.getUserId() );
 
                }
            }
        }

The major part of above provided implementation is persisted session id. Well as server is intimating application(SessionListener.java) on session invalidation so that means i couldn’t access anything saved in session as it is invalidated by server or user has called invalidate function himself. So for that we need to persist every user’s session id in DB and remove it from DB whenever its session expires or invalidates.


The Integration Zone is proudly sponsored by CA Technologies. Learn from expert microservices and API presentations at the Modernizing Application Architectures Virtual Summit Series.

Topics:

Published at DZone with permission of Shan Arshad, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}