Java for Web学习笔记(十九):Session(3)Session Listener
2016-04-12 21:43
639 查看
Session Listener
可以通过Listner来监听session的变化,这就是所谓的publish and subscribe模型。这是一种消息信息发布一方叫发布者,信息的接收方叫订阅者,实际也是事件驱动的高大上说法,订阅某个事件,然后触发处理。这种方式最大的作用是将进行session变化以及session变化后的处理进行,尤其适合于第三方代码(进行session变化),自己的代码处理session变化,例如SpringFramwork。
定义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 |
相关链接:
我的Professional Java for Web Applications相关文章
相关文章推荐
- Java第六次实验要求
- StandardServer.await: create[8005]: java.net.BindException: Address already in use: JVM_Bind java.ne
- java内存泄漏的定位与分析
- Spring IOC容器创建对象的方式
- 轻松搞定Struts 2:三步走上手小入门
- JavaCv 高效实现图像的遍历
- JAVA super() this() 详解
- java.lang.NullPointerException
- spring事务配置
- Spring集成Quartz定时任务例子
- Java注解全面解析(转)
- Java POI 导出EXCEL经典实现 Java导出Excel弹出下载框
- 使用内部类来实现Java的”多继承“
- java多线程编程关键字volatile,ThreadLocal和synchronized
- java hashcode
- Java学习基础入门
- java获取真实ip
- Struts2 入门学习笔记(二)——拦截器应用
- RabbitMQ Spring AMQP
- Java设计模式系列之迭代器模式