HttpSessionListener

这个接口主要是用来监控session的创建和消毁的,它有两个方法
  1. public void sessionCreated(HttpSessionEvent se)
  2. public void sessionDestroyed(HttpSessionEvent se)
我们要实现监控session的功能,需要实现这个接口,然后在web .xml中配置一个Listener.
  1. /**
  2. * 编写以下SessionCounter.java
  3. * 并编译为SessiionCounter.class
  4. * 然后放到你的网站的classpath的
  5. * SessionCount(自己建立此目录)下面
  6. */
  7. 1.
  8. 2. package SessionCount;
  9. 3. import javax.servlet.*;
  10. 4. import javax.servlet.http.*;
  11. 5.
  12. 6. public class SessionCounter implements HttpSessionListener {
  13. 7.
  14. 8. private static int activeSessions = 0;
  15. 9.
  16. 10. public void sessionCreated(HttpSessionEvent se) {
  17. 11. activeSessions++;
  18. 12. }
  19. 13.
  20. 14. public void sessionDestroyed(HttpSessionEvent se) {
  21. 15. if(activeSessions > 0)
  22. 16. activeSessions--;
  23. 17. }
  24. 18.
  25. 19. public static int getActiveSessions() {
  26. 20. return activeSessions;
  27. 21. }
  28. 22. }

  1. <%@ page import="SessionCount.SessionCounter" %>
  2. 在线:<%= SessionCounter.getActiveSessions() %>

  1. xml version="1.0" encoding="ISO-8859-1"?>
  2. PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
  3. "http://java.sun.com/j2ee/dtds/web-app_2.3.dtd">
  4. <web-app>
  5. <listener>
  6. <listener-class>
  7. SessionCount.SessionCounter
  8. listener-class>
  9. listener>
  10. web-app>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值