Before going into the details of the SessionListener we should firstly know about the sessions.



Before going into the details of the SessionListener we should firstly know about the sessions. As we know that Http protocol is a "stateless" protocol. The term stateless means that it can't persist the information. It can't remember the previous transactions. Whenever a client makes a request for any resources to the server, the server receives the request and processes the request and sends back the response. After sending the response the server closes the connection and forgets about the previous requests. Whenever a client sends any request to the server, the server treats each request as a new request. To remove this we have been provided the facility of the session. In session tracking whenever a client sends a request to the server then server creates a unique id for that request and sends back the unique id to the client along with the response object, now whenever a client sends a request to the server it also sends a unique id with it so that the server can know from where the request is coming. 

Listeners listen the events. Whenever any event occurs it is listened by the listener. The listener will be controller by the web servers. 

HttpSessionListener is an interface which extends java.util.EventListener class. The main purpose of this listener is to notify whenever there is a change in the list of active sessions in a web application 

This interface has  two methods:

  1. sessionCreated(HttpSessionEvent event): It will notify when the session is created.
  2. sessionDestroyed(HttpSessionEvent event): It will notify when the session gets invalidated.

In the above methods we can see that we have used HttpSessionEvent as the parameter of both the methods. This class has only one method getSession() which returns the current session.

The code of the program is given below:

Make the entry of this file in the deployment descriptor file that is web.xml

import javax.servlet.*;
import javax.servlet.http.*;

public class MySessionListener
implements HttpSessionListener {
public MySessionListener() {
public void sessionCreated(HttpSessionEvent sessionEvent) {
// Get the session 
HttpSession session = sessionEvent.getSession();
try {
System.out.println("Session created: "+session);
} catch (Exception e) {
System.out.println("Error in setting session attribute: " 
}+ e.getMessage());
public void sessionDestroyed(HttpSessionEvent sessionEvent) {
// Get the session that was invalidated
HttpSession session = sessionEvent.getSession();
// Log a message
System.out.println("Session invalidated: "+session);
System.out.println("The name is: " + session.getAttribute("foo"));


import javax.servlet.*;
import javax.servlet.http.*;

public class ServletSessionListener extends HttpServlet{
public void doGet(HttpServletRequest request, 
HttpServletResponse response) throws ServletException, IOException
PrintWriter pw = response.getWriter();
HttpSession session= request.getSession();
String str = (String)session.getAttribute("foo");
pw.println("The name is " + str);

The output of the program is given below:


Download this example.

Share on Google+Share on Google+


Posted on: July 19, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: HttpSessionListener   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
September 6, 2012
I coundn't execute this program

how to execute this program while executing am getting this error:Allocate exception for servlet MySessionListener java.lang.ClassCastException: MySessionListener cannot be cast to javax.servlet.Servlet