<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Java世界

    學習筆記

    常用鏈接

    統計

    積分與排名

    天籟村

    新華網

    雅虎

    最新評論

    java servlet 監聽器種類及介紹

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

    ServletContextListener監聽ServletContext。
    當創建ServletContext時,激發 contextInitialized(ServletContextEvent sce)方法;
    當銷毀ServletContext時,激發contextDestroyed(ServletContextEvent sce)方法。

     

    ServletContextListener 接口 

    contextInitialized 初始化方法

    contextDestroyed 銷毀方法 



    ServletRequestListener, ServletRequestAttributeListener 接口

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

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

     

    HttpSessionListener 接口

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

    利用HttpSessionListener接口可針對HTTP會話建立一個“監聽器類”。只要Web應用程序內新增了一個HTTP會話,Servlet 容器就會將該事件(HttpSessionEvent)轉交給適當的“監聽器類”進行處理(必須事先配置web.xml)。

    下表是HttpSessionListener接口內定義的兩個方法,只要是實現該接口的“監聽器類”,就必須包含這兩種方法。

    方法名稱

    調用時機

    sessionCreated(HttpSessionEvent se)

    在Web應用程序內建立一個新的HTTP會話時, Servlet容器將會調用此方法

    sessionDestoryed(HttpSessionEvent se)

    在Web應用程序內移除某個HTTP會話時,Servlet容器將會調用此 方法

      HttpSessionActivationListener 接口

    當Web應用程序的會話必須跨越其他服務器時,實現HttpSessionActivationListener接口的“監聽器類”將會收到會話轉移的事 件。
    下表是HttpSessionActivationListener接口內定義的兩種方法。

    方法名稱

    調用時機

    sessionWillPassivate(HttpSessionEvent se)

    當HTTP會話必須轉移到其他服務器之前,Servlet容器將會調用此方法

    sessionDidActivate(HttpSessionEvent se)

    當HTTP會話轉移到其他服務器以后,Servlet容器將會調用此方法

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

    HttpSessionAttributeListener 接口

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

    方法名稱

    調用時機

    attributeAdded(HttpSessionBindingEvent scab)

    在HttpSession對象內加入新的屬性時會調用此方法

    attributeRemoved(ServletContextAttributeEvent scab)

    在HttpSession對象內刪除某個屬性時會調用此方法

    attributeReplaced(ServletContextAttributeEvent scab)

    在HttpSession對象內置換某個屬性時會調用此方法

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

    HttpSessionBindingListener 接口

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

    (1)準備綁定至HTTP會話的對象必須實現 HttpSessionBindingListener接口- - 監聽器對象。

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

     

    方法名稱

    調用時 機

    valueBound(HttpSessionBindingEvent event)

    當監聽器對象綁定至HTTP會話時,Servlet容器將會調用此方法

    valueUnbound(HttpSessionBindingEvent event)

    當監聽器對象從HTTP會話內修改、移除或會話銷毀時,Servlet容器將會調用此方法



    _______________________________________________________________________________________________________________________



    ServletContextAttributeListener

    監聽對ServletContext屬性的操作,比如增加/刪除/修改

    ServletContextListener

    監聽ServletContext,當創建ServletContext時,激發 contextInitialized(ServletContextEvent sce)方法;當銷毀ServletContext時,激發contextDestroyed(ServletContextEvent sce)方法

    HttpSessionListener

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

    HttpSessionBindingListener

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

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

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

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

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

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

    valueUnbound的觸發條件是以下三種情況:

    執行session.invalidate()時。

    session超時,自動銷毀時。

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

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

    HttpSessionAttributeListener

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

    HttpSessionActivationListener

    使代碼可以支持分布式環境
    為了負載均衡或者fail-over,web容器可以遷移一個session到其他的jvm.
    session的passivation是指非活動的session被寫入持久設備(比如硬盤)。
    activate自然就是相反的過程。在分布式環境中切換的屬性必須實現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){   
          //被設置到session中(setAttribute)   
          logout("valueBound(" +event.getSession().getId()+event.getValue()+ ")" );  
       }   
       public    void   valueUnbound(HttpSessionBindingEvent   event){   
          //從session中解除(removeAttribute)   
          logout("valueUnbound(" +event.getSession().getId()+event.getValue()+ ")" );  
       }  
    }

    關于session超時配置

    Xml代碼

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

    ServletRequestListener 

    requestDestroyed  request 響應后// 當發出請求,服務器響應后執行此方法
    requestInitialized request 響應前//當發送請求,服務器響應前執行此方法

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

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

    ServletRequestAttributeListener 

    此外 我還找到了個 ResponseListener

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

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

    HttpSessionBindingListener         HttpSessionBindingEvent

    ServletRequestListener               ServletRequestEvent

    ServletRequestAttributeListener  ServletRequestAttributeEvent 

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

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

    1.ServletContextListener:
    用于監聽WEB 應用啟動和銷毀的事件,監聽器類需要實現javax.servlet.ServletContextListener 接口。
    ServletContextListener 是 ServletContext 的監聽者,如果 ServletContext 發生變化,如服務器啟動時 ServletContext 被創建,服務器關閉時 ServletContext 將要被銷毀。

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

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

    2.ServletContextAttributeListener:用于監聽WEB應用屬性改變的事件,包括:增加屬性、刪除屬性、修改屬性,監聽器類需要實現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相關監聽接口
    1.HttpSessionBindingListener接口
    注意:HttpSessionBindingListener接口是唯一不需要再web.xml中設定的Listener

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

    思考:如何實現記錄網站的客戶登錄日志, 統計在線人數?

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

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

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

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


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


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


    網站導航:
     
    主站蜘蛛池模板: 久久久久国产免费| 成人毛片100免费观看| 91网站免费观看| 97亚洲熟妇自偷自拍另类图片| 久久av免费天堂小草播放| 青青草原亚洲视频| 丝瓜app免费下载网址进入ios| 国产国拍亚洲精品福利| 亚洲国产免费综合| 亚洲精品~无码抽插| 免费国产在线视频| 亚洲狠狠狠一区二区三区| 一个人免费观看视频www| 亚洲人成电影网站免费| 免费观看亚洲人成网站| 一级视频在线免费观看| 亚洲成AV人在线观看天堂无码| 久久精品一本到99热免费| 亚洲AV无码乱码在线观看代蜜桃| 在线观看日本免费a∨视频| 国产成人亚洲综合在线| 亚洲日韩VA无码中文字幕| 久久精品成人免费看| 亚洲毛片无码专区亚洲乱| 国产黄色片在线免费观看| 久久精品免费网站网| 亚洲精品中文字幕无码AV| 日韩毛片无码永久免费看| 国产精品免费αv视频| 亚洲宅男永久在线| 青青青国产免费一夜七次郎| 一区二区三区免费视频网站| 亚洲黄色中文字幕| 国产自产拍精品视频免费看| a毛片视频免费观看影院| 亚洲av午夜精品无码专区| 亚洲?V无码乱码国产精品| 永久免费在线观看视频| 黄色网址大全免费| 亚洲韩国在线一卡二卡| 免费在线观看a级毛片|