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.
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.
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.
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.
ReplyDeleteIt 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.
Thanks for the information.Get the best Selenium Course in Chennai from Hope Tutors at Affordable Fees.
ReplyDeleteThanks for sharing this unique information with us.Keep update like this.
ReplyDeleteDevOps Training in Velachery
DevOps Training in Anna Nagar
DevOps Training in Tambaram
DevOps Training in T Nagar
DevOps Training in Vadapalani
DevOps Training in Adyar
DevOps Training in OMR
DevOps Training in Thiruvanmiyur
DevOps Training in Porur
Nice blog
ReplyDeleteVisit here -> BIG DATA AND HADOOP TRAINING IN BANGALORE
I really enjoyed to read this blog...i got lot of useful information from this blog...
ReplyDeleteAWS Training in Chennai
AWS Training in Bangalore
AWS Training in Coimbatore
AWS Training in Hyderabad
AWS Course in Bangalore
Best AWS Training in Bangalore
AWS Training Institutes in Bangalore
AWS Certification Training in Bangalore
Data Science Courses in Bangalore
DevOps Training in Bangalore
it is very much useful for me to understand many concepts and helped me a lot. it is really explainable very well and i got more information from your blog.
ReplyDeletepython training in chennai
python online training in chennai
python training in bangalore
python training in hyderabad
python online training
python flask training
python flask online training
python training in coimbatore
ReplyDelete360DigiTMG, 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.
I appreciate that you produced this wonderful article to help us get more knowledge about this topic.
ReplyDeleteI 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
Such a very useful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article.
ReplyDeleteWeb 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
Its a wonderful post and very helpful, thanks for all this information. You are including better information regarding this topic in an effective way.Thank you so much
ReplyDeletejava training in chennai
java training in tambaram
aws training in chennai
aws training in tambaram
python training in chennai
python training in tambaram
selenium training in chennai
selenium training in tambaram
I must appreciate you for providing such a valuable content for us. This is one amazing piece of article. Helped a lot in increasing my knowledge.
ReplyDeletesap training in chennai
sap training in porur
azure training in chennai
azure training in porur
cyber security course in chennai
cyber security course in porur
ethical hacking course in chennai
ethical hacking course in porur
Great post and informative blog.it was awesome to read, thanks for sharing this great content to my vision.
ReplyDeleteGood discussion.
hardware and networking training in chennai
hardware and networking training in omr
xamarin training in chennai
xamarin training in omr
ios training in chennai
ios training in omr
iot training in chennai
iot training in omr