java fx获取在线用户_关于使用HttpSessionBindingListener获取在线用户数,同一用户登陆一次...

考虑到项目中统计在线用户数量和同一用户只能登陆一次的需求,查询联系 HttpSessionBindingListener接口的使用,记录以备后用,也供同样需要的同仁参考。

下面为我的测试例子,首先建个web工程,例子中程序包括:OnLineUser.java  ,login.jsp ,logout.jsp,onLineUser.jsp四个文件

OnLineUser.java清单:

package pub;

None.gif

ExpandedBlockStart.gif/*

6a9c071a08f1dae2d3e1c512000eef41.png * onLineUser类实现HttpSessionBindingListener接口

6a9c071a08f1dae2d3e1c512000eef41.png * onLineUser类将具有HttpSessionBindingListener接口的特有属性

6a9c071a08f1dae2d3e1c512000eef41.png * 那么HttpSessionBindingListener接口的特有属性是什么呢?

6a9c071a08f1dae2d3e1c512000eef41.png * HttpSessionBindingListener接口具有的两个空函数

6a9c071a08f1dae2d3e1c512000eef41.png * public void valueBound(HttpSessionBindingEvent e)

6a9c071a08f1dae2d3e1c512000eef41.png * public void valueUnBound(HttpSessionBindingEvent e)

6a9c071a08f1dae2d3e1c512000eef41.png * 

6a9c071a08f1dae2d3e1c512000eef41.png * onLineUser实现一些方法:获取在线用户数

6a9c071a08f1dae2d3e1c512000eef41.png * 

ExpandedBlockEnd.gif */

None.gif

None.gifimport javax.servlet.http.*; 

None.gifimport java.util.*; 

None.gif

