Sunday, June 23, 2013

HTTP Session Listeners

Event Listeners enable you to track key events in your Web applications.

In Java EE 6 typically there are 3 levels/types of Listeners:
  • Servlet Context
  • HTTP Session
  • Servlet Request
In this post we'll learn about HTTP Session Listeners. All events which are related to sessions are handled by session Listeners. The table below lists the Event and the corresponding Listener interfaces.

Events Listener Interface
Lifecycle javax.servlet.http.HttpSessionListener
Attribute Change javax.servlet.http.HttpSessionAttributeListener
Session Migration javax.servlet.http.HttpSessionActivationListener
Object Binding javax.servlet.http.HttpSessionBindingListener

Session Migration is itself a big topic and will be discussed in another post.

Lifecycle
This deals with the Lifecycle of a session, i.e, session creation and destruction.

package com.web;

import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

@WebListener // Declares class as a Web Listener
public class SessionLifiecycle implements HttpSessionListener {

   /**
    * Called on Server startup
    */
    public SessionLifiecycle() {
        System.out.println("Listener Ready!");
    }

    /**
     * Called when session is created, 
     * for e.g a call to request.getSession();
     */
    public void sessionCreated(HttpSessionEvent sessionEvent) {
        System.out.println("Created: " + sessionEvent.getSession().getId());
    }

    /**
     * Called when session is destroyed, 
     * for e.g a call to request.getSession().invalidate();
     */
    public void sessionDestroyed(HttpSessionEvent sessionEvent) {
        System.out.println("Destroyed: " + sessionEvent.getSession().getId());
    } 
}

Output
Listener Ready!

Created: 325C3E174A7AD4DE8163544AD1E767ED
Destroyed: 325C3E174A7AD4DE8163544AD1E767ED

Attribute Change
This deals with change in session attributes. Event is fired when a attribute is added, replaced or removed.

package com.web;

import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;

@WebListener
public class SessionAttributes implements HttpSessionAttributeListener {

    /**
     * Called on Server startup
     */
    public SessionAttributes() {
     System.out.println("Attribute Listener Ready!");
    }

    /**
     * Called when session attribute is added,
     * request.getSession().setAttribute("attr1", "1");
     */
    public void attributeAdded(HttpSessionBindingEvent se) {
     System.out.println("Attribute Added: " + se.getName());
    }

    /**
     * Called when session attribute is replaced, value of existing attribute is changed
     * request.getSession().setAttribute("attr1", "2"); 
     */
    public void attributeReplaced(HttpSessionBindingEvent se) {
     System.out.println("Attribute Replaced: " + se.getName());
    }
    
    /**
     * Called when session attribute is removed, 
     * request.getSession().removeAttribute("attr1");
     */
    public void attributeRemoved(HttpSessionBindingEvent se) {
     System.out.println("Attribute Removed: " + se.getName());
    }
}

Output
Attribute Listener Ready!

Attribute Added: attr1; value: 1
Attribute Replaced: attr1; value: 1       // Value changes only after listener execution
Attribute Removed: attr1; value: 2

Object Binding
This notifies an object is being bound or unbound to/from a session.

package com.web;

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

// @WebListener This is not required
public class SessionObjectBinding implements HttpSessionBindingListener {

    public SessionObjectBinding() {
        System.out.println("Binding Object created!");
    }

    /**
     * Set the value of this object
     */
    @Override
    public String toString() {
        return "Constant value";
    }

    /**
     * Called when this object is bound to a session.
     * request.getSession().setAttribute("object1", new SessionObjectBinding());
     */
    public void valueUnbound(HttpSessionBindingEvent event) {
        System.out.println("Value Unbound: " + event.getName() + "; Value: "
                + event.getValue());
    }

    /**
     * Called when this object is unbound from a session.
     * request.getSession().removeAttribute("object1");
     */
    public void valueBound(HttpSessionBindingEvent event) {
        System.out.println("Value Bound: " + event.getName() + "; Value: "
                + event.getValue());
    }
}

Output
When an object is bound to the session an attribute is also added so attribute Listener is also called.
Binding Object created!

Value Bound: object1; Value: Constant value
Attribute Added: object1; value: Constant value

Value Unbound: object1; Value: Constant value
Attribute Removed: object1; value: Constant value

No comments:

Post a Comment