HttpSessionBindingListener获取在线用户数,同一用户登陆一次

本文介绍了一个基于JSP实现的在线用户管理系统,通过实现HttpSessionBindingListener接口来统计在线用户数量,并确保同一用户只能登录一次。系统包含OnLineUser类及login.jsp、logout.jsp和onLineUser.jsp三个页面。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

项目中统计在线用户数量和同一用户只能登陆一次的需求,查询联系 HttpSessionBindingListener接口的使用,记录以备后用

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

OnLineUser.java清单:

 

package pub;

/**//*
 * onLineUser类实现HttpSessionBindingListener接口
 * onLineUser类将具有HttpSessionBindingListener接口的特有属性
 * 那么HttpSessionBindingListener接口的特有属性是什么呢?
 * HttpSessionBindingListener接口具有的两个空函数
 * public void valueBound(HttpSessionBindingEvent e)
 * public void valueUnBound(HttpSessionBindingEvent e)
 * 
 * onLineUser实现一些方法:获取在线用户数
 * 
 
*/


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

public class OnLineUser implements HttpSessionBindingListener ...
   
   
public OnLineUser()...{
   }
 

   
private Vector users=new Vector();
   
public int getCount()...{
       users.trimToSize();
//调整Vector users的容量为Vector users的大小
       return users.capacity();//返回users的容量
   }

   
public boolean existUser(String userName)...{
       users.trimToSize();
       
boolean existUser=false;
       
for (int i=0;i<users.capacity();i++ )
       
...{
           
if (userName.equals((String)users.get(i)))
           
...{
               existUser
=true;
               
break;
           }

       }

       
return existUser;
   }


   
public boolean deleteUser(String userName) ...{
       users.trimToSize();
       
if(existUser(userName))...{
           
int currUserIndex=-1;
           
for(int i=0;i<users.capacity();i++)...{
               
if(userName.equals((String)users.get(i)))...{
                   currUserIndex
=i;
                   
break;
               }

           }

           
if (currUserIndex!=-1)...{
               users.remove(currUserIndex);
               users.trimToSize();
               
return true;
           }

       }

       
return false;
   }


   
public Vector getOnLineUser()
   
...{
       
return users;
   }

   
   
public void valueBound(HttpSessionBindingEvent e) ...
       users.trimToSize();
       System.out.println(
"请求:::::::::::"+e.getName());
    
if(!existUser(e.getName()))...{
        users.add(e.getName());
        System.out.print(e.getName()
+"    登入到系统 "+(new Date()));
        System.out.println(
"      在线用户数为:"+getCount());
    }
else...{
        System.out.println(e.getName()
+"已经存在");
        }

   }
 
 
   
public void valueUnbound(HttpSessionBindingEvent e) ...
    users.trimToSize();
    String userName
=e.getName();
    deleteUser(userName);
    System.out.print(userName
+"    退出系统 "+(new Date()));
    System.out.println(
"      在线用户数为:"+getCount());
   }
 
   
   
}
 


==========================================================================

login.jsp 清单:

 

<%...@ page contentType="text/html;charset=gb2312" %> 
<html>
<head>
<title>测试HttpSessionBindingListener接口</title>
</head>

<body>
<form name="login" method="post" action="onLineUser.jsp">
<input type=text name="username">
<input type=submit name="submit" value="登陆">
</form>
</body>
</html>

================================================================================

logout.jsp清单:

 

<%...@ page contentType="text/html;charset=gb2312" %> 
<%...@ page import="pub.OnLineUser,java.util.*" %> 
<jsp:useBean id="onlineuser" class="pub.OnLineUser" scope="application"/>
<html> 
<head> 
<title>搞定JSP在线人数</title>
</head>
<body> 
<center> 
  
<h1>登陆成功,欢迎您访问!</h1>
</center>
<%... 
   
String username=request.getParameter("username");
   
if(username!=null&&onlineuser.deleteUser(username)){
       out.println(username
+"已经退出系统!");
       session.removeAttribute(username);
       out.println(
"<script>window.location="login.jsp";</script>");
   }
else{
       out.println(username
+"已经退出系统!");
       out.println(
"<script>window.location="login.jsp";</script>");
   }
%> 
<center> 
  
<p>elapsed制作</p>
  
<p> </p>
  
<p><href="logout.jsp">退出系统</a></p>
</center> 
</body> 
</html>

 

==============================================================================

onLineUser.jsp清单

 

<%...@ page contentType="text/html;charset=gb2312" %> 
<%...@page import="java.util.*,pub.*"%>
<jsp:useBean id="onlineuser" class="pub.OnLineUser" scope="application" />
<html> 
<head> 
<title>搞定JSP在线人数</title>
</head>
<body> 
<center> 
  
<h1>登陆成功,欢迎您访问!</h1&
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值