How to define a HttpSessionBindingListener for a J2EE Web Application


HttpSessionBindingListener interface is used for tracking of session events on the object. If an object implements HttpSessionBindingListener interface, it will be aware of its association with a  HttpSession.

First create a class that implements HttpSessionBindingListener interface

package com.tunatore.listeners;

import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;

/**
 *
 * @author tunatore
 */
public class ObjectWillBeInSession implements HttpSessionBindingListener{

private String property1 = null;
private String property2 =null;

    @Override
    public void valueBound(HttpSessionBindingEvent event) {
        //code to run when ObjectWillBeInSession object associated with a http session
        System.out.println(“valueBound is working!”);
        System.out.println(“object’s property1 value –> ” + ((ObjectWillBeInSession)event.getValue()).getProperty1());
        System.out.println(“object’s property2 value –> ” + ((ObjectWillBeInSession)event.getValue()).getProperty2());
    }

    @Override
    public void valueUnbound(HttpSessionBindingEvent event) {
        //code to run when ObjectWillBeInSession object removed from a http session        
        //logging into a database server could be done here
        System.out.println(“valueUnbound is working!”);
        System.out.println(“object’s property1 value –> ” + ((ObjectWillBeInSession)event.getValue()).getProperty1());
        System.out.println(“object’s property2 value –> ” + ((ObjectWillBeInSession)event.getValue()).getProperty2());
    }

/**
     * @return the property1
     */
public String getProperty1() {
return property1;
}

   /**
     * @param property1 the property1 to set
     */
public void setProperty1(String property1) {
this.property1 = property1;
}

   /**
     * @return the property2
     */
public String getProperty2() {
return property2;
}

/**
     * @param property2 the property2 to set
     */
public void setProperty2(String property2) {
this.property2 = property2;
}

}

And define it in the web.xml file

<?xml version=”1.0″ encoding=”UTF-8″?>
<web-app version=”3.0″ xmlns=”http://java.sun.com/xml/ns/javaee&#8221; xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221; xsi:schemaLocation=”http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd”&gt;
    <listener>
        <description>HttpSessionBindingListener</description>
        <listener-class>com.tunatore.listeners.ObjectWillBeInSession</listener-class>
    </listener>
   <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
</web-app>

And last create a jsp file for testing listener class

<%–
    Document   : index
    Created on : 18.Jun.2011, 21:19:35
    Author     : tunatore
–%>

<%@page import=”com.tunatore.listeners.ObjectWillBeInSession”%>
<%@page contentType=”text/html” pageEncoding=”UTF-8″%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″>
<title>Testing HttpSessionBindingListeners</title>
</head>
<body>
<h1>Testing HttpSessionBindingListeners</h1>
        <%
            ObjectWillBeInSession owi = new ObjectWillBeInSession();
            owi.setProperty1(“I am a value for Property1”);
            owi.setProperty2(“I am a value for Property2”);
            //this will call HttpSessionBindingListener’s
            //valueBound method for the object
            session.setAttribute(“owi”, owi);

            //this will call HttpSessionBindingListener’s
            //valueUnbound method for the object
            session.removeAttribute(“owi”);        
        %>
</body>
</html>

The output of the program(taken from System.out)

valueBound is working!
object’s property1 value –> I am a value for Property1
object’s property2 value –> I am a value for Property2
valueUnbound is working!
object’s property1 value –> I am a value for Property1
object’s property2 value –> I am a value for Property2

Download Source

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s