已阅读5页,还剩16页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第7章 监听器Listener Java Web程序设计基础 7.1 Listener概述 n什么是Listener nListener的使用 7.1.1 什么是Listener nWeb应用程序员可以利用Listener接 口,监听在Container中某一个执行程 序,并且根据其应用程序的需求做出 适当的响应 7.1.2 Listener的使用 n使用Listener需要实现相应的 Listener接口 nListener需要在web.xml中配置才生效 7.2 Listener的分类 n监听对象的创建与销毁 n实例:监听Session、Request与 ServletContext n监听对象的属性变化 n监听Session内的对象 Listener 接口简介 Listener 接口简介 Listener接口Event类 ServletContextListenerServletContextEvent ServletContextAttributeListe ner ServletContextAttributeEvent HttpSessionListenerHttpSessionEvent HttpSessionActivationListen er HttpSessionBindingListenerHttpSessionBindingEvent HttpSessionAttributeListener ServletRequestListenerServletRequestEvent ServletRequestAttributeListe ner ServletRequestAttributeEven t 7.2.1 监听对象的创建与销毁 nHttpSessionListener: 监听Session的创建与销毁 nServleContextListener: 监听Context的创建与销毁 nServletRequestListener: 监听request的创建与销毁 HttpSessionListener n和ServletContextListener接口类似 ,当有Session产生或是消失,会自动 调用: sessionCreated和 sessionDestroyed方法 ServletContextListener n一个实现ServletContextListener接 口的程序,当Container启动时,程序 会自动开始监听的工作,它首先会调 用contextInitialized()接收对应的 javax.servlet.ServletContextEvent 事件 ServletRequestListener nServletRequestListener是Servlet 2.4的新功能 n该接口和ServletContextListener接 口类似,当有请求产生或消失,会自 动调用requestInitialized()和 requestDestroyed(). 7.2.2 实例:监听Session、Request与ServletContext nJava中的类可以实现多个接口,因此 实现Listener时可以用一个类实现多 个Listener接口。这样的好处是多种 Listener一块儿工作,使用同一个类 的资源 public class ListenerTest implements HttpSessionListener, ServletContextListener, ServletRequestListener 7.2.3 监听对象的属性变化 nListener用于监听session、request 、context的属性变化 ServletContextAttributeListener HttpSessionAttributeListener ServletRequestAttributeListener ServletContextAttributeListener n一个实现 ServletContextAttributeListener接 口的程序,它能够监听Application范 围的变化 HttpSessionAttributeListener n和ServletContextAttributeListener接口 类似,只不过该接口是监听Session范围的 变化的 nHttpSessionAttributeListener和 HttpSessionBindingListener两个功能很 相似,但是也有一些不同点: HttpSessionAttributeListener需要在web.xml 中设定。 HttpSessionAttributeListener监听web站点所 有Session范围的变化;而 HttpSessionBindingListener只单纯监听实现 它的类 ServletRequestAttributeListener nServletRequestAttributeListener也 是Servlet2.4的新功能 n该接口和 ServletContextAttributeListener接 口功能相似,只不过该接口监听 Request范围的变化 7.2.4 监听Session内的对象 n主要包括: HttpSessionBindingListener HttpSessionActivationListener HttpSessionBindingListener n当我们实现这个接口后,只要对象加 入Session范围或从Session范围中移 除时,Container分别会自动调用下列 两个方法: valueBound(HttpSessionBindingEvent event) valueUnbound(HttpSessionBindingEvent event) HttpSessionActivationListener n该接口主要用于:同一个Session转移至不 同JVM的情形,例如:负载平衡(Load Balancing)机制。 n当Session被存储起来,并且等待转移至另 一个JVM,这段时间成为失效状态,若 Session中的属性对象实现该接口时, Container会自动调用 sessionWillPassivate方法
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 湖南省湘潭市岳塘区湘钢二中2026-2027学年七年级上学期英语期中考试题(含答案)
- 贵州省黔西南布依族苗族自治州册亨县三校联考2026-2027学年高二上学期11月期中生物试卷(含答案)
- 雨水管网工程施工方案
- 印刷企业生产管理记录台账
- 新能源电站建设工程技术监督制度
- 预制T梁支座安装试验检测报告
- 中小学阅读教育的策略与实施路径
- 校级小课题研究管理工作制度
- 吉林2027年3月高三历史质量检测试卷及答案
- 山东2026年11月初中九年级道德与法治期中试卷及答案
- 2026年中医经典考试题库(含答案)
- 2026中国柔性显示屏折叠终端市场消费者接受度调研报告
- 2026年纪检监察业务模拟试题(含答案)
- 2026年心肺康复学习题库(含答案)
- DL-T 5220-2021《10kV及以下架空配电线路设计规范》
- 110kV变电站可行性研究报告
- (2026全考点)N1叉车司机证理论模拟考试有答案
- 市政工程施工项目部安全资料目录
- 2026年江苏省普通高中学业水平合格性考试物理学科必修第三册必考知识点清单
- 幼儿园诚信工作制度
- 雨课堂学堂在线学堂云《人工智能时代的创新思维(北京理工)》单元测试考核答案
评论
0/150
提交评论