servlet的大监听器.docx

上传人:小飞机 文档编号:3165711 上传时间:2023-03-11 格式:DOCX 页数:4 大小:37.60KB
返回 下载 相关 举报
servlet的大监听器.docx_第1页
第1页 / 共4页
servlet的大监听器.docx_第2页
第2页 / 共4页
servlet的大监听器.docx_第3页
第3页 / 共4页
servlet的大监听器.docx_第4页
第4页 / 共4页
亲,该文档总共4页,全部预览完了,如果喜欢就下载吧!
资源描述

《servlet的大监听器.docx》由会员分享,可在线阅读,更多相关《servlet的大监听器.docx(4页珍藏版)》请在三一办公上搜索。

1、servlet的大监听器JSP/Servlet 中的事件处理写过AWT或Swing程序的人一定对桌面程序的事件处理机制印象深刻:通过实现Listener接口的类可以在特定事件(Event)发生时,呼叫特定的方法来对事件进行响应。 其实我们在编写JSP/Servle程序时,也有类似的事件处理机制,所不同的是在JSP/Servlet中是在web.xml中注册Listener,由Container在特定事件发生时呼叫特定的实现Listener的类。 1. Servlet中的Listener和Event: 在JSP 2.0/Servlet 2.4中,共有八个Listener接口,六个Event类别。

2、ServletContextListener接口 接口方法 contextInitialized与 contextDestroyed 接收事件 ServletContextEvent 触发场景 在Container加载Web应用程序时,会呼叫contextInitialized,而当容器移除Web应用程序时,会呼叫contextDestroyed 方法。 ServletContextAttributeListener 接口方法 attributeAdded、 attributeReplaced、attributeRemoved 接收事件 ServletContextAttributeEvent

3、 触发场景 若有对象加入为application对象的属性,则会呼叫attributeAdded,同理在置换属性与移除属性时,会分别呼叫attributeReplaced、attributeRemoved。 HttpSessionListener 接口方法 sessionCreated与sessionDestroyed 接收事件 HttpSessionEvent 触发场景 在session 对象建立或被消灭时,会分别呼叫这两个方法。 HttpSessionAttributeListener 接口方法 attributeAdded、 attributeReplaced、attributeRemo

4、ved 接收事件 HttpSessionBindingEvent 触发场景 若有对象加入为session对象的属性,则会呼叫attributeAdded,同理在置换属性与移除属性时,会分别呼叫attributeReplaced、 attributeRemoved。 HttpSessionActivationListener 接口方法 sessionDidActivate与 sessionWillPassivate 接收事件 HttpSessionEvent 触发场景 Activate与Passivate是用于置换对象的动作,当session对象为了资源利用或负载平衡等原因而必须暂时储存至硬盘或

5、其它储存器时,所作的动作称之为Passivate,而硬盘或储存器上的session对象重新加载JVM时所采的动作称之为Activate,所以容 易理解的,sessionDidActivate与 sessionWillPassivate分别于Activeate后与将Passivate前呼叫。 ServletRequestListener 接口方法 requestInitialized与 requestDestroyed 接收事件 RequestEvent 触发场景 在request对象建立或被消灭时,会分别呼叫这两个方法。 ServletRequestAttributeListener 接口方法

6、 attributeAdded、 attributeReplaced、attributeRemoved 接收事件 HttpSessionBindingEvent 触发场景 若有对象加入为request对象的属性,则会呼叫attributeAdded,同理在置换属性与移除属性时,会分别呼叫attributeReplaced、 attributeRemoved。 HttpSessionBindingListener 接口方法 valueBound与valueUnbound 接收事件 HttpSessionBindingEvent 触发场景 实现HttpSessionBindingListener接

7、 口的类别,其实例如果被加入至session对象的属性中,则会呼叫 valueBound,如果被从session对象的属性中移除,则会呼叫valueUnbound,实现 HttpSessionBindingListener接口的类别不需在web.xml中设定。 2. 如何注册Servlet中的事件 实现上面这几个接口的类别,除了HttpSessionBindingListener外,必须在web.xml中向容器注册,容器才会在对应的事件发生时呼叫对应的类别,如: demo.servlet.listener.CustomServletContextListener 3. Servlet事件的应用

8、实例 看到这里,你也许会有疑问: 了解这些 listener和event 有什么用呢?我平时开发没有用到这些,一样也能完成任务啊. 不错,在日常的开发中很少用到这些事件处理的方面,但是在某些情况下使用事件处理机制却可以达到事半功倍的效果,例如下面两个例子: 4.Java类实例 = /侦听启动和关闭 import javax.servlet.ServletContextListener; import javax.servlet.*; public class TigerListen implements ServletContextListener public void contextInitialized(ServletContextEvent sce) System.out.print(Init) ; public void contextDestroyed(ServletContextEvent sce) System.out.print(Destroved) ; 对应的web.xml是 = TigerListen =

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号