博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Jsp使用HttpSessionBindingListener实现在线人数记录
阅读量:7102 次
发布时间:2019-06-28

本文共 3684 字,大约阅读时间需要 12 分钟。

onLineUser.java 继承HttpSessionBindingListener实现在线人数记录功能

1 package com.trs; 2   3 import java.util.*;   4 import javax.servlet.http.*;   5 import javax.servlet.*;   6   7 /** 8 *HttpSessionBindingListener接口有两方需要实现的方法: 9 *public synchronized void valueBound(HttpSessionBindingEvent httpsessionbindingevent)10 *public synchronized void valueUnbound(HttpSessionBindingEvent httpsessionbindingevent)11 *Session创建的时候Servlet容器将会调用valueBound方法;Session删除的时候则调用valueUnbound方法.12 */13 public class onLineUser implements HttpSessionBindingListener14 {  15     public onLineUser()16     { 17     } 18  19     //保存在线用户的向量20     private Vector users=new Vector();21  22     //得到用户总数23     public int getCount()24     { 25         users.trimToSize(); 26         return users.capacity(); 27     }28  29     //判断是否存在指定的用户30     public boolean existUser(String userName)31     { 32         users.trimToSize(); 33         boolean existUser=false; 34         for (int i=0;i35         { 36             if (userName.equals((String)users.get(i)))37             {38                 existUser=true; 39                 break;40             }41         }42         return existUser; 43     }44  45     //删除指定的用户46     public boolean deleteUser(String userName)47     { 48         users.trimToSize(); 49         if(existUser(userName))50         { 51             int currUserIndex=-1; 52             for(int i=0;i53             { 54                 if(userName.equals((String)users.get(i)))55                 { 56                     currUserIndex=i; 57                     break; 58                 } 59             } 60             if (currUserIndex!=-1)61             { 62                 users.remove(currUserIndex); 63                 users.trimToSize(); 64                 return true; 65             } 66         } 67         return false; 68     }69  70     //得到当前在线用户的列表71     public Vector getOnLineUser() 72     {73         return users; 74     } 75  76     public void valueBound(HttpSessionBindingEvent e)77     {  78         users.trimToSize(); 79         if(!existUser(e.getName()))80         { 81             users.add(e.getName()); 82             System.out.print(e.getName()+"\t  登入到系统\t"+(new Date())); 83             System.out.println("     在线用户数为:"+getCount()); 84         }else85             System.out.println(e.getName()+"已经存在"); 86     }  87  88     public void valueUnbound(HttpSessionBindingEvent e)89     {  90         users.trimToSize(); 91         String userName=e.getName(); 92         deleteUser(userName); 93         System.out.print(userName+"\t  退出系统\t"+(new Date())); 94         System.out.println("     在线用户数为:"+getCount()); 95     }96 }

logout.jsp

1 <%@ page contentType="text/html;charset=GB2312" pageEncoding="GBK"%> 2 <%@ page import="com.trs.onLineUser,java.util.*" %>   3 
4 5 6 show 7 8 9 <% 10 String name=(String)session.getValue("name");11 if(name!=null && name.length()!=0)12 {13 if(onlineuser.deleteUser(name)) 14 out.println(name+"已经退出系统!"); 15 else16 out.println(name+"没有登陆到系统!"); 17 }18 %>19 20

online.jsp

1 <%@ page contentType="text/html;charset=GB2312" pageEncoding="GBK"%> 2 <%@page import="com.trs.onLineUser,java.util.*" %> 3  4  5 <% 6 String name=request.getParameter("name"); 7 String password=request.getParameter("password"); 8   9 if(name!=null && password!=null)10 {11 Cookie cookie1=new Cookie("name", name); 12 cookie1.setMaxAge(100000);13 response.addCookie(cookie1);14  15 Cookie cookie2=new Cookie("password", password); 16 cookie2.setMaxAge(100000);17 response.addCookie(cookie2);18 out.println("完成书写Cookie!");19 }20 else21 {22 out.println("书写失败!");23 }24 %>25 26 

需要说明的是这种方式适合只有单台服务器的小网站使用,如果网站有多台web server则不能使用这种方式记录在线人数。

转载地址:http://xmkhl.baihongyu.com/

你可能感兴趣的文章
git的配置
查看>>
python2.0_s12_day19_前端结合后端展示客户咨询纪录
查看>>
angular中$location读取url信息
查看>>
POJ1837 Balance[分组背包]
查看>>
防火墙/IDS测试工具Ftester
查看>>
iOS WebSocket
查看>>
Java多线程 -- wait() 和 notify() 使用入门
查看>>
React@16.3 全新的Context API进阶教程
查看>>
区块链开发教程系列【加精】
查看>>
dubbo源码解析(四十一)集群——Mock
查看>>
前端面试问题汇总
查看>>
4.java数组
查看>>
MySQL数据类型优化
查看>>
蚂蚁金服核心技术:百亿特征实时推荐算法揭秘 ...
查看>>
阿里云智能技术战略架构师陈绪:透视2019云计算酣战 ...
查看>>
深度学习要多深,才能读懂人话?|阿里小蜜前沿探索 ...
查看>>
好程序员分享如何看待CSS中BEM的命名方式?
查看>>
国内唯一,阿里云入选全球区块链云服务报告,领先AWS、Google ...
查看>>
从零开始学习JAVA多线程(二)
查看>>
如何实现多云成本的管理
查看>>