Jsp使用HttpSessionBindingListener实现在线人数记录
onLineUser.java 继承HttpSessionBindingListener实现在线人数记录功能
package com.trs; import java.util.*;
import javax.servlet.http.*;
import javax.servlet.*; /**
*HttpSessionBindingListener接口有两方需要实现的方法:
*public synchronized void valueBound(HttpSessionBindingEvent httpsessionbindingevent)
*public synchronized void valueUnbound(HttpSessionBindingEvent httpsessionbindingevent)
*Session创建的时候Servlet容器将会调用valueBound方法;Session删除的时候则调用valueUnbound方法.
*/
public class onLineUser implements HttpSessionBindingListener
{
public onLineUser()
{
} //保存在线用户的向量
private Vector users=new Vector(); //得到用户总数
public int getCount()
{
users.trimToSize();
return users.capacity();
} //判断是否存在指定的用户
public boolean existUser(String userName)
{
users.trimToSize();
boolean existUser=false;
for (int i=0;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
{
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();
if(!existUser(e.getName()))
{
users.add(e.getName());
System.out.print(e.getName()+"\t 登入到系统\t"+(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+"\t 退出系统\t"+(new Date()));
System.out.println(" 在线用户数为:"+getCount());
}
}
logout.jsp
<%@ page contentType="text/html;charset=GB2312" pageEncoding="GBK"%>
<%@ page import="com.trs.onLineUser,java.util.*" %>
<jsp:useBean id="onlineuser" class="com.trs.onLineUser" scope="application"/>
<html>
<head>
<title>show</title>
</head>
<body>
<%
String name=(String)session.getValue("name");
if(name!=null && name.length()!=0)
{
if(onlineuser.deleteUser(name))
out.println(name+"已经退出系统!");
else
out.println(name+"没有登陆到系统!");
}
%>
</body>
</html>
online.jsp
<%@ page contentType="text/html;charset=GB2312" pageEncoding="GBK"%>
<%@page import="com.trs.onLineUser,java.util.*" %>
<html>
</body>
<%
String name=request.getParameter("name");
String password=request.getParameter("password"); if(name!=null && password!=null)
{
Cookie cookie1=new Cookie("name", name);
cookie1.setMaxAge(100000);
response.addCookie(cookie1); Cookie cookie2=new Cookie("password", password);
cookie2.setMaxAge(100000);
response.addCookie(cookie2);
out.println("完成书写Cookie!");
}
else
{
out.println("书写失败!");
}
%>
</body>
</html>
需要说明的是这种方式适合只有单台服务器的小网站使用,如果网站有多台web server则不能使用这种方式记录在线人数。
Jsp使用HttpSessionBindingListener实现在线人数记录的更多相关文章
- 用HttpSessionListener与HttpSessionBindingListener实现在线人数统计
在线人数统计方面的实现,最初我的想法是,管理session,如果session销毁了就减少,如果登陆用户了就新增一个,但是如果是用户非法退出,如:未注销,关闭浏览器等,这个用户的session是管理不 ...
- [转]用HttpSessionListener与HttpSessionBindingListener实现在线人数统计
原文链接:http://www.cnblogs.com/shencheng/archive/2011/01/07/1930227.html 下午比较闲(其实今天都很闲),想了一下在线人数统计方面的实现 ...
- Java遇见HTML——JSP篇之商品浏览记录的实现
一.项目总体介绍 使用Cookie实现商品浏览记录. 要实现这个程序采取的是Model1(Jsp+JavaBean)架构实现,具体步骤: 首先要有个数据库,商品表,操作数据库的一个类DBHelper类 ...
- springMVC笔记:jsp页面获取后台数据记录列表
1.读取数据库中的记录List<HashMap<String,String>> attributes; 2.Controller构造Model如下: @RequestMappi ...
- JSP标签使用的代码记录——《%= %》(神奇的CSDN为啥标题不让打英文的尖括号)
关于JSP的一些标签,在用到的时候有些生疏,就去找了找资源重新温习了一下. 附上两个JSP<%= %>标签的博客,同时也记录当前项目里用到的方法. jsp页面中<%@ %>.& ...
- jsp简单实现统计在线人数
通过重写HttpSessionListener接口实现 //session操作类 public class SessionMap { private static Map<String,Http ...
- jsp利用application统计在线人数的方法
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...
- 用jsp的application写一个记录用户登陆网站的数量
</head><body><%int i = 0;Object number = application.getAttribute("num");if ...
- jsp内置对象学习记录
1.session,是一个会话保留在服务器端的对象(默认保留时间为30分钟),所以我们可以在session里面放用户信息以便后续的访问便利(缺点:cookie劫持,导致用户数据泄露).案例:(1)同个 ...
随机推荐
- HDU1429 胜利大逃亡 状压bfs
http://acm.hdu.edu.cn/viewcode.php?rid=22225154 因为总共a-j有10种钥匙,所以可以把有没有钥匙的状态压到一个int数里,然后dfs. 昨天状态特别不好 ...
- 【8.30校内测试】【找规律模拟】【DP】【二分+贪心】
对于和规律或者数学有关的题真的束手无策啊QAQ 首先发现两个性质: 1.不管中间怎么碰撞,所有蚂蚁的相对位置不会改变,即后面的蚂蚁不会超过前面的蚂蚁或者落后更后面的蚂蚁. 2.因为所有蚂蚁速度一样,不 ...
- PHP 登录DEMO
logintest.php 页面 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "h ...
- python开发_tarfile_文档归档压缩|解压缩
''' python中的tarfile模块实现文档的归档压缩和解压缩 功能: 把工作空间下面的所有文件,打包生成一个tar文件 同时提供一个方法把该tar文件中的一些文件解压缩到 指定的目录中 ''' ...
- [转]Jquery实现页面定时跳转
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- HDU 5301 Buildings 数学
Buildings 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5301 Description Your current task is to m ...
- Trigger a TTL circuit from ECL levels
ECL circuits typically have relatively small logic spans of approximately 800 mV. Because of the sma ...
- linux查看端口被哪个服务占用的命令
netstat -tunpl | grep 6379
- python笔记22-literal_eval函数处理返回json中的单双引号
前言 在做接口测试的时候,最常见的接口返回数据就是json类型,json类型数据实际上就是字串,通常标准的json格式是可以转化成python里面的对应的数据类型的 有时候开发返回的数据比较坑,不按常 ...
- pytest文档3-pycharm运行pytest
前言 上一篇pytest文档2-用例运行规则已经介绍了如何在cmd执行pytest用例,平常我们写代码在pycharm比较多 写完用例之后,需要调试看看,是不是能正常运行,如果每次跑去cmd执行,太麻 ...