已阅读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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 黑龙江省哈尔滨市某中学2024-2025学年高二年级上册期末考试生物试卷(含答案)
- 2.1《立在地球边上放号》导学案(含答案)2024-2025学年统编版高中语文必修上册
- 文化适应干预效果分析-洞察与解读
- (2025年)(完整)污水处理工初级工考试试题库及参考答案
- 产品变更评估报告
- 国家开放大学电大本科【国际私法】2025年期末试题及答案
- 矿物质营养科普
- 湖南长沙某中学2025-2026学年高三年级上册9月月考化学试题(解析版)
- (2025年)铁路旅客运输考试题库及答案
- 2025年全国科技工作者日科普知识竞赛试题及答案
- 中医护理操作悬灸方法
- 坛紫菜人工育苗技术规范
- 金融机构内部审计工作手册
- 油料物资保障方案(3篇)
- 农药中毒护理流程
- CJ/T 120-2016给水涂塑复合钢管
- T/CECS 10214-2022钢面镁质复合风管
- 2 集体生活成就我 公开课一等奖创新教案-5
- 家庭作业布置及小学数学改进措施
- GB/T 45484-2025船舶与海上技术接收船上加注燃料质量流量计要求
- 2025年上海奉贤投资集团有限公司招聘笔试参考题库含答案解析
评论
0/150
提交评论