HttpSessionBindingListener和HttpSessionAttributeListener区别
?? HttpSessionBindingListener和HttpSessionAttributeListener是两个经常让初学者弄混的监听器,其实它们有很大的区别。这2个监听器在文章中简称为BindingListener和AttributeListener.
?? ?? 1.BindingListener有2个方法,valueBound(HttpSessinBindingEvent)和valueUnbount(HttpSessionBindingEvent)。实现BindingListener接口的对象被绑<script type="text/javascript"></script><script type="text/javascript"></script><script type="text/javascript"></script><script type="text/javascript"></script><script type="text/javascript"></script>定到session时触发valueBound事件,解除绑定时触发valueUnbound事件。举例来说:
?
AttributeListener<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->MyListener implements HttpSessionAttributeListener{ attributeAdded(HttpSessionBindingEvenet event) { System.out.println("有对象加入session中"); } attributeRemoved(HttpSessionBindingEvent event) { System.out.println("有对象从session中移除"); } attributeReplaced(HttpSessionBindingEvent event) { System.out.println("属性值改变"); }}?
??????OtherObject other = new OtherObject();
??????当有对象添加到session中时,session.setAttribute("object",other)触发attributeAdded事件,
??????当该对象从session移除时,session.removeAttribute("object")触发attriubteRemoved事件,
????? 当该属性的值发生变化时, ?session.replaceAttribute("object",another)触发attributeRepalced事件。
????? 注意:只要有对象保存到session中或从session中移除或改变属性的值都会触发相应事件,不论该对象是否实现了AttributeListener。
?
????? 总结:
??????1.只有实现了HttpSessionBindingListener的类,在和session绑定、解除绑定时触发其事件。
??????2.实现了HttpSessionAttributeListener后,任何对象(不论其是否实现了AttributeListener)在变化时均触发对应的事件。