logo

HttpSessionEvent And HttpSessionListener


Show

The HttpSessionEvent is notified when the session object is modified . The corresponding Listener interface for this event is HttpSessionListener.

We can perform some operations at this event like counting total and current logged-in users, maintaining a log of user details like login time, logout time etc.

Methods of HttpSessionListener interface

The HttpSessionListener interface has two methods declared inside it which must be implemented by the servlet programmer to perform some action.

  1. public void sessionCreated(HttpSessionEvent e): is invoked when a session object is made .
  2. public void sessionDestroyed(ServletContextEvent e): is invoked when the session is invalidated.

HttpSessionEvent and HttpSessionListener to count total and current logged-in users has Example which are given below

In this example, are counting the entire and current logged-in users. For this purpose, we've created four files:

  1. index.html: to urge input from the user.
  2. MyListener.java: A listener class that counts total and current logged-in users and stores this information in ServletContext object as an attribute.
  3. First.java: A Servlet class that makes a session and prints the entire and current logged-in users.
  4. Logout.java: A Servlet class that invalidates a session.

Index.html

<form action="servlet1">  
Name:<input type="text" name="username"><br>  
Password:<input type="password" name="userpass"><br>  
  

<input type="submit" value="login"/>  
</form> 

MyListener.java

import javax.servlet.ServletContext;  
import javax.servlet.http.HttpSessionEvent;  
import javax.servlet.http.HttpSessionListener;  
  
public class CountUserListener implements HttpSessionListener{  
    ServletContext ctx=null;  
    static int total=0,current=0;      

    public void sessionCreated(HttpSessionEvent e) {  
    total++;  
    current++;  
      
    ctx=e.getSession().getServletContext();  
    ctx.setAttribute("totalusers", total);  
    ctx.setAttribute("currentusers", current);      

    }  
  
    public void sessionDestroyed(HttpSessionEvent e) {  
       current--;  
       ctx.setAttribute("currentusers",current);  
    }    

}  

First.java

import java.io.IOException;  
import java.io.PrintWriter;  

import javax.servlet.ServletContext;  
import javax.servlet.ServletException;  
import javax.servlet.http.HttpServlet;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
import javax.servlet.http.HttpSession;  

public class First extends HttpServlet {  
public void doGet(HttpServletRequest request,  
 HttpServletResponse response)  
    throws ServletException, IOException {    

        response.setContentType("text/html");  
        PrintWriter out = response.getWriter();        

        String n=request.getParameter("username");  
        out.print("Welcome "+n);           

        HttpSession session=request.getSession();  
        session.setAttribute("uname",n);            

        //retrieving data from ServletContext object  
        ServletContext ctx=getServletContext();  
        int t=(Integer)ctx.getAttribute("totalusers");  
        int c=(Integer)ctx.getAttribute("currentusers");  
        out.print("
total users= "
+t); out.print("
current users= "
+c); out.print("
logout"
); out.close(); } }

Logout.java

import java.io.IOException;  
import java.io.PrintWriter;    

import javax.servlet.ServletException;  
import javax.servlet.http.HttpServlet;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
import javax.servlet.http.HttpSession;  
  

public class LogoutServlet extends HttpServlet {  
public void doGet(HttpServletRequest request,  
 HttpServletResponse response)  
        throws ServletException, IOException {  

        response.setContentType("text/html");  
        PrintWriter out = response.getWriter();       

        HttpSession session=request.getSession(false);  
        session.invalidate();//invalidating session           

        out.print("You are successfully logged out");                  
        out.close();  
    }   

}  

download this example