关于使用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 ...
随机推荐
- SQL中Truncate的用法(转)
转自:http://www.studyofnet.com/news/555.html 本文导读:删除表中的数据的方法有delete,truncate, 其中TRUNCATE TABLE用于删除表中的所 ...
- Windows Server 2008 R2 IIS重装
背景描述: 在一个刚睡醒午觉后的下午,忽然收到客户反馈,说昨天开始应用特别卡,各种卡各种不好用,忽然想到上次说要优化服务器IIS配置还一直没弄,然后迷迷糊糊的就开始进行客户现场服务器IIS配置优化,涉 ...
- 天坑 之 java web servlet+jsp项目 配置后 404 (MyEclipse转eclipse)
最近搞一个自己的博客系统玩,用了servlet+jsp,结果发现了两个大问题: 1.无法 Export 出 WAR文件: 2.生成WAR,放置到TOMCAT的 webapps目录后,http://lo ...
- Remove Duplicate Letters
316. Remove Duplicate Letters Total Accepted: 2367 Total Submissions: 12388 Difficulty: Medium Given ...
- 前端利器,如何使用fiddle拦截在线css进行先下调试
fiddle的功能相当的强悍,用户也非常广,不过今天我就教大家用fiddle进行前端调试. 首先下载软件fiddle,点击对应的版本下载安装. 安装成功后打开看到右侧的导航栏: 点击AutoRespo ...
- STL初步
1.stackstack 模板类的定义在<stack>头文件中.stack 模板类需要两个模板参数,一个是元素类型,一个容器类型,但只有元素类型是必要的,在不指定容器类型时,默认的容器类型 ...
- how to use http.Agent in node.js
Actually now that I look at the Agent code, it looks like it sets maxSockets on a per domain basis i ...
- android 管理Bitmap内存 - 开发文档翻译
由于本人英文能力实在有限,不足之初敬请谅解 本博客只要没有注明“转”,那么均为原创,转贴请注明本博客链接链接 Managing Bitmap Memory 管理Bitmap内存 In additi ...
- 任何时候都适用的20个C++技巧
这些小技巧之所以特别,是因为这些信息通常吧不能在C++书籍或者网站上找到.比如说,成员指针,即使对于高级程序员也是比较棘手,和易于产生bugs的,是应该尽量避免的问题之一. <翻 by凌云健笔& ...
- libevent evbuffer bug
今天发现 libevent 2.0.22 一个坑爹的bug,导致消息混乱.查找问题浪费一天,复现代码如下 #include <event2/buffer.h> #include <s ...