我的Blog我做主^_^

          走向一條通往JAVA的不歸路...

            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            64 隨筆 :: 68 文章 :: 77 評(píng)論 :: 0 Trackbacks
          目前Servlet2.4和JSP2.0總共有8個(gè)監(jiān)聽器接口和6個(gè)Event類,其中HttpSessionAttributeListener與HttpSessionBindingListener皆使用HttpSessionBindingEvent;HttpSessionListener和HttpSessionActivationListener則都使用HttpSessionEvent;其余Listener對(duì)應(yīng)的Event如下所示:

          Listener接口

          Event

          ServletContextListener

          ServletContextEvent

          ServletContextAttributeListener

          ServletContextAttributeEvent

          HttpSessionListener

          HttpSessionEvent

          HttpSessionActivationListener

          HttpSessionAttributeListener

          HttpSessionBindingEvent

          HttpSessionBindingListener

          ServletRequestListener

          ServletRequestEvent

          ServletRequestAttributeListener

          ServletRequestAttributeEvent


          分別介紹:
          一 ServletContext相關(guān)監(jiān)聽接口
          補(bǔ)充知識(shí):
          通過ServletContext 的實(shí)例可以存取應(yīng)用程序的全局對(duì)象以及初始化階段的變量。
          在JSP文件中,application 是 ServletContext 的實(shí)例,由JSP容器默認(rèn)創(chuàng)建。Servlet 中調(diào)用 getServletContext()方法得到 ServletContext 的實(shí)例。
          注意:
          全局對(duì)象即Application范圍對(duì)象,初始化階段的變量指在web.xml中,經(jīng)由<context-param>元素所設(shè)定的變量,它的范圍也是Application范圍,例如:

          <context-param>
          <param-name>Name</param-name>
          <param-value>browser</param-value>
          </context-param>
          當(dāng)容器啟動(dòng)時(shí),會(huì)建立一個(gè)Application范圍的對(duì)象,若要在JSP網(wǎng)頁中取得此變量時(shí):
          String name = (String)application.getInitParameter("Name");
          或者使用EL時(shí):
          ${initPara.name}
          若是在Servlet中,取得Name的值方法:
          String name = (String)ServletContext.getInitParameter("Name");


          1.ServletContextListener:
          用于監(jiān)聽WEB 應(yīng)用啟動(dòng)和銷毀的事件,監(jiān)聽器類需要實(shí)現(xiàn)javax.servlet.ServletContextListener 接口。
          ServletContextListener 是 ServletContext 的監(jiān)聽者,如果 ServletContext 發(fā)生變化,如服務(wù)器啟動(dòng)時(shí) ServletContext 被創(chuàng)建,服務(wù)器關(guān)閉時(shí) ServletContext 將要被銷毀。

          ServletContextListener接口的方法:
          void contextInitialized(ServletContextEvent sce)
          通知正在接受的對(duì)象,應(yīng)用程序已經(jīng)被加載及初始化。
          void contextDestroyed(ServletContextEvent sce)
          通知正在接受的對(duì)象,應(yīng)用程序已經(jīng)被載出。

          ServletContextEvent中的方法:
          ServletContext getServletContext()
          取得ServletContext對(duì)象


          2.ServletContextAttributeListener:用于監(jiān)聽WEB應(yīng)用屬性改變的事件,包括:增加屬性、刪除屬性、修改屬性,監(jiān)聽器類需要實(shí)現(xiàn)javax.servlet.ServletContextAttributeListener接口。

          ServletContextAttributeListener接口方法:
          void attributeAdded(ServletContextAttributeEvent scab)
          若有對(duì)象加入Application的范圍,通知正在收聽的對(duì)象
          void attributeRemoved(ServletContextAttributeEvent scab)
          若有對(duì)象從Application的范圍移除,通知正在收聽的對(duì)象
          void attributeReplaced(ServletContextAttributeEvent scab)
          若在Application的范圍中,有對(duì)象取代另一個(gè)對(duì)象時(shí),通知正在收聽的對(duì)象


          ServletContextAttributeEvent中的方法:
          java.lang.String getName()
          回傳屬性的名稱
          java.lang.Object getValue()
          回傳屬性的值

          二、HttpSession相關(guān)監(jiān)聽接口
          1.HttpSessionBindingListener接口
          注意:HttpSessionBindingListener接口是唯一不需要再web.xml中設(shè)定的Listener

          當(dāng)我們的類實(shí)現(xiàn)了HttpSessionBindingListener接口后,只要對(duì)象加入Session范圍(即調(diào)用HttpSession對(duì)象的setAttribute方法的時(shí)候)或從Session范圍中移出(即調(diào)用HttpSession對(duì)象的removeAttribute方法的時(shí)候或Session Time out的時(shí)候)時(shí),容器分別會(huì)自動(dòng)調(diào)用下列兩個(gè)方法:
          void valueBound(HttpSessionBindingEvent event)
          void valueUnbound(HttpSessionBindingEvent event)

          思考:如何實(shí)現(xiàn)記錄網(wǎng)站的客戶登錄日志, 統(tǒng)計(jì)在線人數(shù)?

          2.HttpSessionAttributeListener接口
          HttpSessionAttributeListener監(jiān)聽HttpSession中的屬性的操作。
          當(dāng)在Session增加一個(gè)屬性時(shí),激發(fā)attributeAdded(HttpSessionBindingEvent se) 方法;當(dāng)在Session刪除一個(gè)屬性時(shí),激發(fā)attributeRemoved(HttpSessionBindingEvent se)方法;當(dāng)在Session屬性被重新設(shè)置時(shí),激發(fā)attributeReplaced(HttpSessionBindingEvent se) 方法。這和ServletContextAttributeListener比較類似。

          3.HttpSessionListener接口
          HttpSessionListener監(jiān)聽HttpSession的操作。當(dāng)創(chuàng)建一個(gè)Session時(shí),激發(fā)session Created(HttpSessionEvent se)方法;當(dāng)銷毀一個(gè)Session時(shí),激發(fā)sessionDestroyed (HttpSessionEvent se)方法。

          4.HttpSessionActivationListener接口
          主要用于同一個(gè)Session轉(zhuǎn)移至不同的JVM的情形。

          四、ServletRequest監(jiān)聽接口
          1.ServletRequestListener接口
          和ServletContextListener接口類似的,這里由ServletContext改為ServletRequest
          2.ServletRequestAttributeListener接口
          和ServletContextListener接口類似的,這里由ServletContext改為ServletRequest
           
          轉(zhuǎn)的別人的



          posted on 2008-10-21 11:10 java_蟈蟈 閱讀(265) 評(píng)論(0)  編輯  收藏 所屬分類: JAVA
          主站蜘蛛池模板: 唐海县| 德江县| 本溪市| 三明市| 桃园县| 绩溪县| 三亚市| 通州区| 宜黄县| 建始县| 白河县| 保山市| 高阳县| 景泰县| 洞头县| 瓦房店市| 农安县| 长宁区| 敖汉旗| 徐水县| 乃东县| 许昌市| 隆尧县| 贵阳市| 莲花县| 定兴县| 临桂县| 青铜峡市| 泸西县| 建阳市| 陆良县| 肃北| 丹东市| 古丈县| 丁青县| 桑日县| 乌什县| 和静县| 梁河县| 长顺县| 凤山县|