Thursday 9 October 2014

Call a method just before a session expires

In this tutorial we gonna learn that how to call a specific method when a session is going to be destroyed or invalidated by the application container. First of all we need to know why we are doing this. Well it may have many applications but typically we use it for updating user counts, releasing resources etc. We could use HttpSessionListener and do the job in sessionDestroyed() method.

How to implement HttpSessionListener ?

To implement SessionListner we just have to make a class that implements HttpSessionListener and override the sessionDestroyed() method. If you want to call a method when a session is created then you can override sessionCreated() method. After overriding these methods you can do whatever you want to do. You can even get the data associated with that session in these methods.

Example :

package service;
import java.util.logging.Logger;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

public class SessionListner implements HttpSessionListener {
   
   static final Logger log = Logger.getAnonymousLogger();
   private static int totalActiveSessions;
  
   public static int getTotalActiveSession(){
  return totalActiveSessions;
   }
  
   @Override
   public void sessionCreated(HttpSessionEvent se) {
  totalActiveSessions++;
  
   }
  
   @Override
   public void sessionDestroyed(HttpSessionEvent se) {
  totalActiveSessions--;
                // se.getAttribute("username") if you want to get session data 
   }
}


Now after making this class you have to register this listener to your web.xml file. To do this just add below line of code.

<listener>
    <listener-class>service.SessionListner</listener-class>
</listener>

Now whenever any session will be invalidated or timed out, the sessionDestroyed method will be called.

12 comments:

  1. Thanks for the post, I am techno savvy. I believe you hit the nail right on the head. I am highly impressed with your blog.
    It is very nicely explained. Your article adds best knowledge to our Java Online Training from India.
    or learn thru Java Online Training from India Students.

    ReplyDelete
  2. Thanks for the information.Get the best Selenium Course in Chennai from Hope Tutors at Affordable Fees.

    ReplyDelete

  3. 360DigiTMG, Indore is a leading solutions provider of Training and Consulting to assist students, professionals by delivering top-notch, world-class classroom and online training. It offers artificial intelligence course in indore.

    ReplyDelete
  4. I appreciate that you produced this wonderful article to help us get more knowledge about this topic.
    I know, it is not an easy task to write such a big article in one day, I've tried that and I've failed. But, here you are, trying the big task and finishing it off and getting good comments and ratings.
    IELTS Coaching in chennai

    German Classes in Chennai

    GRE Coaching Classes in Chennai

    TOEFL Coaching in Chennai

    spoken english classes in chennai | Communication training

    ReplyDelete