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;
   public void sessionCreated(HttpSessionEvent se) {
   public void sessionDestroyed(HttpSessionEvent se) {
                // 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.


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


  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.

  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

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

  4. Great post and informative 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

  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

  6. Awesome blog. I enjoyed reading your articles. This is truly a great read for me. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work!
    data science course in coimbatore


  7. 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.

  8. 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

  9. very nice blogs!!! i have to learning for lot of information for this sites...Sharing for wonderful information.Thanks for sharing this valuable information to our vision. You have posted a trust worthy blog keep sharing.

    Web Designing Training in Chennai

    Web Designing Course in Chennai

    Web Designing Training in Bangalore

    Web Designing Course in Bangalore

    Web Designing Training in Hyderabad

    Web Designing Course in Hyderabad

    Web Designing Training in Coimbatore

    Web Designing Training

    Web Designing Online Training

  10. Good Post! , it was so good to read and useful to improve my knowledge as an updated one, keep blogging.After seeing your article I want to say that also a well-written article with some very good information which is very useful for the readers....thanks for sharing it and do share more posts likethis.

  11. I am glad to post a worthy article about the German Language Course and IELTS Coaching from KCR consultants, this may change your career growth and language skill.