Jsp使用HttpSessionBindingListener实现在线人数记录

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 HttpSessionBindingListener
14 {  
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;i
35         { 
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;i
53             { 
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()+"	  登入到系统	"+(new Date())); 
83             System.out.println("     在线用户数为:"+getCount()); 
84         }else
85             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+"	  退出系统	"+(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 <jsp:useBean id="onlineuser" class="com.trs.onLineUser" scope="application"/> 
 4 <html>
 5 <head>
 6 <title>show</title>
 7 </head>  
 8 <body>
 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 else
16   out.println(name+"没有登陆到系统!"); 
17 }
18 %>
19 </body>
20 </html>

online.jsp

 1 <%@ page contentType="text/html;charset=GB2312" pageEncoding="GBK"%>
 2 <%@page import="com.trs.onLineUser,java.util.*" %>
 3 <html>
 4 </body>
 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 else
21 {
22 out.println("书写失败!");
23 }
24 %>
25 </body>
26 </html>

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

原文地址:https://www.cnblogs.com/lr393993507/p/5553295.html