posts - 60,comments - 71,trackbacks - 0
          form:http://www.aygfsteel.com/gm_jing/articles/211624.html

          Listener是Servlet的監(jiān)聽器,它可以監(jiān)聽客戶端的請求、服務(wù)端的操作等。通過監(jiān)聽器,可以自動激發(fā)一些操作,比如監(jiān)聽在線的用戶的數(shù)量。當(dāng)增加一個HttpSession時,就激發(fā)sessionCreated(HttpSessionEvent se)方法,這樣就可以給在線人數(shù)加1。常用的監(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)方法。

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

          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) 方法。

          下面我們開發(fā)一個具體的例子,這個監(jiān)聽器能夠統(tǒng)計在線的人數(shù)。在ServletContext初始化和銷毀時,在服務(wù)器控制臺打印對應(yīng)的信息。當(dāng)ServletContext里的屬性增加、改變、刪除時,在服務(wù)器控制臺打印對應(yīng)的信息。

          要獲得以上的功能,監(jiān)聽器必須實現(xiàn)以下3個接口:

          HttpSessionListener

          ServletContextListener

          ServletContextAttributeListener

          我們看具體的代碼,見示例14-9。

          【程序源代碼】

          1 // ==================== Program Discription =====================
          2 // 程序名稱:示例14-9 : EncodingFilter .java
          3 // 程序目的:學(xué)習(xí)使用監(jiān)聽器
          4 // ==============================================================
          5 import javax.servlet.http.*;
          6 import javax.servlet.*;
          7
          8 public class OnLineCountListener implements HttpSessionListener,
          ServletContextListener,ServletContextAttributeListener
          9 {
          10  private int count;
          11  private ServletContext context = null;
          12  
          13  public OnLineCountListener()
          14  {
          15   count=0;
          16   //setContext();
          17  }
          18  //創(chuàng)建一個session時激發(fā)
          19  public void sessionCreated(HttpSessionEvent se)
          20  {
          21   count++;
          22   setContext(se);
          23   
          24  }
          25  //當(dāng)一個session失效時激發(fā)
          26  public void sessionDestroyed(HttpSessionEvent se)
          27  {
          28   count--;
          29   setContext(se);
          30  }
          31  //設(shè)置context的屬性,它將激發(fā)attributeReplaced或attributeAdded方法
          32  public void setContext(HttpSessionEvent se)
          33  {
          34   se.getSession().getServletContext().
          setAttribute("onLine",new Integer(count));
          35  }
          36   //增加一個新的屬性時激發(fā)
          37  public void attributeAdded(ServletContextAttributeEvent event) {
          38 
          39  log("attributeAdded('" + event.getName() + "', '" +
          40      event.getValue() + "')");
          41 
          42     }
          43    
          44    //刪除一個新的屬性時激發(fā)
          45     public void attributeRemoved(ServletContextAttributeEvent event) {
          46
          47  log("attributeRemoved('" + event.getName() + "', '" +
          48      event.getValue() + "')");
          49 
          50     }
          51
          52  //屬性被替代時激發(fā)
          53     public void attributeReplaced(ServletContextAttributeEvent event) {
          54 
          55   log("attributeReplaced('" + event.getName() + "', '" +
          56       event.getValue() + "')");
          57     }
          58     //context刪除時激發(fā)
          59      public void contextDestroyed(ServletContextEvent event) {
          60 
          61   log("contextDestroyed()");
          62   this.context = null;
          63 
          64     }
          65 
          66     //context初始化時激發(fā)
          67     public void contextInitialized(ServletContextEvent event) {
          68 
          69   this.context = event.getServletContext();
          70   log("contextInitialized()");
          71 
          72     }
          73     private void log(String message) {
          74 
          75      System.out.println("ContextListener: " + message);
          76     }  
          77 }
           


          【程序注解】
          在OnLineCountListener里,用count代表當(dāng)前在線的人數(shù),OnLineCountListener將在Web服務(wù)器啟動時自動執(zhí)行。當(dāng)OnLineCountListener構(gòu)造好后,把count設(shè)置為0。每增加一個Session,OnLineCountListener會自動調(diào)用sessionCreated(HttpSessionEvent se)方法;每銷毀一個Session,OnLineCountListener會自動調(diào)用sessionDestroyed(HttpSessionEvent se)方法。當(dāng)調(diào)用sessionCreated(HttpSessionEvent se)方法時,說明又有一個客戶在請求,此時使在線的人數(shù)(count)加1,并且把count寫到ServletContext中。ServletContext的信息是所有客戶端共享的,這樣,每個客戶端都可以讀取到當(dāng)前在線的人數(shù)。
           

           


          從作用域范圍來說,Servlet的作用域有ServletContext,HttpSession,ServletRequest.

          Context范圍:

          ServletContextListener:
          對一個應(yīng)用進行全局監(jiān)聽.隨應(yīng)用啟動而啟動,隨應(yīng)用消失而消失主要有兩個方法:

          contextDestroyed(ServletContextEvent event)

          在應(yīng)用關(guān)閉的時候調(diào)用

          contextInitialized(ServletContextEvent event)

          在應(yīng)用啟動的時候調(diào)用

          這個監(jiān)聽器主要用于一些隨著應(yīng)用啟動而要完成的工作,也就是很多人說的我想在容器
          啟動的時候干..........
          一般來說對"全局變量"初始化,如

          public void contextInitialized(ServletContextEvent event){
          ServletContex sc = event.getServletContext();
          sc.setAttribute(name,value);
          }

          以后你就可以在任何servlet中g(shù)etServletContext().getAttribute(name);
          我最喜歡用它來做守護性工作,就是在contextInitialized(ServletContextEvent event)
          方法中實現(xiàn)一個Timer,然后就讓應(yīng)用在每次啟動的時候讓這個Timer工作:
          程序代碼:
          public void contextInitialized(ServletContextEvent event){
          timer = new Timer();
          timer.schedule(new TimerTask(){
          public void run(){
          //do any things
          }
          },0,時間間隔);
          }


          有人說Timer只能規(guī)定從現(xiàn)在開始的多長時間后,每隔多久做一次事或在什么時間做
          一次事,那我想在每月1號或每天12點做一項工作如何做呢?
          你只要設(shè)一個間隔,然后每次判斷一下當(dāng)時是不是那個時間段就行了啊,比如每月一號做,那你
          時間間隔設(shè)為天,即24小時一個循環(huán),然后在run方法中判斷當(dāng)時日期new Date().getDate()==1
          就行了啊.如果是每天的12點,那你時間間隔設(shè)為小時,然后在run中判斷new Date().getHour()
          ==12,再做某事就行了.

          ServletContextAttributeListener:

          這個監(jiān)聽器主要監(jiān)聽ServletContex對象在setAttribute()和removeAttribute()的事件,注意
          也就是一個"全局變量"在被Add(第一次set),replace(對已有的變量重新賦值)和remove的時候.
          分別調(diào)用下面三個方法:
          public void attributeAdded(ServletContextAttributeEvent scab)這個方法不僅可以知道
          哪些全局變量被加進來,而且可獲取容器在啟動時自動設(shè)置了哪些context變量:
          程序代碼:
          public void attributeAdded(ServletContextAttributeEvent scab){
          System.out.println(scab.getName());
          }
          public void attributeRemoved(ServletContextAttributeEvent scab)

          public void attributeReplaced(ServletContextAttributeEvent scab)


          Session范圍:
          HttpSessionListener:
          這個監(jiān)聽器主要監(jiān)聽一個Session對象被生成和銷毀時發(fā)生的事件.對應(yīng)有兩個方法:
          程序代碼:
          public void sessionCreated(HttpSessionEvent se)

          public void sessionDestroyed(HttpSessionEvent se)


          一般來說,一個session對象被create時,可以說明有一個新客端進入.可以用來粗略統(tǒng)計在線人
          數(shù),注意這不是精確的,因為這個客戶端可能立即就關(guān)閉了,但sessionDestroyed方法卻會按一定
          的策略很久以后才會發(fā)生.

          HttpSessionAttributeListener:
          和ServletContextAttributeListener一樣,它監(jiān)聽一個session對象的Attribut被Add(一個特定
          名稱的Attribute每一次被設(shè)置),replace(已有名稱的Attribute的值被重設(shè))和remove時的事件.
          對就的有三個方法.
          程序代碼:
          public void attributeAdded(HttpSessionBindingEvent se)

          public void attributeRemoved(HttpSessionBindingEvent se)

          public void attributeReplaced(HttpSessionBindingEvent se)


          上面的幾個監(jiān)聽器的方法,都是在監(jiān)聽?wèi)?yīng)用邏輯中servlet邏輯中發(fā)生了什么事,一般的來說.
          我們只要完成邏輯功能,比如session.setAttribute("aaa","111");我只要把一個名為aaa的變量
          放在session中以便以后我能獲取它,我并不關(guān)心當(dāng)session.setAttribute("aaa","111");發(fā)生時
          我還要干什么.(當(dāng)然有些時候要利用的),但對于下面這個監(jiān)聽器,你應(yīng)該好好發(fā)解一下:

          HttpSessionBindingListener:
          上面的監(jiān)聽器都是作為一個獨立的Listener在容器中控制事件的.而HttpSessionBindingListener
          對在一對象中監(jiān)聽該對象的狀態(tài),實現(xiàn)了該接口的對象如果被作為value被add到一個session中或從
          session中remove,它就會知道自己已經(jīng)作為一個session對象或已經(jīng)從session刪除,這對于一些非
          純JAVA對象,生命周期長于session的對象,以及其它需要釋放資源或改變狀態(tài)的對象非常重要.
          比如:
          session.setAttribute("abcd","1111");
          以后session.removeAttribute("abcd");因為abcd是一個字符中,你從session中remove后,它就會
          自動被垃圾回收器回收,而如果是一個connection:(只是舉例,你千萬不要加connection往session
          中加入)
          程序代碼:
          session.setAttribute("abcd",conn);

          以后session.removeAttribute("abcd");這時這個conn被從session中remove了,你已經(jīng)無法獲取它
          的句柄,所以你根本沒法關(guān)閉它.而在沒有remove之前你根本不知道什么時候要被remove,你又無法
          close(),那么這個connection對象就死了.另外還有一些對象可以在被加入一個session時要鎖定
          還要被remove時要解鎖,應(yīng)因你在程序中無法判斷什么時候被remove(),add還好操作,我可以先加鎖
          再add,但remove就后你就找不到它的句柄了,根本沒法解鎖,所以這些操作只能在對象自身中實現(xiàn).
          也就是在對象被add時或remove時通知對象自己回調(diào)相應(yīng)的方法:
          程序代碼:
          MyConn extends Connection implements HttpSessionBindingListener{
          public void valueBound(HttpSessionBindingEvent se){
          this.initXXX();
          }
          public void valueUnbound(HttpSessionBindingEvent se){

          this.close();
          }
          }


          session.setAttribute("aaa",new MyConn());
          這時如果調(diào)用session.removeAttribute("aaa"),則觸發(fā)valueUnbound方法,就會自動關(guān)閉自己.
          而其它的需要改變狀態(tài)的對象了是一樣.


          posted on 2008-07-15 17:25 henry1451 閱讀(342) 評論(0)  編輯  收藏

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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 页游| 宁阳县| 翁源县| 台安县| 新沂市| 莱西市| 塔河县| 五原县| 石景山区| 贵南县| 吉隆县| 民县| 遵化市| 曲阜市| 永清县| 昌邑市| 黄陵县| 鄂伦春自治旗| 永泰县| 浏阳市| 班玛县| 松原市| 绥中县| 砀山县| 万载县| 孟州市| 扎鲁特旗| 十堰市| 南川市| 金堂县| 阳东县| 怀集县| 中江县| 德格县| 灵山县| 双辽市| 安塞县| 龙岩市| 元朗区| 富民县| 江山市|