HttpSessionBindingListener Example

HttpSessionBindingListener Example explains about how to use HttpSessionBindingListener in a web application.

The listener HttpSessionBindingListener is an interface extending from base interface java.util.EventListener interface. This interface will notify an object when it is bound to or unbound from a session.

javax.servlet.http.HttpSessionBindingListener interface has following two methods:

valueBound(HttpSessionBindingEvent event).
valueUnBound(HttpSessionBindingEvent event).

You can see the below example, which is demonstrating HttpSessionBindingListener Example

Package Structure

HttpSessionBindingListener Example

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  <listener>
    <listener-class>com.listener.HttpSessionAttributeListenerExample</listener-class>
  </listener>
</web-app>

HttpSessionBindingListenerExample.java

package com.listener;

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

public class HttpSessionBindingListenerExample implements  HttpSessionBindingListener {
 
ServletContext context;

 
public HttpSessionBindingListenerExample(ServletContext context) {
   
this.context = context;
 
}

 
public void valueBound(HttpSessionBindingEvent event) {
   
context.log("The value bound is " + event.getName());
 
}

 
public void valueUnbound(HttpSessionBindingEvent event) {
   
context.log("The value unbound is " + event.getName());
 
}
}

Invoke HttpSessionListener From Servlet


// Get the current session object, create one if necessary
HttpSession session = req.getSession();
// Add a HttpSessionBindingListenerExample
session.setAttribute("name",new HttpSessionBindingListenerExample(getServletContext()));
session.removeAttribute("name");

Note

From Servlet API 3.0, we can use javax.servlet.annotation, so that web.xml is not necessary

Here we are using @WebListener annotation

HttpSessionBindingListenerExample.java

package com.listener;

import javax.servlet.*;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.*;

@WebListener
public class HttpSessionBindingListenerExample implements HttpSessionBindingListener {
   
ServletContext context;

   
public HttpSessionBindingListenerExample() {

    }

   
public HttpSessionBindingListenerExample(ServletContext context) {
       
this.context = context;
   
}

   
public void valueBound(HttpSessionBindingEvent event) {
       
System.out.println("The value bound is " + event.getName());
   
}

   
public void valueUnbound(HttpSessionBindingEvent event) {
       
System.out.println("The value unbound is " + event.getName());
   
}
}

ListenerServletExample.java

package com.listener;

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

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


/**
* This servlet is used for showing an example about sessionListener
*/

@WebServlet("/ListenerServletExample")
public class ListenerServletExample extends HttpServlet {
   
private static final long serialVersionUID = 1L;

  
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      
      
HttpSession session = request.getSession(); //sessionCreated() method is invoked
      
session.setAttribute("name",new HttpSessionBindingListenerExample(getServletContext()));
       session.removeAttribute
("name");

      
       PrintWriter out = response.getWriter
();
       out.append
("Session Created Successfully");
   
}
}

Tomcat Console Output

The value bound is name
The value unbound is name

 









Your email address will not be published. Required fields are marked *