HttpSessionListener example

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 transac

HttpSessionListener example

HttpSessionListener example

     

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 ListenerSession
implements HttpSessionListener {
public ListenerSession() {
}
public void sessionCreated(HttpSessionEvent sessionEvent) {
// Get the session that was created
HttpSession session = sessionEvent.getSession();
// Store something in the session, and log a message
try {
System.out.println("Session created: "+session);
session.setAttribute("dog", "labrador");
session.setAttribute("name", "Diana");
} 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 breed of the dog is: " + session.getAttribute("dog"));
System.out.println("The name of the dog is : " + session.getAttribute("name"));
}
}

 

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class ServletListenerSession extends HttpServlet{
public void doGet(HttpServletRequest request, 
HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
HttpSession session= request.getSession();
String str = (String)session.getAttribute("dog");
String dogName = (String)session.getAttribute("name");
pw.println("The breed of the dog is " + str);
pw.println("The name of the dog is " + dogName);
}
}

The output of the example is given below:

Download this example.