Java世界

          學(xué)習(xí)筆記

          常用鏈接

          統(tǒng)計

          積分與排名

          天籟村

          新華網(wǎng)

          雅虎

          最新評論

          java servlet 監(jiān)聽器種類及介紹

          ServletContextAttributeListener 監(jiān)聽對ServletContext屬性的操作,比如增加、刪除、修改屬性。

          ServletContextListener監(jiān)聽ServletContext。
          當(dāng)創(chuàng)建ServletContext時,激發(fā) contextInitialized(ServletContextEvent sce)方法;
          當(dāng)銷毀ServletContext時,激發(fā)contextDestroyed(ServletContextEvent sce)方法。

           

          ServletContextListener 接口 

          contextInitialized 初始化方法

          contextDestroyed 銷毀方法 



          ServletRequestListener, ServletRequestAttributeListener 接口

          Servlet 2.4版在事件監(jiān)聽器中加入了ServletRequest監(jiān)聽器,包括:ServletRequestListener, ServletRequestAttributeListener ,用來管理和控制與ServletRequest動作有關(guān)的事件。

          對于ServletRequest事件,當(dāng)request初始化、銷毀或者request屬性的增加、刪除和替換時,事件監(jiān)聽類得到通知。
          下表列出了 ServletRequest的事件類型,對應(yīng)特定事件的監(jiān)聽類必須實現(xiàn)的接口和當(dāng)事件發(fā)生時調(diào)用的方法。
          事件類型接口方法
          request初始化javax.servlet.ServletRequestListenerrequestInitialized()
          request銷毀javax.servlet.ServletRequestListenerrequestDestroyed()
          增加屬性javax.servlet.ServletRequestAttributeListenerattributeAdded()
          刪除屬性javax.servlet.ServletRequestAttributeListenerattributeRemoved()
          屬性被替換javax.servlet.ServletRequestAttributeListenerattributeReplaced()

           

          HttpSessionListener 接口

          Http會話(Seesion)與請求(Request)與ServletContext用法相當(dāng)。需指出,Request監(jiān)聽器在Tomcat 4.1不能調(diào)試,故升級到Tomcat 5.0才可以,所以可以肯定RequestListener是符合Servlet2.4新規(guī)范的,需用tomcat5.0以上版本。

          利用HttpSessionListener接口可針對HTTP會話建立一個“監(jiān)聽器類”。只要Web應(yīng)用程序內(nèi)新增了一個HTTP會話,Servlet 容器就會將該事件(HttpSessionEvent)轉(zhuǎn)交給適當(dāng)?shù)?#8220;監(jiān)聽器類”進行處理(必須事先配置web.xml)。

          下表是HttpSessionListener接口內(nèi)定義的兩個方法,只要是實現(xiàn)該接口的“監(jiān)聽器類”,就必須包含這兩種方法。

          方法名稱

          調(diào)用時機

          sessionCreated(HttpSessionEvent se)

          在Web應(yīng)用程序內(nèi)建立一個新的HTTP會話時, Servlet容器將會調(diào)用此方法

          sessionDestoryed(HttpSessionEvent se)

          在Web應(yīng)用程序內(nèi)移除某個HTTP會話時,Servlet容器將會調(diào)用此 方法

            HttpSessionActivationListener 接口

          當(dāng)Web應(yīng)用程序的會話必須跨越其他服務(wù)器時,實現(xiàn)HttpSessionActivationListener接口的“監(jiān)聽器類”將會收到會話轉(zhuǎn)移的事 件。
          下表是HttpSessionActivationListener接口內(nèi)定義的兩種方法。

          方法名稱

          調(diào)用時機

          sessionWillPassivate(HttpSessionEvent se)

          當(dāng)HTTP會話必須轉(zhuǎn)移到其他服務(wù)器之前,Servlet容器將會調(diào)用此方法

          sessionDidActivate(HttpSessionEvent se)

          當(dāng)HTTP會話轉(zhuǎn)移到其他服務(wù)器以后,Servlet容器將會調(diào)用此方法

          舉例來說,會話S必須從服務(wù)器A轉(zhuǎn)移到服務(wù)器B,此時Servlet容器會在S轉(zhuǎn)移前產(chǎn)生一個會話“被動(passive)”事件,該事件由 HttpSessionActivationListener接口的sessionWillPassivate()方法予以回應(yīng)。當(dāng)S轉(zhuǎn)移到服務(wù)器B以 后,Servlet容器會再產(chǎn)生一個會話“啟動”(activate)事件,該事件由HttpSessionActivationListener接口的 sessionDidActivate()方法予以回應(yīng)。

          HttpSessionAttributeListener 接口

          HttpSessionAttributeListener接口與ServletContextAttributeListener非常類似,前者是針對 HTTP會話所設(shè)計的“監(jiān)聽器接口”,后者則是針對Servlet運行環(huán)境(context)所設(shè)計的“監(jiān)聽器接口”,該接口定義的方法見下表。

          方法名稱

          調(diào)用時機

          attributeAdded(HttpSessionBindingEvent scab)

          在HttpSession對象內(nèi)加入新的屬性時會調(diào)用此方法

          attributeRemoved(ServletContextAttributeEvent scab)

          在HttpSession對象內(nèi)刪除某個屬性時會調(diào)用此方法

          attributeReplaced(ServletContextAttributeEvent scab)

          在HttpSession對象內(nèi)置換某個屬性時會調(diào)用此方法

          當(dāng)HTTP會話(HttpSession對象)內(nèi)新增、置換或刪除某個屬性時將會產(chǎn)生一個事件(HttpSessionBindingEvent),只要 是實現(xiàn)HttpSessionAttributeListener接口的“監(jiān)聽器類”就可以回應(yīng)該事件。當(dāng)然了,你必須將這個“監(jiān)聽器類”定義在 web.xml文件內(nèi)。

          HttpSessionBindingListener 接口

          HttpSessionBindingListener接口在觀念上與HttpSessionAttributeListener接口有點類似,但是它與 本章探討的“監(jiān)聽器類”并沒有直接關(guān)系。
          因為Servlet 2.3規(guī)范以前尚未制定Web應(yīng)用程序的“監(jiān)聽器”機制,如果想知道HTTP會話內(nèi)何時加入或移除某個對象,必須采用下列方式:

          (1)準(zhǔn)備綁定至HTTP會話的對象必須實現(xiàn) HttpSessionBindingListener接口- - 監(jiān)聽器對象。

          (2)在該對象內(nèi)改寫HttpSessionBindingListener接口 所定義的兩種方法(參考下表)。

           

          方法名稱

          調(diào)用時 機

          valueBound(HttpSessionBindingEvent event)

          當(dāng)監(jiān)聽器對象綁定至HTTP會話時,Servlet容器將會調(diào)用此方法

          valueUnbound(HttpSessionBindingEvent event)

          當(dāng)監(jiān)聽器對象從HTTP會話內(nèi)修改、移除或會話銷毀時,Servlet容器將會調(diào)用此方法



          _______________________________________________________________________________________________________________________



          ServletContextAttributeListener

          監(jiān)聽對ServletContext屬性的操作,比如增加/刪除/修改

          ServletContextListener

          監(jiān)聽ServletContext,當(dāng)創(chuàng)建ServletContext時,激發(fā) contextInitialized(ServletContextEvent sce)方法;當(dāng)銷毀ServletContext時,激發(fā)contextDestroyed(ServletContextEvent sce)方法

          HttpSessionListener

          監(jiān)聽HttpSession的操作。當(dāng)創(chuàng)建一個Session時,激發(fā)session Created(SessionEvent se)方法;當(dāng)銷毀(或超時)一個Session時,激發(fā)sessionDestroyed (HttpSessionEvent se)方法

          HttpSessionBindingListener

          valueBound---被設(shè)置到session中(setAttribute)
          valueUnbound---從session中解除(removeAttribute)

          HttpSessionBindingListener和HttpSessionListener之間的最大區(qū)別: HttpSessionListener只需要設(shè)置到web.xml中就可以監(jiān)聽整個應(yīng)用中的所有session。 HttpSessionBindingListener必須實例化后放入某一個session中,才可以進行監(jiān)聽。

          從監(jiān)聽范圍上比較,HttpSessionListener設(shè)置一次就可以監(jiān)聽所有session,HttpSessionBindingListener通常都是一對一的。

          HttpSessionBindingListener 需要存儲在session 里 ,比如
          session.setAttribute("ListenerName", new ImplBindingListener(username));

          正是這種區(qū)別成就了HttpSessionBindingListener的優(yōu)勢,我們可以讓每個listener對應(yīng)一個username,這樣就不需要每次再去session中讀取username,進一步可以將所有操作在線列表的代碼都移入listener,更容易維護。

          這里可以直接使用listener的username操作在線列表,不必再去擔(dān)心session中是否存在username。

          valueUnbound的觸發(fā)條件是以下三種情況:

          執(zhí)行session.invalidate()時。

          session超時,自動銷毀時。

          執(zhí)行session.setAttribute("ListenerName", "其他對象");或session.removeAttribute("ListenerName");將listener從session中刪除時。

          因此,只要不將listener從session中刪除,就可以監(jiān)聽到session的銷毀

          HttpSessionAttributeListener

          監(jiān)聽HttpSession中的屬性的操作。當(dāng)在Session增加一個屬性時,激發(fā) attributeAdded(HttpSessionBindingEvent se) 方法;當(dāng)在Session刪除一個屬性時,激發(fā)attributeRemoved(HttpSessionBindingEvent se)方法;當(dāng)在Session屬性被重新設(shè)置時,激發(fā)attributeReplaced(HttpSessionBindingEvent se) 方法

          HttpSessionActivationListener

          使代碼可以支持分布式環(huán)境
          為了負(fù)載均衡或者fail-over,web容器可以遷移一個session到其他的jvm.
          session的passivation是指非活動的session被寫入持久設(shè)備(比如硬盤)。
          activate自然就是相反的過程。在分布式環(huán)境中切換的屬性必須實現(xiàn)serializable接口

          一般情況下他和HttpSessionBindingListener一起使用

          Java代碼

          public   class  Fun  implements  HttpSessionBindingListener,HttpSessionActivationListener{  
             //HttpSessionActivationListener   
             public    void   sessionDidActivate(HttpSessionEvent   event){         
                logout("sessionDidActivate(" +event.getSession().getId()+ ")" ); //激活   
             }   
             public    void   sessionWillPassivate(HttpSessionEvent   event){  
                //被傳送到別的jvm或 寫到硬盤   
                logout("sessionWillPassivate(" +event.getSession().getId()+ ")" );  
             }  
             //HttpSessionBindingListener   
             public    void   valueBound(HttpSessionBindingEvent   event){   
                //被設(shè)置到session中(setAttribute)   
                logout("valueBound(" +event.getSession().getId()+event.getValue()+ ")" );  
             }   
             public    void   valueUnbound(HttpSessionBindingEvent   event){   
                //從session中解除(removeAttribute)   
                logout("valueUnbound(" +event.getSession().getId()+event.getValue()+ ")" );  
             }  
          }

          關(guān)于session超時配置

          Xml代碼

          <session-config>      
            <session-timeout>1<session-timeout>        
          <session-config> 

          ServletRequestListener 

          requestDestroyed  request 響應(yīng)后// 當(dāng)發(fā)出請求,服務(wù)器響應(yīng)后執(zhí)行此方法
          requestInitialized request 響應(yīng)前//當(dāng)發(fā)送請求,服務(wù)器響應(yīng)前執(zhí)行此方法

          當(dāng)請求的頁面中包含了鏈接的css文件或js腳本文件等,都會相應(yīng)增加觸RequestListener方法的次數(shù)。
          比如你在請求的頁面中使用元素引入了一個css文件,則請求該頁面時會觸發(fā)兩次requestInitialized方法,也就是說瀏覽器會發(fā)送兩次請求。
          而HttpSessionListener不會發(fā)生這種情況。
          會引起這種情況的元素有: css,js,jsp導(dǎo)入等。

          如果同時配置了ServletContextListener,HttpSessionListener,ServletRequestListener,容器啟動時
          會先調(diào)用ServletContextListener的contextInitialized方法。
          然后當(dāng)客戶端有請求到來,會先調(diào)用ServletRequestListener的requestInitialized方法,然后再調(diào)用HttpSessionListener的sessionCreated方法,
          如果發(fā)生上面所說的頁面鏈接了其它文件的情況,則會再次觸發(fā)ServletRequestListener的requestInitialized方法

          ServletRequestAttributeListener 

          此外 我還找到了個 ResponseListener

          Servlet監(jiān)聽器用于監(jiān)聽一些重要事件的發(fā)生,監(jiān)聽器對象可以在事情發(fā)生前、發(fā)生后可以做一些必要的處理。
          接口:
          目前Servlet2.4和JSP2.0總共有8個監(jiān)聽器接口和6個Event類,其中HttpSessionAttributeListener與
          HttpSessionBindingListener 皆使用HttpSessionBindingEvent;HttpSessionListener和 HttpSessionActivationListener則都使用HttpSessionEvent;其余Listener對應(yīng)的Event如下所示:

          Listener接口                                Event類 
          ServletContextListener                ServletContextEvent 
          ServletContextAttributeListener   ServletContextAttributeEvent 
          HttpSessionListener                    HttpSessionEvent  
          HttpSessionActivationListener      HttpSessionEvent
          HttpSessionAttributeListener       HttpSessionBindingEvent

          HttpSessionBindingListener         HttpSessionBindingEvent

          ServletRequestListener               ServletRequestEvent

          ServletRequestAttributeListener  ServletRequestAttributeEvent 

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


          posted on 2012-08-24 10:20 Rabbit 閱讀(4730) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 锡林浩特市| 天祝| 陕西省| 二手房| 武威市| 安乡县| 饶阳县| 平武县| 焉耆| 长顺县| 元江| 翁牛特旗| 邵武市| 双桥区| 商南县| 昭通市| 玛沁县| 乐亭县| 绩溪县| 绥滨县| 苏尼特右旗| 峡江县| 福清市| 囊谦县| 黄陵县| 濮阳市| 宁乡县| 武强县| 璧山县| 阳泉市| 佛冈县| 枣阳市| 迁安市| 长泰县| 邵东县| 轮台县| 东乌珠穆沁旗| 河北区| 隆化县| 望城县| 开封市|