甜咖啡

          我的IT空間

          servlet監聽器判斷用戶的session是否還存

          //   SessionListener.java    
             
               
             
            import  
          java.io.*;    
             
            import   java.util.*;    
             
            import  
          javax.servlet.http.*;    
             
               
             
            //監聽登錄的整個過程    
           
           
            public   class   SessionListener   implements    
             
             
           
          HttpSessionBindingListener    
             
            {    
             
               
             

            public   String   privateInfo="";   //生成監聽器的初始化參數字符串    
             
           
          private   String   logString="";   //日志記錄字符串    
             
            private   int  
          count=0;   //登錄人數計數器    
             
               
             
            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();   //如果文件不存在,創建此文件    
             
           
          if(file.length()>1048576)   //如果文件大于1M,重新創建一個文件    
             
            continue;  
           
             
            FileOutputStream   foo=new   FileOutputStream  
             
             

            ("yeeyoo.log"+i,true);//以append方式打開創建文件    
             
           
          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();   //如果文件不存在,創建此文件    
             
           
          if(file.length()>1048576)   //如果文件大于1M,重新創建一個文件    
             
            continue;  
           
             
            FileOutputStream   foo=new   FileOutputStream  
             
             

            ("yeeyoo.log"+i,true);//以append方式打開創建文件    
             
           
          foo.write(logString.getBytes(),0,logString.length());   //寫入日志  
             
             

            字符串    
             
            foo.close();    
             
            break;//退出    
           
           
            }    
             
            }catch(FileNotFoundException   e){}    
             

            catch(IOException   e){}    
             
            }    
             
               
             

            }    
             
               
             
            登錄日志的實現:    
             
               
           
           
            下面再來看看我們的登錄Servlet中使用這個監聽器的部分源代碼:    
             
            ……    
             
           
          HttpSession   session   =   req.getSession   (true);    
             
            ……    

             
          ////////////////////////////////////////////////////////////////
             
             
            ///////    
             
            SessionListener  
          sessionListener=new   SessionListener("    
             
             
           
          IP:"+req.getRemoteAddr());   //對于每一個會話過程均啟動一個監聽器    
             
           
          session.setAttribute("listener",sessionListener);   //將監聽器植入  
             
             

            HttpSession,這將激發監聽器調用valueBound方法,從而記錄日志文件  
             
             
            。    

             
            ////////////////////////////////////////////////////////////////  

             
             
            ///////    
             
           
          當系統退出登錄時,只需簡單地調用session.removeAttribute  
             
             
           
          (“listener”);即可自動調用監聽器的valueUnbound方法。或者,當  
             
             
            Session  
          Time   Out的時候也會調用此方法。    
             
               
             
               
             
           
          登錄人數的統計:    
             
            ServletContext  
          session1=getServletConfig().getServletContext  
             
             
           
          ();//取得ServletContext對象實例    
             
           
          if((SessionListener)session1.getAttribute("listener1")==null)    
             
           
          {    
             
            SessionListener   sessionListener1=new  
          SessionListener("count");//  
             
             
           
          只設置一次,不同于上面日志文件的記錄每次會話均設置。即當第一個客  
             
             
           
          戶連接到服務器時啟動一個全局變量,此后所有的客戶將使用相同的上下  
             
             
            文。    
             
           
          session1.setAttribute("listener1",sessionListener1);//將監聽器對  
             
             

            象設置成ServletContext的屬性,具有全局范圍有效性,即所有的客戶均  
             
             
            可以取得它的實例。
             
             
            }    
             
           
          session.setAttribute("listener1",(SessionListener)  
             
             
           
          session1.getAttribute("listener1"));//取出此全局對象,并且將此對  
             
             
           
          象綁定到某個會話中,此舉將促使監聽器調用valueBound,計數器加一。    
             
           
          在此后的程序中隨時可以用以下代碼取得當前的登錄人數:    
             
           
          ((SessionListener)session.getAttribute("listener1")).getCount()    
             

            getCount()是監聽器的一個方法,即取得當前計數器的值也就是登錄人數  
             
             
            了。

          修改web.xml,增加:  
             
               
             
          <listener>
             

            <listener-class>SessionListener</listener-class>    
             

            </listener>  
             
               
             
                   
          <servlet-mapping>  
             
                           
          <servlet-name>SessionListener</servlet-name>  
             
                   
                  <url-pattern>/servlet/SessionListener</url-pattern>  
           
           
                    </servlet-mapping>  
             
               
             
                 
            <servlet>  
             
                           
          <servlet-name>SessionListener</servlet-name>  
             
                   
                  <servlet-class>SessionListener</servlet-class>  
             

            </servlet>  

          posted on 2011-07-13 16:19 甜咖啡 閱讀(1166) 評論(0)  編輯  收藏


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


          網站導航:
           

          導航

          <2011年7月>
          262728293012
          3456789
          10111213141516
          17181920212223
          24252627282930
          31123456

          統計

          常用鏈接

          留言簿(1)

          我參與的團隊

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 武宣县| 南陵县| 赤壁市| 石柱| 上饶县| 大丰市| 德令哈市| 英吉沙县| 竹山县| 射阳县| 安陆市| 麻江县| 台北县| 长沙市| 筠连县| 东辽县| 洛阳市| 宿州市| 会同县| 进贤县| 三明市| 唐海县| 台北市| 通渭县| 新邵县| 新丰县| 黄冈市| 东辽县| 云南省| 普兰县| 喜德县| 巴彦县| 桂林市| 化德县| 南涧| 诸暨市| 汉中市| 内丘县| 怀化市| 白水县| 清水县|