关于使用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 ...
随机推荐
- 求一组数字序列的分布情况(java)
最近需要做一个正态分布的函数图像所以要处理一段double序列 写了这个算法 先上效果图: 核心思想: 1先根据步长计算每一个区间 2循环进行判断序列中每个数属于哪个区间 3用一个数组来保存每一个区 ...
- python 3.5 之 单双三引号
1. 单引号和双引号用法都是一样的,但是如果字符串里有相同的字符时要使用\进行转义 举例:1) print 'hello'2) print "hello"1和2,结果都是hello ...
- 运行加速多线程和GPU
http://blog.csdn.net/augusdi/article/details/11883003
- (转)(VS2013 )由于应用程序配置不正确,程序未能启动”--原因及解决方法
今天把别人的程序拿过来编译时通过,但是运行的时候,提示:由于应用程序配置不正确,程序未能启动 搜了一下,各种方法.最终通过下面的方法解决的. 项目--->配置属性---->链接器----& ...
- mySql-数据库之存储过程学习总结
之前在工作中总是听别人提到存储过程,觉得是个很高深的东西,利用工作之余,看了下相关的知识,现将学习知识总结如下,希望可以为刚学习的人提供些许帮助. 开发环境:Navicat For Mysql. My ...
- oracle 序列
查询序列值 select td_prodline_attr_seq.nextval from dual 查询用户建的所有序列 用户名 必须大写select SEQUENCE_OWNER,SEQ ...
- 【搜索引擎Jediael开发笔记2】使用HttpClient下载网页至本地文件
本文使用HttpClient根据url进行网页下载.其中 (1)HttpClient的相关知识请参见HttpClient基础教程 (2) package org.ljh.search.download ...
- C# 弗洛伊德(Floyd)算法
弗洛伊德(Floyd)算法 主要是用于计算图中所有顶点对之间的最短距离长度的算法,如果是要求某一个特定点到图中所有顶点之间的最短距离可以用; ; ; ; ...
- vc6.0 使用Ado 连接MS-SqlServer2000 连接字符串
vc6.0 使用Ado 连接MS-SqlServer2000 连接字符串 分类: C/C++ VC 2012-04-12 20:23 836人阅读 评论(0) 收藏 举报 sql server数据库服 ...
- [置顶] 获取系统时间的方法--linux
一. localtime 函数获取(年/月/日/时/分/秒)数值. 1.感性认识 #include<time.h> //C语言的头文件 #include<stdio.h> ...