关于使用HttpSessionBindingListener获取在线用户数,同一用户登陆一次
原创地址:http://blog.csdn.net/jiaoxueli/article/details/2226134
考虑到项目中统计在线用户数量和同一用户只能登陆一次的需求,查询联系 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><a 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>
</center>
<% session = request.getSession(false); %>
<%
String username=request.getParameter("username");
if (onlineuser.existUser(username)){
out.println("用户<font color=red>"+username+"</font>已经登陆!");
}else{
session.setMaxInactiveInterval(600);//Sesion有效时长,以秒为单位
session.setAttribute(username,onlineuser);
out.println("欢迎新用户:<font color=red>"+username+"</font>登陆到系统!");
}
out.println("<br>当前在线用户人数:<font color=red>"+onlineuser.getCount()+"</font><br>");
Vector vt=onlineuser.getOnLineUser();
Enumeration e = vt.elements();
out.println("在线用户列表");
out.println("<table border=1>");
out.println("<tr><td>用户名</td></tr>");
/*while(e.hasMoreElements()){
out.println("<tr><td>");
out.println((String)e.nextElement()+"<br>");
out.println("</td></tr>");
}
下面的方法也是可以的,这两种方法都是可行的,其实Iterator是Enumeration的子类
*/
for(Iterator it=vt.iterator();it.hasNext();){
out.println("<tr><td>");
out.println((String)it.next()+"<br>");
out.println("</td></tr>");
}
out.println("</table>");
%>
<center>
<p>elapsed制作</p>
<p> </p>
<%
out.println("<p><a href='logout.jsp?username="+username+"'>退出系统</a></p>");
%>
</center>
</body>
</html>
该监听器不需要在web.xml中配置listener,完成后部署启动,访问login.jsp测试即可。
关于使用HttpSessionBindingListener获取在线用户数,同一用户登陆一次的更多相关文章
- 基于tomcat获取在线用户数
https://blog.csdn.net/smallnetvisitor/article/details/84697505 需求: 统计某应用的在线用户数 实现方案: 1.基于session监听(复 ...
- 阶段5 3.微服务项目【学成在线】_day18 用户授权_17-细粒度授权-获取当前用户信息
3.4.1需求分析 要想实现只查询自己的课程信息则需要获取当前用户所属的企业id. 1.认证服务在用户认证通过将用户所属公司id等信息存储到jwt令牌中. 2.用户请求到达资源服务后,资源服务需要取出 ...
- Python GUI之tkinter窗口视窗教程大集合(看这篇就够了) JAVA日志的前世今生 .NET MVC采用SignalR更新在线用户数 C#多线程编程系列(五)- 使用任务并行库 C#多线程编程系列(三)- 线程同步 C#多线程编程系列(二)- 线程基础 C#多线程编程系列(一)- 简介
Python GUI之tkinter窗口视窗教程大集合(看这篇就够了) 一.前言 由于本篇文章较长,所以下面给出内容目录方便跳转阅读,当然也可以用博客页面最右侧的文章目录导航栏进行跳转查阅. 一.前言 ...
- PHP统计当前在线用户数实例
HTML 我们在页面上放置一个显示当前在线人数的div#total以及一个用于展示访客地区分布的列表#onlinelist,默认我们在列表中放置一张与加载动画图片,后面我们用jQuery控制当鼠标滑向 ...
- websocket 无需通过轮询服务器的方式以获得响应 同步在线用户数 上线下线 抓包 3-way-handshake web-linux-shell 开发
https://code.google.com/archive/p/phpwebsocket/source/default/source The WebSocket API (WebSockets) ...
- 基于express+redis高速实现实时在线用户数统计
作者:zhanhailiang 日期:2014-11-09 本文将介绍怎样基于express+redis高速实现实时在线用户数统计. 1. 在github.com上创建项目uv-tj.将其同步到本地: ...
- 在线用户数-Constants
package com.pb.news.constants; public class Constants { public static int ONLINE_USER_COUNT=0;//在线用户 ...
- 获取用户登陆所在的ip及获取所属信息
package com.tcl.topsale.download.entity; public class GeoLocation { private String countryCode; priv ...
- 转:通过ASP.Net页面获取域用户名(当前登陆的用户)
通过ASP.Net页面获取域用户名(当前登陆的用户) 原文地址: https://www.cnblogs.com/fast-michael/archive/2011/03/14/2057954.htm ...
随机推荐
- 导出Eclipse环境配置
第一种方法: Eclipse的 File -> Export(导出), 在窗口中展开 General(常规) -> Perferences(首选项)-->Export all(全部导 ...
- iOS 获取系统目录
//获取根目录 NSString *homePath = NSHomeDirectory(); NSLog(@"Home目录:%@",homePath); //获取Document ...
- jquery mobile 入门
简介:jQuery Mobile框架可以轻松的帮助我们实现非常好看的.可跨设备的Web应用程序.我们将后续的介绍中向大家介绍大量的代码及实例. jQuery一直以来都是非常流行的富客户端及Web应用程 ...
- V - stl 的 优先队列 Ⅱ
Description Because of the wrong status of the bicycle, Sempr begin to walk east to west every morni ...
- 滑动冲突的补充——Event的流程走向
一.之前分析的滑动冲突,并没有讲述event事件是如何分发到不同的控件 View的滑动冲突 现在分析一下滑动冲突event事件的流向 假设: 我们的一个事件为 点下——>左滑动一次——> ...
- linux 命令总结(转载)
linux 命令总结(转载) 1. 永久更改ip ifconfig eth0 新ip 然后编辑/etc/sysconfig/network-scripts/ifcfg-eth0,修改ip 2.从Lin ...
- vs2010中看不见类视图和资源视图的解决方法
vs2010工程中,因为删除了“vcxproj.filter”文件,所以导致资源视图看不见了. 解决方法是:先关掉工程,将工程对应的扩展名为.suo和.sdf删除,重新打开解决方案,问题解决.
- [Linux] rlwrap - 解决Linux下sqlplus退格、上翻键乱码问题
在Linux下使用sqlplus你会发现:退格键无法正常使用(乱码),上翻键也无法正常显示历史功能,非常讨厌! 为了让退格键和上翻键在sqlplus里正常发挥它的作用,我们必须安装一个软件 - rlw ...
- 使用工具来提升Android开发效率
正所谓工欲善其事,必先利其器.学习并应用优秀的轮子,能够让我们跑的更快,走的更远.这里所指的工具是广义的,泛指能帮助我们开发的东西,或者能提高我们效率的东西,包含:开发工具.监測工具.第三方代码库等. ...
- check cable connection PXE-M0F: Exiting intel PXE ROM no bootable device-- insert boot disk and pre
今天修电脑遇到一个问题,新买的电脑的原装的是linux,然后我按常规方式进入PE后重装系统,然后开机一直显示下面的代码,进不去: check cable connection PXE-M0F: Exi ...