DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Redis-Based Tomcat Session Management
  • Cookies Revisited: A Networking Solution for Third-Party Cookies
  • A Beginner's Guide to Spark UI: Concepts and How to Use It
  • Multi-Tenant .NET Applications With Keycloak Realms

Trending

  • Intro to RAG: Foundations of Retrieval Augmented Generation, Part 2
  • Implementing API Design First in .NET for Efficient Development, Testing, and CI/CD
  • Understanding the Shift: Why Companies Are Migrating From MongoDB to Aerospike Database?
  • Software Delivery at Scale: Centralized Jenkins Pipeline for Optimal Efficiency
  1. DZone
  2. Coding
  3. Languages
  4. Hooking Up HTTPSessionListener with Tomcat

Hooking Up HTTPSessionListener with Tomcat

By 
Shan Arshad user avatar
Shan Arshad
·
May. 09, 14 · Interview
Likes (0)
Comment
Save
Tweet
Share
14.2K Views

Join the DZone community and get the full member experience.

Join For Free

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.


Apache Tomcat Session (web analytics)

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

Opinions expressed by DZone contributors are their own.

Related

  • Redis-Based Tomcat Session Management
  • Cookies Revisited: A Networking Solution for Third-Party Cookies
  • A Beginner's Guide to Spark UI: Concepts and How to Use It
  • Multi-Tenant .NET Applications With Keycloak Realms

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!