zoukankan      html  css  js  c++  java
  • HttpSessionBindingListener和HttpSessionAttributeListener区别

    637人阅读 评论(0) 收藏 举报

     HttpSessionBindingListener和HttpSessionAttributeListener是两个经常让初学者弄混的监听器,其实它们有很大的区别。这2个监听器在文章中简称为BindingListener和AttributeListener.

        1.BindingListener有2个方法,valueBound(HttpSessinBindingEvent)和valueUnbount(HttpSessionBindingEvent)。实现BindingListener接口的对象被绑 定到session时触发valueBound事件,解除绑定时触发valueUnbound事件。举例来说:

    1. public class UserObject implements HttpSessionBindingListener{      
    2.         public void valueBound(HttpSessionBindingEvent event){          
    3.             System.out.println("触发绑定事件!");      
    4.             }      
    5.         public void valueUnbound(HttpSessionBindingEvent event){          
    6.             System.out.println("解除和session的绑定");      
    7.               
    8.         }  

    UserObject user = new UserObject();

    当把该监听器保存到session中,session.setAttribute("user",user)时就会触发valueBound事件.

    当该监听器从session中移除时即session.removeAttribute("user"),触发valueUnbound事件;session失效或超时

    时也会触发valueUnbound事件。

    注意:只有该监听器(UserObject)保存到session中或从session移除时才会触发事件,其他没有实现该listener对象保存到session时不会触发该事件。

        2.AttributeListener接口有3个方法,attributeAdded(HttpSessionBindingEvent),attributeRemoved(HttpSessionBindingEvent),

    attributeReplaced(HttpSeesionEvent)。当在session中添加、移除或更改属性值时会触发相应的事件。

    例子:

    1. MyListener implements HttpSessionAttributeListener{      
    2.     attributeAdded(HttpSessionBindingEvenet event){          
    3.         System.out.println("有对象加入session中");      
    4.         }       
    5.     attributeRemoved(HttpSessionBindingEvent event){           
    6.         System.out.println("有对象从session中移除");       
    7.         }       
    8.     attributeReplaced(HttpSessionBindingEvent event){            
    9.         System.out.println("属性值改变");        
    10.         }  
    11.     }  

    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)在变化时均触发对应的事件。

    更多 0
  • 相关阅读:
    hdoj2187:悼念512汶川大地震遇难同胞 (贪心)
    2.0其它之Transform详解,以及UIElement和FrameworkElement的常用属性
    2.0外观之样式, 模板, 视觉状态和视觉状态管理器
    2.0图形之Ellipse, Line, Path, Polygon, Polyline, Rectangle
    2.0控件之ListBox, MediaElement, MultiScaleImage, PasswordBox, ProgressBar, RadioButton
    2.0画笔之SolidColorBrush, ImageBrush, VideoBrush, LinearGradientBrush, RadialGradientBrush
    2.0图形之基类System.Windows.Shapes.Shape
    2.0交互之鼠标事件和键盘事件
    2.0控件之ScrollViewer, Slider, StackPanel, TabControl, TextBlock, TextBox, ToggleButton
    2.0交互之InkPresenter(涂鸦板)
  • 原文地址:https://www.cnblogs.com/redcoatjk/p/3878039.html
Copyright © 2011-2022 走看看