冰浪

          哥已不再年輕 - 堅定夢想,畢生追求!
          posts - 85, comments - 90, trackbacks - 0, articles - 3
            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          以下是一些不詳細(xì)的代碼,主要是說明原理:
          捕獲Session事件的意義:
          1、    記錄網(wǎng)站的客戶登錄日志(登錄,退出信息等)
          2、    統(tǒng)計在線人數(shù)
          3、    等等還有很多,呵呵,自己想吧……總之挺重要的。

          Session代表客戶的會話過程,客戶登錄時,往Session中傳入一個對象,即可跟蹤客戶的會話。在Servlet中,傳入Session的對象如果是一個實現(xiàn)HttpSessionBindingListener接口的對象(方便起見,此對象稱為監(jiān)聽器),則在傳入的時候(即調(diào)用HttpSession對象的setAttribute方法的時候)和移去的時候(即調(diào)用HttpSession對象的removeAttribute方法的時候或Session Time out的時候)Session對象會自動調(diào)用監(jiān)聽器的valueBound和valueUnbound方法(這是HttpSessionBindingListener接口中的方法)。
          由此可知,登錄日志也就不難實現(xiàn)了。
          另外一個問題是,如何統(tǒng)計在線人數(shù),這個問題跟實現(xiàn)登錄日志稍微有點不同,統(tǒng)計在線人數(shù)(及其信息),就是統(tǒng)計現(xiàn)在有多少個Session實例存在,我們可以增加一個計數(shù)器(如果想存儲更多的信息,可以用一個對象來做計數(shù)器,隨后給出的實例中,簡單起見,用一個整數(shù)變量作為計數(shù)器),通過在valueBound方法中給計數(shù)器加1,valueUnbound方法中計數(shù)器減1,即可實現(xiàn)在線人數(shù)的統(tǒng)計。當(dāng)然,這里面要利用到ServletContext的全局特性。(有關(guān)ServletContext的敘述請參考Servlet規(guī)范),新建一個監(jiān)聽器,并將其實例存入ServletContext的屬性中,以保證此監(jiān)聽器實例的唯一性,當(dāng)客戶登錄時,先判斷ServletContext的這個屬性是否為空,如果不為空,證明已經(jīng)創(chuàng)建,直接將此屬性取出放入Session中,計數(shù)器加1;如果為空則創(chuàng)建一個新的監(jiān)聽器,并存入ServletContext的屬性中。

          舉例說明:

          實現(xiàn)一個監(jiān)聽器:
          // SessionListener.java

          import java.io.*;
          import java.util.*;
          import javax.servlet.http.*;

          //監(jiān)聽登錄的整個過程
          public class SessionListener implements HttpSessionBindingListener
          {

          public String privateInfo="";        //生成監(jiān)聽器的初始化參數(shù)字符串
          private String logString="";        //日志記錄字符串
          private int count=0;        //登錄人數(shù)計數(shù)器

          public SessionListener(String info){
             this.privateInfo=info;
          }

          public int getCount(){
             return count;
          }

          public void valueBound(HttpSessionBindingEvent event)
          {
             count++;
             if (privateInfo.equals("count"))
             {
                 return;
             }
             try{
          Calendar calendar=new GregorianCalendar();
          System.out.println("LOGIN:"+privateInfo+" TIME:"+calendar.getTime());
          logString="\nLOGIN:"+privateInfo+" TIME:"+calendar.getTime()+"\n";
          for(int i=1;i<1000;i++){
           File file=new File("yeeyoo.log"+i);
           if(!(file.exists()))
             file.createNewFile();   //如果文件不存在,創(chuàng)建此文件
           if(file.length()>1048576) //如果文件大于1M,重新創(chuàng)建一個文件
             continue;
             FileOutputStream foo=new FileOutputStream("yeeyoo.log"+i,true);//以append方式打開創(chuàng)建文件
             foo.write(logString.getBytes(),0,logString.length()); //寫入日志字符串
             foo.close();
             break;//退出
          }
             }catch(FileNotFoundException e){}
              catch(IOException e){}
          }

          public void valueUnbound(HttpSessionBindingEvent event)
          {
             count--;
             if (privateInfo.equals("count"))
             {
                 return;
             }
             try{
          Calendar calendar=new GregorianCalendar();
          System.out.println("LOGOUT:"+privateInfo+" TIME:"+calendar.getTime());
          logString="\nLOGOUT:"+privateInfo+" TIME:"+calendar.getTime()+"\n";
          for(int i=1;i<1000;i++){
           File file=new File("yeeyoo.log"+i);
           if(!(file.exists()))
             file.createNewFile();   //如果文件不存在,創(chuàng)建此文件
           if(file.length()>1048576) //如果文件大于1M,重新創(chuàng)建一個文件
             continue;
             FileOutputStream foo=new FileOutputStream("yeeyoo.log"+i,true);//以append方式打開創(chuàng)建文件
             foo.write(logString.getBytes(),0,logString.length()); //寫入日志字符串
             foo.close();
             break;//退出
          }
             }catch(FileNotFoundException e){}
              catch(IOException e){}
          }

          }

          登錄日志的實現(xiàn):

             下面再來看看我們的登錄Servlet中使用這個監(jiān)聽器的部分源代碼:
             ……
             HttpSession session = req.getSession (true);
             ……
             ///////////////////////////////////////////////////////////////////////
             SessionListener sessionListener=new SessionListener(" IP:"+req.getRemoteAddr());  //對于每一個會話過程均啟動一個監(jiān)聽器
                 session.setAttribute("listener",sessionListener);  //將監(jiān)聽器植入HttpSession,這將激發(fā)監(jiān)聽器調(diào)用valueBound方法,從而記錄日志文件。
            ///////////////////////////////////////////////////////////////////////
             當(dāng)系統(tǒng)退出登錄時,只需簡單地調(diào)用session.removeAttribute(“listener”);即可自動調(diào)用監(jiān)聽器的valueUnbound方法。或者,當(dāng)Session Time Out的時候也會調(diào)用此方法。


          登錄人數(shù)的統(tǒng)計:
          ServletContext session1=getServletConfig().getServletContext();//取得ServletContext對象實例
                if((SessionListener)session1.getAttribute("listener1")==null)
                {
                 SessionListener sessionListener1=new SessionListener("count");//只設(shè)置一次,不同于上面日志文件的記錄每次會話均設(shè)置。即當(dāng)?shù)谝粋€客戶連接到服務(wù)器時啟動一個全局變量,此后所有的客戶將使用相同的上下文。
                 session1.setAttribute("listener1",sessionListener1);//將監(jiān)聽器對象設(shè)置成ServletContext的屬性,具有全局范圍有效性,即所有的客戶均可以取得它的實例。
                }
                session.setAttribute("listener1",(SessionListener)session1.getAttribute("listener1"));//取出此全局對象,并且將此對象綁定到某個會話中,此舉將促使監(jiān)聽器調(diào)用valueBound,計數(shù)器加一。
             在此后的程序中隨時可以用以下代碼取得當(dāng)前的登錄人數(shù):
             ((SessionListener)session.getAttribute("listener1")).getCount()
             getCount()是監(jiān)聽器的一個方法,即取得當(dāng)前計數(shù)器的值也就是登錄人數(shù)了。

          -------------------------------------------------------------------------------->>>>>>>>>>


          另一個代碼:

          你應(yīng)該對user object 實現(xiàn)HttpSessionBindingListener的valueBound和valueUnbound方法
          ____________________________________________________________
          import javax.servlet.*;
          import javax.servlet.http.*;

          public class UseridWrapper implements HttpSessionBindingListener
          {
          public String userid = "0";
          public User(String id)
          {
          this.userid = id;
          }
          public void valueBound(HttpSessionBindingEvent e)
          {
             System.out.println("the user with id: "+this.userid+" logon!");
          //here can use one Singleton object to manage the user list,
            //ex: UserManager.add(this);

          }
          public void valueUnbound(HttpSessionBindingEvent e)
          {
            System.out.println("the user with id"+this.userid+"  exit!");
            //here can use one Singleton object to manage the user list,
            //ex: UserManager.remover(this);
          }
          }
          //______________________________________________________________
          使用:
          User user=new User("1");
          session.setAttribute("Login",);//觸發(fā)valueBound事件
          使用:
          session.invalidate() or session is timeout 觸發(fā)valueUnbound()
          >>>>>>
          //______________________________________________________________
          使用:
          UseridWrapper user=new UseridWrapper ("1");
          session.setAttribute("Login",user);//觸發(fā)valueBound事件
          使用:
          session.invalidate() or session is timeout 觸發(fā)valueUnbound()

             以上代碼基本可實現(xiàn)在線統(tǒng)計功能,加以修改就可以了

          主站蜘蛛池模板: 桂平市| 靖宇县| 繁昌县| 庄浪县| 罗江县| 普陀区| 连州市| 那曲县| 读书| 石河子市| 肥西县| 绿春县| 毕节市| 冀州市| 巴彦淖尔市| 临桂县| 慈利县| 衡南县| 秦皇岛市| 加查县| 堆龙德庆县| 循化| 应用必备| 两当县| 仙桃市| 洮南市| 盐边县| 阜新市| 定南县| 广元市| 库尔勒市| 伊吾县| 大渡口区| 昌图县| 十堰市| 南部县| 子长县| 旬阳县| 刚察县| 雷山县| 甘泉县|