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类别。
ServletContextListener接口
[接口方法] contextInitialized()与 contextDestroyed()
[接收事件] ServletContextEvent
[触发场景] 在Container加载Web应用程序时(例如启动 Container之后),会呼叫contextInitialized(),而当容器移除Web应用程序时,会呼叫contextDestroyed ()方法。
ServletContextAttributeListener
[接口方法] attributeAdded()、 attributeReplaced()、attributeRemoved()
[接收事件] ServletContextAttributeEvent
[触发场景] 若有对象加入为application(ServletContext)对象的属性,则会呼叫attributeAdded(),同理在置换属性与移除属性时,会分别呼叫attributeReplaced()、attributeRemoved()。
HttpSessionListener
[接口方法] sessionCreated()与sessionDestroyed ()
[接收事件] HttpSessionEvent
[触发场景] 在session (HttpSession)对象建立或被消灭时,会分别呼叫这两个方法。
HttpSessionAttributeListener
[接口方法] attributeAdded()、 attributeReplaced()、attributeRemoved()
[接收事件] HttpSessionBindingEvent
[触发场景] 若有对象加入为session(HttpSession)对象的属性,则会呼叫attributeAdded(),同理在置换属性与移除属性时,会分别呼叫attributeReplaced()、 attributeRemoved()。
HttpSessionActivationListener
[接口方法] sessionDidActivate()与 sessionWillPassivate()
[接收事件] HttpSessionEvent
[触 发场景] Activate与Passivate是用于置换对象的动作,当session对象为了资源利用或负载平衡等原因而必须暂时储存至硬盘或其它储存器时(透 过对象序列化),所作的动作称之为Passivate,而硬盘或储存器上的session对象重新加载JVM时所采的动作称之为Activate,所以容 易理解的,sessionDidActivate()与 sessionWillPassivate()分别于Activeate后与将Passivate前呼叫。
ServletRequestListener
[接口方法] requestInitialized()与 requestDestroyed()
[接收事件] RequestEvent
[触发场景] 在request(HttpServletRequest)对象建立或被消灭时,会分别呼叫这两个方法。
ServletRequestAttributeListener
[接口方法] attributeAdded()、 attributeReplaced()、attributeRemoved()
[接收事件] HttpSessionBindingEvent
[触发场景] 若有对象加入为request(HttpServletRequest)对象的属性,则会呼叫attributeAdded(),同理在置换属性与移除属性时,会分别呼叫attributeReplaced()、 attributeRemoved()。
HttpSessionBindingListener
[接口方法] valueBound()与valueUnbound()
[接收事件] HttpSessionBi
资料来源:学网(www.xue5.com),原文地址:http://www.xue5.com/itedu/200707/134132.html
分享到:
相关推荐
通信接口概述 通信接口概述 通信接口概述
微机原理与接口概述微机原理与接口概述微机原理与接口概述微机原理与接口概述微机原理与接口概述
3GPP 5G标准 38.470-F1接口概述和一般原则(f50 2019-6
简单的介绍了MIPI、typeC DP、LVDS、RGB和TTL几种显示屏接口
RS232接口概述详细的讲述了rs232接口的各种电器等性能
【推选】接口概述PPT资料.ppt
微机系统与接口:总线与接口概述.ppt
脑机接口概述ppt课件.ppt
SGV接口概述PPT课件.pptx
微型计算机接口概述.pptx
接口概述(共17张PPT)精选.pptx
图形设备接口概述.pptx
Matlab混合编程 MATLAB外部接口概述 共115页.ppt
微机系统与接口:一、总线与接口概述.ppt
SGV接口概述PPT学习教案.pptx
网络编程接口概述PPT课件.pptx
day10_01_接口概述与生活举例
网络编程接口概述PPT学习教案.pptx
微型计算机接口概述PPT学习教案.pptx
JAVA接口概述 说明与举例; ava又提供了一种表达方式:接口