已阅读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中国制造业行业市场现状供需分析及投资评估发展研究
- 2026中国新能源汽车电机电控技术发展与市场前景规划
- 2026中国通信基站市场现状供需分析及投资评估规划分析研究报告
- 自动化生产现场-5S-与定置管理手册
- 2026食品加工业发展趋势预测与投资策略解析报告
- 2026中国涡流泵定制化服务发展现状与柔性生产能力建设
- 2026中国现代农业技术应用与市场需求分析及竞争投资评估规划发展研究报告
- 2026中国云计算服务市场规模市场供需研究投资评估规计划划分析研究报告
- 2026中国智能座舱多模态交互系统用户体验评价体系与市场需求演变研究
- 2026中国智能仓储分拣机器人故障率降低与维护成本报告
- 秋天行车安全教育课件
- GB/T 17473-2025电子浆料性能试验方法导体浆料测试
- EDSS神经功能状况评估
- 数学小讲师课件
- 福建省初级注安考试试题及答案(2025年)
- 水力发电运行值班员作业指导书
- GJB827B--2020军事设施建设费用定额
- 种植义齿制作技术
- 2025至2030年中国家用美容电器具行业发展监测及投资前景预测报告
- 2025卫生职称疾病控制正高高级职称历年考试试题及答案
- 患者健康教育方法及技巧
评论
0/150
提交评论