gembin

          OSGi, Eclipse Equinox, ECF, Virgo, Gemini, Apache Felix, Karaf, Aires, Camel, Eclipse RCP

          HBase, Hadoop, ZooKeeper, Cassandra

          Flex4, AS3, Swiz framework, GraniteDS, BlazeDS etc.

          There is nothing that software can't fix. Unfortunately, there is also nothing that software can't completely fuck up. That gap is called talent.

          About Me

           

          利用HttpSessionListener實現網站在線人數統計功能

          利用HttpSessionListener實現網站在線人數統計功能
           
          在網站中經常需要進行在線人數的統計。過去的一般做法是結合登錄和退出功能,即當用戶輸入用戶名密碼進行登錄的時候計數器加1,然后當用戶點擊退出按鈕退出系統的時候計數器減1。這種處理方式存在一些缺點,例如:用戶正常登錄后,可能會忘記點擊退出按鈕,而直接關閉瀏覽器,導致計數器減1的操作沒有及時執行;網站上還經常有一些內容是不需要登錄就可以訪問的,在這種情況下也無法使用上面的方法進行在線人數統計。
            我們可以利用Servlet規范中定義的事件監聽器(Listener)來解決這個問題,實現更準確的在線人數統計功能。對每一個正在訪問的用戶,J2EE應用服務器會為其建立一個對應的HttpSession對象。當一個瀏覽器第一次訪問網站的時候,J2EE應用服務器會新建一個HttpSession對象,并觸發HttpSession創建事件,如果注冊了HttpSessionListener事件監聽器,則會調用HttpSessionListener事件監聽器的sessionCreated方法。相反,當這個瀏覽器訪問結束超時的時候,J2EE應用服務器會銷毀相應的HttpSession對象,觸發HttpSession銷毀事件,同時調用所注冊HttpSessionListener事件監聽器的sessionDestroyed方法。
            可見,對應于一個用戶訪問的開始和結束,相應的有sessionCreated方法和sessionDestroyed方法執行。這樣,我們只需要在HttpSessionListener實現類的sessionCreated方法中讓計數器加1,在sessionDestroyed方法中讓計數器減1,就輕松實現了網站在線人數的統計功能。
            下面就是利用HttpSessionListener實現在線人數統計的一個例子,這個例子已經在中創軟件的J2EE應用服務器InforWeb中測試通過。
            首先,編寫一個簡單的計數器,代碼如下:
          1. package gongfei.cmc.articles.onlinecounter; 
          2. public class OnlineCounter { 
          3.     private static long online = 0;     
          4.     public static long getOnline() { 
          5.         return online; 
          6.     }     
          7.     public static void raise(){ 
          8.         online++; 
          9.     }  
          10.     public static void reduce(){ 
          11.         online--; 
          12.    } 

            然后,編寫HttpSessionListener實現類,在這個實現類的sessionCreated方法中調用OnlineCounter的raise方法,在sessionDestroyed方法中調用OnlineCounter的reduce方法,代碼如下:
          1. package gongfei.cmc.articles.onlinecounter; 
          2. import javax.servlet.http.HttpSessionEvent
          3. import javax.servlet.http.HttpSessionListener
          4. public class OnlineCounterListener implements HttpSessionListener { 
          5.     public void sessionCreated(HttpSessionEvent hse) { 
          6.         OnlineCounter.raise(); 
          7.     } 
          8.     public void sessionDestroyed(HttpSessionEvent hse) { 
          9.         OnlineCounter.reduce(); 
          10.     } 

            再然后,把這個HttpSessionListener實現類注冊到網站應用中,也就是在網站應用的web.xml中加入如下內容:
          1. <web-app> 
          2.     …… 
          3.     <listener> 
          4.         <listener-class
          5.             gongfei.cmc.articles.example.OnlineCounterListener 
          6.         </listener-class
          7.     </listener> 
          8.     …… 
          9. </web-app> 

          posted on 2007-09-14 17:16 gembin 閱讀(3082) 評論(0)  編輯  收藏


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


          網站導航:
           

          導航

          統計

          常用鏈接

          留言簿(6)

          隨筆分類(440)

          隨筆檔案(378)

          文章檔案(6)

          新聞檔案(1)

          相冊

          收藏夾(9)

          Adobe

          Android

          AS3

          Blog-Links

          Build

          Design Pattern

          Eclipse

          Favorite Links

          Flickr

          Game Dev

          HBase

          Identity Management

          IT resources

          JEE

          Language

          OpenID

          OSGi

          SOA

          Version Control

          最新隨筆

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          free counters
          主站蜘蛛池模板: 朝阳市| 白玉县| 赤城县| 凤凰县| 达孜县| 梧州市| 高邑县| 钟山县| 宿州市| 望城县| 成都市| 广宗县| 惠来县| 巴中市| 延边| 乐平市| 宝山区| 凯里市| 清镇市| 黄梅县| 武汉市| 繁昌县| 武强县| 南京市| 三河市| 白玉县| 罗源县| 伊通| 青岛市| 扎鲁特旗| 安龙县| 海宁市| 凤庆县| 辽阳县| 东明县| 甘德县| 朝阳区| 呼和浩特市| 天柱县| 类乌齐县| 长子县|