您的位置:首页 > 编程语言 > Java开发

Java for Web学习笔记(十九):Session(3)Session Listener

2016-04-12 21:43 609 查看

Session Listener

可以通过Listner来监听session的变化,这就是所谓的publish and subscribe模型。这是一种消息信息发布一方叫发布者,信息的接收方叫订阅者,实际也是事件驱动的高大上说法,订阅某个事件,然后触发处理。这种方式最大的作用是将进行session变化以及session变化后的处理进行,尤其适合于第三方代码(进行session变化),自己的代码处理session变化,例如Spring
Framwork。

定义Listener和监控session的变化

有三种方式。

(1)annotation:@webListener

@WebListener
public class SessionListener implements HttpSessionListener, HttpSessionIdListener {
}

(2)在web.xml中定义

<listener>
<listener-class>com.wrox.SessionListener</listener-class>
</listener>

(3)通过addListener()在代码中注册

addListener()方法只能在ServletContextListener的contextInitialized()或者ServletContainerInitializer’的onStartup()中调用。

代码例子如下:

@WebListener
public class SessionListener implements HttpSessionListener, HttpSessionIdListener {
public SessionListener() {
// TODO Auto-generated constructor stub
}

/**
* @see HttpSessionListener#sessionCreated(HttpSessionEvent)
*/
public void sessionCreated(HttpSessionEvent event)  {
System.out.println(this.date() + ": Session " + event.getSession().getId() + " created.");
}

/**
* @see HttpSessionIdListener#sessionIdChanged(HttpSessionEvent, String)
*/
public void sessionIdChanged(HttpSessionEvent event, String oldSessionId)  {
System.out.println(this.date() + ": Session " + oldSessionId + " changed to " + event.getSession().getId());
}

/**
* @see HttpSessionListener#sessionDestroyed(HttpSessionEvent)
*/
public void sessionDestroyed(HttpSessionEvent event)  {
System.out.println(this.date() + ": Session " + event.getSession().getId() + " destroyed.");
}

private SimpleDateFormat formatter = new SimpleDateFormat("EEE, MMMDD日 yyyy HH:mm:ss");

private String date(){
return this.formatter.format(new Date());
}
}

HttpSessionBindingListener

在Session的Listener中,HttpSessionBindingListener比较特别,不需要进行Listner的定义,无论是xml还是annotation,都不需要。在我们的实验中,如果加上,运行起来也没有问题,前面的话,可以修正为“可以不需要进行Listner的定义”。

如果一个类实现HttpSessionBindingListener接口,当它的一个实例作为Attribute加入到session中,触发接口的valueBound()方法,反之,当作为session的attribute的实例被删除,将触发valueUnbound()方法。



图是Eclipse中给出的Listener,其中和session有关的有五个接口。之前已经测试了HttpSessionListener和HttpSessionIdListener。此处,我们关注HttpSessionBindingListener。例子代码如下:

public class Foo implements HttpSessionBindingListener {
   ……
public void valueBound(HttpSessionBindingEvent event)  {
// getName()将获得Session Attribute的名字
System.out.println("valueBound : " + event.getName());
// 我们将看到getSession()和getSource()指向同一个对象,即添加本类实例的session
System.out.println(event.getSession());
System.out.println(event.getSource());
// 我们将看到getValue()就是本类的对象,即this
System.out.println(this);
System.out.println(event.getValue());
}

public void valueUnbound(HttpSessionBindingEvent event)  {
System.out.println("valueUnbound : " + event.getName());
}
}

在session中执行setAttribute(),将触发Foo中的valueBound(),例子如下

HttpSession session = request.getSession();
if(session.getAttribute("FooHello") == null)
session.setAttribute("FooHello",new Foo());

我们看看console的输出:

valueBound : FooHello
org.apache.catalina.session.StandardSessionFacade@e51115
org.apache.catalina.session.StandardSessionFacade@e51115
cn.wei.flowingflying.chapter05.Foo@19b1a96
cn.wei.flowingflying.chapter05.Foo@19b1a96

如果我们执行session.removeAttribute("FooHello");将会触发Foo中的valueUnbound()。

相关链接:
我的Professional Java for Web Applications相关文章
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: