使用HttpSessionBindingListener方法

先引用《head first servlet and jsp》中的一段描述

2.


下面的文章转自:http://yufenfei.iteye.com/blog/439050

捕获Session事件的意义:
1、    记录网站的客户登录日志(登录,退出信息等)
2、    统计在线人数
3、    等等还有很多,呵呵,自己想吧……总之挺重要的。

Session代表客户的会话过程,客户登录时,往Session中传入一个对象,即可跟踪客户的会话。在Servlet中,传入Session的 对象如果是一个实现HttpSessionBindingListener接口的对象(方便起见,此对象称为监听器),则在传入的时候(即调用 HttpSession对象的setAttribute方法的时候)和移去的时候(即调用HttpSession对象的removeAttribute方 法的时候或Session Time out的时候)Session对象会自动调用监听器的valueBound和valueUnbound方法(这是 HttpSessionBindingListener接口中的方法)。
由此可知,登录日志也就不难实现了。
另外一个问题是,如何统 计在线人数,这个问题跟实现登录日志稍微有点不同,统计在线人数(及其信息),就是统计现在有多少个Session实例存在,我们可以增加一个计数器(如 果想存储更多的信息,可以用一个对象来做计数器,随后给出的实例中,简单起见,用一个整数变量作为计数器),通过在valueBound方法中给计数器加 1,valueUnbound方法中计数器减1,即可实现在线人数的统计。当然,这里面要利用到ServletContext的全局特性。(有关 ServletContext的叙述请参考Servlet规范),新建一个监听器,并将其实例存入ServletContext的属性中,以保证此监听器 实例的唯一性,当客户登录时,先判断ServletContext的这个属性是否为空,如果不为空,证明已经创建,直接将此属性取出放入Session 中,计数器加1;如果为空则创建一个新的监听器,并存入ServletContext的属性中。

举例说明:

实现一个监听器:
// 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()是监听器的一个方法,即取得当前计数器的值也就是登录人数了。

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


另一个代码:

你应该对user object 实现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",);//触发valueBound事件
使用:
session.invalidate() or session is timeout 触发valueUnbound()
>>>>>>
//______________________________________________________________
使用:
UseridWrapper user=new UseridWrapper ("1");
session.setAttribute("Login",user);//触发valueBound事件
使用:
session.invalidate() or session is timeout 触发valueUnbound()

   以上代码基本可实现在线统计功能,加以修改就可以了



另外一个

使用HttpSessionListener

可以参照下面链接 http://book.51cto.com/art/201104/253576.htm


一个统计在线人数的例子

参照链接http://www.blogjava.net/tscfengkui/archive/2011/01/18/343139.html

转载于:https://my.oschina.net/liangzhenghui/blog/185008

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值