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.

15 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. This is quite educational arrange. It has famous breeding about what I rarity to vouch. Colossal proverb.
    This trumpet is a famous tone to nab to troths. Congratulations on a career well achieved. This arrange is synchronous s informative impolites festivity to pity. I appreciated what you ok extremely here 


    Selenium training in bangalore
    Selenium training in Chennai
    Selenium training in Bangalore
    Selenium training in Pune
    Selenium Online training

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

    ReplyDelete
  4. Great post and informative blog.it was awesome to read, thanks for sharing this great content to my vision.
    Good discussion.
    Java Training in Chennai
    Java Training in Coimbatore
    Java Training in Bangalore

    ReplyDelete
  5. Great Article. Thank you for sharing! Really an awesome post for every one.

    IEEE Final Year projects Project Centers in Chennai are consistently sought after. Final Year Students Projects take a shot at them to improve their aptitudes, while specialists like the enjoyment in interfering with innovation. For experts, it's an alternate ball game through and through. Smaller than expected IEEE Final Year project centers ground for all fragments of CSE & IT engineers hoping to assemble. Final Year Project Domains for IT It gives you tips and rules that is progressively critical to consider while choosing any final year project point.

    Spring Framework has already made serious inroads as an integrated technology stack for building user-facing applications. Spring Framework Corporate TRaining the authors explore the idea of using Java in Big Data platforms.
    Specifically, Spring Framework provides various tasks are geared around preparing data for further analysis and visualization. Spring Training in Chennai

    ReplyDelete