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

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

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

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. elsehttp://www.kmnk03.com/hxpfk/ylb/317.html
  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. <%http://www.kmnk03.com/hxpfk/ylb/319.html
  6. String name=request.getParameter("name");
  7. String password=request.getParameter("password");
  8. if(name!=null && password!=null)
  9. {
  10. Cookie cookie1=new Cookie("name", name);
  11. cookie1.setMaxAge(100000);
  12. response.addCookie(cookie1);
  13. Cookie cookie2=new Cookie("password", password);
  14. cookie2.setMaxAge(100000);
  15. response.addCookie(cookie2);
  16. out.println("完成书写Cookie!");
  17. }
  18. else
  19. {
  20. out.println("书写失败!");
  21. }
  22. %>kmnk03.com
  23. </body>www.kmnk03.com
  24. </html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这是一个关于使用HttpSessionBindingListener来统计在线人数的问题。 首先,HttpSessionBindingListener是一个接口,用于监听HttpSession属性的变化。当一个属性被添加、移除或替换时,实现HttpSessionBindingListener接口的类将被通知。 要统计在线人数,我们可以创建一个类,实现HttpSessionBindingListener接口,并在属性被添加和移除时更新在线人数。以下是一个简单的示例代码: ``` public class OnlineCountListener implements HttpSessionBindingListener { private static int onlineCount = 0; @Override public void valueBound(HttpSessionBindingEvent event) { onlineCount++; event.getSession().getServletContext().setAttribute("onlineCount", onlineCount); } @Override public void valueUnbound(HttpSessionBindingEvent event) { onlineCount--; event.getSession().getServletContext().setAttribute("onlineCount", onlineCount); } } ``` 在这个示例中,我们创建了一个静态变量onlineCount来保存在线人数。在valueBound方法中,每当一个新的属性被添加到会话中时,我们将在线人数加1,并将在线人数存储在ServletContext中。在valueUnbound方法中,每当一个属性从会话中移除时,我们将在线人数减1,并将在线人数存储在ServletContext中。最后,我们可以在JSP页面中使用以下代码显示在线人数: ``` <%= application.getAttribute("onlineCount") %> ``` 这样,我们就可以使用HttpSessionBindingListener来统计在线人数了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值