甜咖啡

我的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 甜咖啡 阅读(1151) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航:
 

导航

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

统计

常用链接

留言簿(1)

我参与的团队

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