`

Listener 与 Event

    博客分类:
  • J2EE
阅读更多

撰写过AWT或Swing的Java程序设计人员对于Listener与Event应该并不陌生,实现 Listener的类可以在特定事件发生时,调用特定的方法来对事件进行回应,在编写JSP/Servlet应用程序时,也有类似的Listener接 口与Event类,所不同的是在AWT或 Swing中是向特定组件加入Listener,而在JSP/Servlet中则是在web.xml中注册Listener,由Container在特定事件发生时调用特定的实现Listener类。

至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事件,与ServletContextListener接口类似,在session (HttpSession)对象建立或被消灭时,会分别调用这两个方法。

HttpSessionAttributeListener接口有三个必须实现的方法,attributeAdded()、 attributeReplaced()、attributeRemoved(),它们接收HttpSessionBindingEvent事件,与 ServletContextAttributeListener接口类似,若有对象加入为session(HttpSession)对象的属性,则会呼叫attributeAdded(),同理在置换属性与移除属性时,会分别调用attributeReplaced()、 attributeRemoved()。

HttpSessionActivationListener接口有两个必须实现的方法,sessionDidActivate()与 sessionWillPassivate(),它们接收HttpSessionEvent,Activate与Passivate是用于置换对象的动作,当session对象为了资源利用或负载平衡等原因而必须暂时储存至硬碟或其它储存器时(透过对象序列化),所作的动作称之为Passivate,而硬盘或储存器上的session对象重新载入JVM时所采的动作称之为Activate,所以容易理解的,sessionDidActivate()与 sessionWillPassivate()分别于Activeate后与将Passivate前调用。

ServletRequestListener接口有两个必须实现的方法,requestInitialized()与 requestDestroyed(),它们接收RequestEvent事件,与ServletContextListener接口类似,在 request(HttpServletRequest)对象建立或被消灭时,会分别调用这两个方法。

ServletRequestAttributeListener接口有三个必须实现的方法,attributeAdded()、 attributeReplaced()、attributeRemoved(),它们接收HttpSessionBindingEvent事件,与 ServletContextAttributeListener接口类似,若有对象加入为request(HttpServletRequest)对象的属性,则会调用attributeAdded(),同理在置换属性与移除属性时,会分别调用attributeReplaced()、 attributeRemoved()。

实现以上这几个接口的类,必须在web.xml中向容器注册,容器才会在对应的事件发生时调用对应的类,例如:

<listener>
    <listener-class>
      demo.servlet.listener.CustomServletContextListener
    </listener-class>

</listener>


另外还有一个HttpSessionBindingListener接口,它使用的方法不同,拥有两个必须实现的方法, valueBound()与valueUnbound(),接收的参数为HttpSessionBindingEvent,实现 HttpSessionBindingListener接口的类,其实例如果被加入至session(HttpSession)对象的属性中,则会调用 valueBound(),如果被从session(HttpSession)对象的属性中移除,则会调用valueUnbound(),实现 HttpSessionBindingListener接口的类不需在web.xml中设定。

分享到:
评论

相关推荐

    event_listener.7z

    event_listener.jsx PS事件监听脚本

    event_listener.jsx

    event_listener.jsx PS事件监听脚本 1、修改脚本监听数据本地化语言显示 ---0.02-01 2、函数增加注释当前历史记录名称 ---0.02-02 https://blog.csdn.net/greless/article/details/105811358

    EventListener,eventlistener是一个小型库,用于类之间的通信,如广播信号.zip

    EventListener是一个小型库,用于类之间的通信,如广播信号

    evt-listener:EventListener 作为对象

    off (其中listener是 EventListener 的一个实例)而不必担心保存和传递事件名称和函数。 请参阅下面的比较。 先决条件 使用 npm 安装它,将其保存为依赖项。 npm i evt-listener --save 两个示例共享相同的事件...

    event-listener:通知异步任务或线程

    事件监听器通知异步任务或线程。...use event_listener :: Event;let flag = Arc :: new (AtomicBool :: new ( false ));let event = Arc :: new (Event :: new ());// Spawn a thread that will se

    SpringBoot整合Listener的两种方式.docx

    SpringBoot整合Listener的两种方式: 1.通过注解扫描完成Listener组件的注册 1.创建一个类实现 ...3.重写 contextInitialized() 与 contextDestroyed() 方法 4.编写启动类 5.增加注解 @ServletComponentScan

    supervisor-event-listener:Supervisor事件通知, 支持邮件, Slack, WebHook

    supervisor-event-listener Supervisor事件通知, 支持邮件, Slack, WebHook 简介 Supervisor是*nix环境下的进程管理工具, 可以把前台进程转换为守护进程, 当进程异常退出时自动重启. supervisor-event-listener监听...

    js HTML DOM EventListener功能与用法实例分析

    本文实例讲述了js HTML DOM EventListener功能与用法。分享给大家供大家参考,具体如下: DOM EventListener 用于向指定元素添加事件句柄。 在用户点击按钮时触发监听事件: document.getElementById('myBtn')....

    IE8-EventListener:IE8的Polyfill

    IE8-EventListener IE8的Polyfill 我怎么才能得到它? IE8-EventListener可通过npm获得,并可以使用以下命令进行安装: npm install ie8-eventlistener 它是什么? IE8-EventListener努力向IE8添加完全的...

    outlook event

    outlook event listener

    Android代码-KDispatcher

    KDispatcher is a Kotlin EventDispatcher This is light-weight event dispatcher based on KOTLIN priority: Int?...eventListener - function listener for event priority - the priority to sort ca

    JavaWeb开发技术-Listener监听器.pptx

    事件(Event) 事件源 事件处理器 事件监听器(Listener) 用户的一个操作,如点击一个按钮、调用一个方法、创建一个对象等。 产生事件的对象。 负责监听发生在 事件源上的事件。 监听器的成员方法,当事件发生的...

    kktix-event-listener

    $ npm install kktix-event-listener 用法 var kktixEventListener = require ( 'kktix-event-listener' ) ; kktixEventListener ( 'http://reactjs.kktix.cc/events/react9' ) ; //=&gt; 命令行界面 $ npm install -g ...

    C++ EventListener (C# style)-开源

    这是在 c++ 中实现的 EventListener(就像在 c# 中一样)。我正在使用一些 c++11 特性(比如 final),但是如果你愿意,你可以很容易地删除它们。 检查维基页面以查看使用情况。 需要做的:线程安全; 安全处置调用...

    debug-event-listener.rar_The Listener

    Get the Debug object exposed from the debug context global object.

    Passive Event Listeners - 被动事件监听器1

    Passive Event Listeners - 被动事件监听器在写webapp页面的时候,Chrome 提醒  [Violat

    finsih-the-event-listener_android.zip_The Listener

    实现imagebutton的事件侦听,并完成跳转。侦听的结果为弹出一个toast。标记登陆成功

    event-api:适用于Java的简单事件处理API

    public class YourEventListener implements EventListener&lt; Event&gt; {@Overridepublic void onEvent ( Event event ) {// Do something :)}} 注册监听器: import net.tassia.event.EventManager ;// ...Event...

Global site tag (gtag.js) - Google Analytics