Using session listeners, write code to respond to an event when an object is added to a session, and write code to respond to an event when a session object migrates from one VM to another.

Object is added to session. Listener notification.

package listeners;

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

public final class SessionListener implements HttpSessionAttributeListener {

	public void attributeAdded(HttpSessionBindingEvent event) {
		System.out.println("attributeAdded");
		System.out.println(event.getSession().getId());
		System.out.println(event.getName() + "', '" + event.getValue());		
	}

	public void attributeRemoved(HttpSessionBindingEvent event) {
		System.out.println("attributeRemoved");
		System.out.println(event.getSession().getId());
		System.out.println(event.getName() + "', '" + event.getValue());
    }

    public void attributeReplaced(HttpSessionBindingEvent event) {
		System.out.println("attributeReplaced");
		System.out.println(event.getSession().getId());
		System.out.println(event.getName() + "', '" + event.getValue());
    }
}
					

<web-app>
	...
	<listener>
		<listener-class>listeners.SessionListener</listener-class>
	</listener>
</web-app>

					

Object is added to session. Object notification.

package beans;

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

public class CounterBean implements HttpSessionBindingListener {

	private int count = 1;

	public int getValue() {
		return count;
	}

	public void increment() {
		count++;
	}

	public void valueBound(HttpSessionBindingEvent event) {
		System.out.println("attributeBounded");
		System.out.println(event.getSession().getId());
		System.out.println(event.getName() + "', '" + event.getValue());		
	}

	public void valueUnbound(HttpSessionBindingEvent event) {
		System.out.println("attributeUnbounded");
		System.out.println(event.getSession().getId());
		System.out.println(event.getName() + "', '" + event.getValue());	
	}
}
					
Note, you don't need to (and must not) configure HttpSessionBindingListener in the deployment descriptor, just make a class implementing this interface.

Object migrates from one VM to another. Object notification.

package beans;

import javax.servlet.http.HttpSessionActivationListener;
import javax.servlet.http.HttpSessionEvent;

public class CounterBean implements HttpSessionActivationListener {

	private int count = 1;

	public int getValue() {
		return count;
	}

	public void increment() {
		count++;
	}

	public void sessionWillPassivate(HttpSessionEvent se) {
		System.out.println("session is about to be passivated");
		// save counter's value to persistent storage
		....
	}

	public void sessionDidActivate(HttpSessionEvent se) {
		System.out.println("session has just been activated");
		// retrieve counter's value from persistent storage
		....
	}
}
					
Note, you don't need to (and must not) configure HttpSessionActivationListener in the deployment descriptor, just make a class implementing this interface.

Professional hosting     Belorussian informational portal         Free SCBCD 1.3 Study Guide     Free SCDJWS 1.4 Study Guide     SCDJWS 1.4 Quiz     Free IBM Certified Associate Developer Study Guide     Free SCJP 5.0 (Tiger) Study Guide     Free Mock Exam Engine     IBM Test 000-287. Enterprise Application Development with IBM WebSphere Studio, V5.0 Study Guide