HttpSessionBindingListener Example
Keywords : HttpSessionBindingListener Example, HttpSessionBindingListener Tutorial, How To Use HttpSessionBindingListener In A Web Application.,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

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