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.
|
|
Hosting provided by PerfoHost: KVM VPS. Unix VPS. Windows VPS. VPN. Domains. Dedicated servers. Colocation.