Invalidating the session in jsp

20-Apr-2016 06:36 by 2 Comments

Invalidating the session in jsp

Let us create a Servlet Listener that just counts the number of running http sessions and prints the details whenever a session gets created or destroy. Http Session Listener; public class Session Listener implements Http Session Listener { private int session Count = 0; public void session Created(Http Session Event event) { synchronized (this) { session Count++; } println("Session Created: " + Session()Id()); println("Total Sessions: " + session Count); } public void session Destroyed(Http Session Event event) { synchronized (this) { session Count--; } println("Session Destroyed: " + Session()Id()); println("Total Sessions: " + session Count); } } method will be called by the servlet container whenever a new session is created for this application. Http Session Event class is passed as an argument to the session Created method.Starts eclipse and create a new dynamic web project with name Session Listener. I have selected Apache Tomcat v6.0, you can select any Tomcat version that you have installed. package net.viralpatel.servlet.listener; import javax. This object can be used to get the session related information including session ID.

Whenever a new session is created, this count gets incremented.

The method will be called by the servlet container whenever an existing session is invalidated.

We have used this method in our example to decrement the session count and display the ID of session being destroyed.

Open file from WEB-INF directory of your Project and add following entry for listener tag.

In this entry, we have added Session Listener class in Web xml.

Listener is one of the most popular technologies used in the J2EE web application.

It is part of the Java Servlet as defined in Servlet 2.3 but they have their own specific functionalities.There are two most widely used Servlet Listener i.e.Servlet Context Listener and Http Session Listener.Hence for every session creation and invalidation, the methods will be called by servlet container. Create three JSPs and copy following content into it. Check the console you will see the output that we print using We will create a small web application to test the functionality of Session Listener. Add users and see the list of users added and also check the console output to see the logs for session creation and session count.There will be 3 JSP files, will display a list of users. Once you click Destroy Session, the session gets invalidated and the session count is decremented.

  1. niche speed dating 27-Jun-2016 20:10

    Thanks to Asia Friendfinder, I’ve found my true love and look forward to a life time of happiness." Wendy T.