ExpandedBlockStart.gifpublic class OnLineUser implements HttpSessionBindingListener { 

6a9c071a08f1dae2d3e1c512000eef41.png   

ExpandedSubBlockStart.gif   public OnLineUser(){

ExpandedSubBlockEnd.gif   } 

6a9c071a08f1dae2d3e1c512000eef41.png

6a9c071a08f1dae2d3e1c512000eef41.png   private Vector users=new Vector();

ExpandedSubBlockStart.gif   public int getCount(){

6a9c071a08f1dae2d3e1c512000eef41.png       users.trimToSize();//调整Vector users的容量为Vector users的大小

6a9c071a08f1dae2d3e1c512000eef41.png       return users.capacity();//返回users的容量

ExpandedSubBlockEnd.gif   }

ExpandedSubBlockStart.gif   public boolean existUser(String userName){

6a9c071a08f1dae2d3e1c512000eef41.png       users.trimToSize();

6a9c071a08f1dae2d3e1c512000eef41.png       boolean existUser=false;

6a9c071a08f1dae2d3e1c512000eef41.png       for (int i=0;i

ExpandedSubBlockStart.gif       {

6a9c071a08f1dae2d3e1c512000eef41.png           if (userName.equals((String)users.get(i)))

ExpandedSubBlockStart.gif           {

6a9c071a08f1dae2d3e1c512000eef41.png               existUser=true;

6a9c071a08f1dae2d3e1c512000eef41.png               break;

ExpandedSubBlockEnd.gif           }

ExpandedSubBlockEnd.gif       }

6a9c071a08f1dae2d3e1c512000eef41.png       return existUser;

ExpandedSubBlockEnd.gif   }

6a9c071a08f1dae2d3e1c512000eef41.png

ExpandedSubBlockStart.gif   public boolean deleteUser(String userName) {

6a9c071a08f1dae2d3e1c512000eef41.png       users.trimToSize();

ExpandedSubBlockStart.gif       if(existUser(userName)){

6a9c071a08f1dae2d3e1c512000eef41.png           int currUserIndex=-1;

ExpandedSubBlockStart.gif           for(int i=0;i

ExpandedSubBlockStart.gif               if(userName.equals((String)users.get(i))){

6a9c071a08f1dae2d3e1c512000eef41.png                   currUserIndex=i;

6a9c071a08f1dae2d3e1c512000eef41.png                   break;

ExpandedSubBlockEnd.gif               }

ExpandedSubBlockEnd.gif           }

ExpandedSubBlockStart.gif           if (currUserIndex!=-1){

6a9c071a08f1dae2d3e1c512000eef41.png               users.remove(currUserIndex);

6a9c071a08f1dae2d3e1c512000eef41.png               users.trimToSize();

6a9c071a08f1dae2d3e1c512000eef41.png               return true;

ExpandedSubBlockEnd.gif           }

ExpandedSubBlockEnd.gif       }

6a9c071a08f1dae2d3e1c512000eef41.png       return false;

ExpandedSubBlockEnd.gif   }

6a9c071a08f1dae2d3e1c512000eef41.png

6a9c071a08f1dae2d3e1c512000eef41.png   public Vector getOnLineUser()

ExpandedSubBlockStart.gif   {

6a9c071a08f1dae2d3e1c512000eef41.png       return users;

ExpandedSubBlockEnd.gif   }

6a9c071a08f1dae2d3e1c512000eef41.png   

ExpandedSubBlockStart.gif   public void valueBound(HttpSessionBindingEvent e) { 

6a9c071a08f1dae2d3e1c512000eef41.png       users.trimToSize();

6a9c071a08f1dae2d3e1c512000eef41.png       System.out.println("请求:::::::::::"+e.getName());

ExpandedSubBlockStart.gif    if(!existUser(e.getName())){

6a9c071a08f1dae2d3e1c512000eef41.png        users.add(e.getName());

6a9c071a08f1dae2d3e1c512000eef41.png        System.out.print(e.getName()+"    登入到系统 "+(new Date()));

6a9c071a08f1dae2d3e1c512000eef41.png        System.out.println("      在线用户数为:"+getCount());

ExpandedSubBlockStart.gif    }else{

6a9c071a08f1dae2d3e1c512000eef41.png        System.out.println(e.getName()+"已经存在");

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif   } 

6a9c071a08f1dae2d3e1c512000eef41.png 

ExpandedSubBlockStart.gif   public void valueUnbound(HttpSessionBindingEvent e) { 

6a9c071a08f1dae2d3e1c512000eef41.png    users.trimToSize();

6a9c071a08f1dae2d3e1c512000eef41.png    String userName=e.getName();

6a9c071a08f1dae2d3e1c512000eef41.png    deleteUser(userName);

6a9c071a08f1dae2d3e1c512000eef41.png    System.out.print(userName+"    退出系统 "+(new Date()));

6a9c071a08f1dae2d3e1c512000eef41.png    System.out.println("      在线用户数为:"+getCount());

ExpandedSubBlockEnd.gif   } 

6a9c071a08f1dae2d3e1c512000eef41.png   

6a9c071a08f1dae2d3e1c512000eef41.png   

ExpandedBlockEnd.gif}

login.jsp 清单:

None.gif

None.gif

None.gif

测试HttpSessionBindingListener接口

None.gif

None.gif

None.gif

None.gif

None.gif

None.gif

None.gif

None.gif

None.gif

logout.jsp清单:

ExpandedBlockStart.gif 

None.gif

None.gif 

None.gif

 

None.gif

搞定JSP在线人数

None.gif

None.gif

 

None.gif

 

None.gif  

登陆成功,欢迎您访问!

None.gif

ExpandedBlockStart.gif   String username=request.getParameter("username");

6a9c071a08f1dae2d3e1c512000eef41.png   if(username!=null&&onlineuser.deleteUser(username)){

6a9c071a08f1dae2d3e1c512000eef41.png       out.println(username+"已经退出系统!");

6a9c071a08f1dae2d3e1c512000eef41.png       session.removeAttribute(username);

6a9c071a08f1dae2d3e1c512000eef41.png       out.println("");

6a9c071a08f1dae2d3e1c512000eef41.png   }else{

6a9c071a08f1dae2d3e1c512000eef41.png       out.println(username+"已经退出系统!");

6a9c071a08f1dae2d3e1c512000eef41.png       out.println("");

ExpandedBlockEnd.gif   }

None.gif%> 

None.gif

 

None.gif  

elapsed制作

None.gif  

None.gif  

退出系统

None.gif

 

None.gif 

None.gif

onLineUser.jsp清单

ExpandedBlockStart.gif

None.gif

None.gif 

None.gif

 

None.gif

搞定JSP在线人数

None.gif

None.gif

 

None.gif

 

None.gif  

登陆成功,欢迎您访问!

None.gif

ExpandedBlockStart.gif 

ExpandedBlockStart.gif   String username=request.getParameter("username");

6a9c071a08f1dae2d3e1c512000eef41.png   if (onlineuser.existUser(username)){

6a9c071a08f1dae2d3e1c512000eef41.png       out.println("用户"+username+"已经登陆!");

6a9c071a08f1dae2d3e1c512000eef41.png   }else{

6a9c071a08f1dae2d3e1c512000eef41.png       session.setMaxInactiveInterval(600);//Sesion有效时长,以秒为单位

6a9c071a08f1dae2d3e1c512000eef41.png       session.setAttribute(username,onlineuser); 

6a9c071a08f1dae2d3e1c512000eef41.png       out.println("欢迎新用户:"+username+"登陆到系统!");

6a9c071a08f1dae2d3e1c512000eef41.png   }

6a9c071a08f1dae2d3e1c512000eef41.png   out.println("
当前在线用户人数:"+onlineuser.getCount()+"
");

6a9c071a08f1dae2d3e1c512000eef41.png   Vector vt=onlineuser.getOnLineUser();

6a9c071a08f1dae2d3e1c512000eef41.png   Enumeration e = vt.elements();

6a9c071a08f1dae2d3e1c512000eef41.png   out.println("在线用户列表");

6a9c071a08f1dae2d3e1c512000eef41.png   out.println("

6a9c071a08f1dae2d3e1c512000eef41.png   out.println("

用户名");

6a9c071a08f1dae2d3e1c512000eef41.png   /*while(e.hasMoreElements()){

6a9c071a08f1dae2d3e1c512000eef41.png       out.println("

");

6a9c071a08f1dae2d3e1c512000eef41.png       out.println((String)e.nextElement()+"
");

6a9c071a08f1dae2d3e1c512000eef41.png       out.println("

");

6a9c071a08f1dae2d3e1c512000eef41.png   }

6a9c071a08f1dae2d3e1c512000eef41.png   下面的方法也是可以的,这两种方法都是可行的,其实Iterator是Enumeration的子类

6a9c071a08f1dae2d3e1c512000eef41.png   */

6a9c071a08f1dae2d3e1c512000eef41.png   for(Iterator it=vt.iterator();it.hasNext();){

6a9c071a08f1dae2d3e1c512000eef41.png       out.println("

");

6a9c071a08f1dae2d3e1c512000eef41.png       out.println((String)it.next()+"
");

6a9c071a08f1dae2d3e1c512000eef41.png       out.println("

");

6a9c071a08f1dae2d3e1c512000eef41.png   }

6a9c071a08f1dae2d3e1c512000eef41.png   out.println("

");

ExpandedBlockEnd.gif     

None.gif%> 

None.gif

 

None.gif  

elapsed制作

None.gif  

ExpandedBlockStart.gif   out.println("

退出系统

");

None.gif%>

None.gif

 

None.gif 

None.gif

该监听器不需要在web.xml中配置listener,完成后部署启动,访问login.jsp测试即可。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值