Listener开发(选讲).ppt

第三部分(续)Listener开发 内容: (1)Listener的定义和作用, (2)配置Listener (3)istener的分类以及常用的Listener,如ServletContext Listener和HttpSession Listener。 目标: 熟练掌握如何定义并配置Listener 熟练掌握ServletContextListener接口的实现和作用 熟练掌握ServletContextAttributeListener接口的实现和作用 熟练掌握HttpSessionListener接口的实现和作用 熟练掌握HttpSessionAttributeListener接口的实现和作用 12.1 Listener简介 12.2 ServletContext有关的Listener接口 12.2.1 ServletContextListener接口 12.2.2 ServletContextAttributeListener接口 12.3 HttpSession有关的Listener接口 12.3.1 HttpSessionListener接口 12.3.2 HttpSessionAttributeListener接口 12.4 综合练习 12.5 小结 Listener的中文译名为监听器,从字面上可以看出Listener主要用来监听之用。通过Listener可以监听容器中某一执行动作,并根据其要求做出相应的响应。 到目前Servlet 2.4开始,一共包含八个Listener接口,可以将其分为三类,分别如下。 第一类:与ServletContext有关的Listener接口(2个) ServletContextListener、ServletContextAttributeListener。 第二类:与HttpSession有关的Listener接口(4个) HttpSessionListener、HttpSessionAttributeListener、HttpSessionBindingListener和HttpSessionActivationListener。 第三类:与ServletRequest有关的Listener接口(2个) ServletRequestListener和ServletRequestAttributeListener。 ServletContext 有关的Listener包含两个Listener接口,分别是 ServletContextListener和ServletContextAttributeListener。 其中ServletContextListener接口用来实现ServletContext的启动和销毁监听;ServletContextAttributeListener接口用来实现application范围属性变化的监听。 ServletContextListener接口用来实现ServletContext的启动和销毁监听。 该接口中包含两个方法, contextInitialized()方法,用来监听ServletContext的启动和初始化; contextDestroyed()方法,用来监听ServletContext的销毁。 在这两个方法中还包含一个参数sce,其类型为ServletContextEvent。通过ServletContextEvent对象的getServletContext()方法可以获得ServletContext对象。 示例代码 MyServletContextListener.java ServletContextAttributeListener接口用来实application范围属性变化的监听。 该接口中包含三个方法, attributeAdded()方法,监听application范围属性的添加; attributeReplaced()方法,监听application范围属性的替换; attributeRemoved()方法,监听application范围属性的移除。 在这两个方法中还包含一个参数scab,其类型为 ServletContextAttributeEvent。通过ServletContextAttributeEvent对象的getName()方法可以获得属性的名称;通过ServletContextAttributeEvent对象的getValue()方法可以获得属性的值。 示例代码 MyServle

文档评论(0)

1亿VIP精品文档

相关文档