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. }

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

HttpSessionBindingListener和HttpSessionAttributeListener区别的更多相关文章

  1. HttpSessionBindingListener和HttpSessionAttributeListener区别 - gengkunpeng的专栏 - 博客频道 - CSDN.NET

    分享到 一键分享 QQ空间 新浪微博 百度云收藏 人人网 腾讯微博 百度相册 开心网 腾讯朋友 百度贴吧 豆瓣网 搜狐微博 百度新首页 QQ好友 和讯微博 更多... 百度分享 HttpSession ...

  2. <Listener>HttpSessionListener和HttpSessionAttributeListener区别

    一.HttpSessionListener HttpSessionListener是对Session的一个监听,主要监听关于Session的两个事件,即初始化和销毁.HttpSessionListen ...

  3. EL&Filter&Listener:EL表达式和JSTL,Servlet规范中的过滤器,Servlet规范中的监听器,观察着设计模式,监听器的使用,综合案例学生管理系统

    EL&Filter&Listener-授课 1 EL表达式和JSTL 1.1 EL表达式 1.1.1 EL表达式介绍 *** EL(Expression Language):表达式语言 ...

  4. 在Java Web程序中使用监听器可以通过以下两种方法

    之前学习了很多涉及servlet的内容,本小结我们说一下监听器,说起监听器,编过桌面程序和手机App的都不陌生,常见的套路都是拖一个控件,然后给它绑定一个监听器,即可以对该对象的事件进行监听以便发生响 ...

  5. 《Head First Servlets & JSP》-5-属性和监听

    Servlet初始化参数 在DD文件(web.xml)中 Servlet参数在/参数下: 在Servlet代码中 在Servlet初始化之前不能使用Servlet初始化参数 一旦有了一个Servlet ...

  6. servlet/filter/listener/interceptor区别与联系

    转自:http://www.cnblogs.com/doit8791/p/4209442.html servlet.filter.listener是配置到web.xml中(web.xml 的加载顺序是 ...

  7. servlet、filter、listener、interceptor之间的区别和联系

    一.概念 1.servlet:servlet是一种运行服务器端的java应用程序,具有独立于平台和协议的特性,并且可以动态的生成web页面,它工作在客户端请求与服务器响应的中间层. 2.filter: ...

  8. filter listener interceptor的区别

    转自: http://www.cnblogs.com/shangxiaofei/p/5328377.html https://www.cnblogs.com/jinb/p/6915351.html 一 ...

  9. 【转】servlet/filter/listener/interceptor区别与联系

    原文:https://www.cnblogs.com/doit8791/p/4209442.html 一.概念: 1.servlet:servlet是一种运行服务器端的java应用程序,具有独立于平台 ...

随机推荐

  1. bash shell for循环

    1 同c一样用四个空格进行缩进 2 每行一条语句,不用分号 3 不用大括号标识代码块,但是要用do/done来标识代码块 4 用双小括号,类似于c的for进行编码 for ((i=1; i<=1 ...

  2. js获取指定字符后面的字符

    function getCaption(obj){ var index=obj.lastIndexOf("\-"); obj=obj.substring(index+1,obj.l ...

  3. 三连击 P1008 洛谷 python写法

    三连击 P1008 洛谷 题意 将\(1,2, \cdots,9\)共9个数分成3组,分别组成3个三位数,且使这3个三位数构成1:2:3的比例,试求出所有满足条件的3个三位数. 解题思路 这里我使用了 ...

  4. C++ 函数返回对象时并没有调用拷贝构造函数

    #include <iostream> #include <vector> #include <string.h> using namespace std; cla ...

  5. 洛谷 - P4114 - Qtree1 - 重链剖分

    https://www.luogu.org/problem/P4114 维护边权的话,用深度大的点表示这条边(可以遍历一边边询问两端深度,这样不需要修改dfs1,也可以在dfs1的时候向下走的同时把边 ...

  6. ssm框架搭建整合测试

    下载各种jar包 mybatis下载 https://github.com/mybatis/mybatis-3/releases mysql驱动下载 http://mvnrepository.com/ ...

  7. Echarts数据可视化grid直角坐标系(xAxis、yAxis)详解:

    mytextStyle={ color:"#333", //文字颜色 fontStyle:"normal", //italic斜体 oblique倾斜 font ...

  8. JavaScript、ES6中类的this指向问题

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. JavaScript——call() 方法

    function Product(name, price) { this.name = name; this.price = price; } function Food(name, price) { ...

  10. CentOS7系统局域网内配置本地yum源解决cannot find a valid baseurl for repo

    一.     问题详情 因为服务器无法连接外网,所有直接用yum安装某些功能将受到影响,报错如下: Error: Cannot find a valid baseurl for repo: base ...