zoukankan      html  css  js  c++  java
  • 《JSP2.0 技术手册》读书笔记四Listener

    Listener,监听器可谓很神秘,作用很大,要想实现一些特殊的功能,监听器的作用就要得到发挥了,而且各种框架和技术都有监听器的概念。web应用程序员可以利用Listener接口,监听容器中的某一个执行程序,并且根据程序的需求做出适当响应。

    目前有八个监听接口和六个事件类,每个监听器与要监听的事件间对应关系如下:
    ServletContextListener ---- ServletContextEvent;
    ServletContextAttributeListener ---- ServletContextAttributeEvent;
    HttpSessionListener,HttpSessionActivationListener ---- HttpSessionEvent;
    HttpSessionAttributeListener,HttpSessionBindingListener ---- HttpSessionBindingEvent;
    ServletRequestListener ---- ServletRequestEvent;
    ServletRequestAttributeListener ---- ServletRequestAttributeEvent

    ServletContext有关Listener

    在以前的笔记中我们已经知道一个web应用只有一个servletContext对象,它保存着应用和容器的信息。
    一个实现javax.servlet.ServletContextListener接口的程序,当Container启动时,程序会开始监听工作。方法如下:
    contextInitialized(javax.servlet.ServletContextEvent): 容器启动,调用此方法,通知应用程序被加载及初始化。
    contextDestroyed(ServletContextEvent):应用被移除,即服务器关闭时,调用。
    其中ServletContexEvent可以调用方法getServletContext()取得ServletContext对象(http://blog.csdn.net/whuqin/archive/2010/10/07/5925569.aspx)。当服务启动或者关闭时,可以使用该Listener来捕捉事件进行响应。
    实现javax.servlet.ServletContextAttributeListener接口的程序能够监听Application(ServletContext对象)范围的变化,即有对象(属性)加入或者移除Application的范围时,该程序会被调用通知。方法如下:
    attributeAdded(javax.servlet.ServletContextAttributeEvent):有对象加入application,调用。
    attributeReplaced(ServletContextAttributeEvent):application范围中的对象被替换,调用。
    attributeRemoved(ServletContextAttributeEvent):对象从application范围中移除时,调用。
    当jsp页面中,出现application.setAttribute(name,object)和application.removeAttribute(name)时,该监听器就会被调用执行相应方法。其中ServletContextAttributeEvent类方法有:getName()-获得新增/移除/取代的属性名称;getValue()-获得属性值(若为取代,则返回取代前的值)。该事件类继承ServletContextEvent。
    与ServlletContext有关的监听器均需要在web.xml中配置,如下:

    <web-app> 
    <listener>
      <listener-class>mylistener</listener-class>
     </listener>
    </web-app>

    HttpSession有关监听器 

    一个类实现了java.servlet.http.HttpSessionBindingListener后,当此类的对象加入或移除Session范围时(session.setAttribute()或session.removeAttribute()在jsp中被使用时),该类中的方法将被调用。即此监听器只监听自己加入或移除sesion,而且无需在web.xml中配置。方法如下:
    valueBound(javax.servlet.http.HttpSessionBindingEvent):实现该监听器接口的对象被加入session时,调用。
    valueUnbound(HttpSessionBindingEvent):实现该接听器的对象被移除时,调用。

    实现javax.servlet.http.HttpSessionAttributeListener接口的类,功能与ServletContextAttributeListener类似,监控session范围内所有对象的增加/替代和移除。方法如下:
    attributeAdded(HttpSessionBindingEvent):有对象加入session,调用。
    attributeReplaced(HttpSessionBindingEvent):session范围有对象被代替,调用。
    attributeRemoved(HttpSessionBindingEvent):有对象从session中移除,调用。
    其中HttpSessionBindingEvent方法有:getName()获得新增/移除/取代的属性名;getValue获得属性值(被取代的属性值);getSession()获得HttpSession对象。另外,需要在web.xml中配置。方法如上了。
    补:所有的监听器都需要配置,只有HttpSessionBindingListener不需要。

    javax.servlet.http.HttpSessionListener负责监听Session的产生和销毁。方法如下:
    sessionCreated(javax.servlet.http.HttpSessionEvent):session被加载及初始化,调用。
    sessionDestroyed(HttpSessionEvent):session被载出,调用。
    其中HttpSessionEvent的方法getSession()可以获得当前操作的session对象。

    javax.servlet.http.HttpSessionActivationListener接口主要用于:同一个Session转移至不同JVM的情形,如负载平衡。这个监听器不常用。当session被存储起来,等待转移至另一个jvm时,这段时间为失效状态。若session中的属性对象实现了此接口,容器会自动调用sessionWillPassivate(HttpSessionEvent)。当session被转移至其他jvm之后,又成为有效状态,方法sessionDidActivate(HttpSessionEvent)会被调用。

    ServletRequest有关监听器 

    javax.servlet.ServletRequestListener,与ServletContextListener/HttpSessionListener功能类似。当有请求产生或是销毁时调用此监听器方法。
    requestInitialized(javax.servlet.servletRequestEvent):servletRequest被加载并初始化,调用。
    requestDestroyed(ServletRequestEvent):serveltRequest载出,调用。
    其中,ServletRequestEvent有两方法:getServletContext()和getServletRequest()。

    javax.servlet.ServletRequestAttributeListener,与ServletContextAttributeListener/HttpSessionAttributeListener功能类似。监听request范围内的属性增加/删除/取代,即在jsp页面,显示使用了request.setAttribute()/reqeust.removeAttribute()。
    attributeAdded(javax.servlet.ServletRequestAttributeEvent):有对象加入request,调用。
    attributeReplaced(ServletRequestAttributeEvent):request范围内有对象被取代,调用。
    attributeRemoved(ServletRequestAttributeEvent):有对象从request范围移除,调用。
    其中ServletRequestAttributeEvent有方法:getName()返回属性名;getValue()返回属性值(被取代的属性值)。继承ServletRequestEvent。

  • 相关阅读:
    C#练习3
    C#练习2
    C#环境变量配置及csc命令详解(转自cy88310)
    建站流程(转)
    C#练习
    程序竞赛1
    排序算法
    输出有向图的邻接矩阵
    C#高效分页代码(不用存储过程)
    存储过程详解
  • 原文地址:https://www.cnblogs.com/whuqin/p/4982125.html
Copyright © 2011-2022 走看看