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


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="" xmlns="" xmlns:web="" xsi:schemaLocation="" version="2.5">

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()));

1 2 Next

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