当前位置:编程学习 > JS >>

jsp编写当前在线人数统计代码实例

首先写个类:
   
  1. import javax.servlet.*;
  2.     import javax.servlet.http.*;
  3.     public class SessionCounter implements HttpSessionListener {
  4.     private static int activeSessions = 0;
  5.     public void sessionCreated(HttpSessionEvent se) {
  6.     activeSessions ;
  7.     }
  8.    public void sessionDestroyed(HttpSessionEvent se) {
  9.     if(activeSessions > 0)
  10.     activeSessions--;
  11.     }
  12.     public static int getActiveSessions() {
  13.     return activeSessions;
  14.     }
  15.     }
  16.     然后配置web.xml
  17.     <?xml version="1.0" encoding="UTF-8"?>
  18.     <web-app version="2.4"
  19.     xmlns="http://java.sun.com/xml/ns/j2ee"
  20.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  21.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
  22.     http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  23.     ****************************************
  24.     <!-- Listeners -->
  25.     <listener>
  26.     <listener-class>
  27.     SessionCount.SessionCounter (注意此处)
  28.     </listener-class>
  29.     </listener>
  30.     *****************************************
  31.     </web-app>
  32. 建个JSP测试:
  33.     test.jsp
  34.     <%@ page language="Java" contentType="text/html;charset=GBK"%>
  35.     <%@ page import="java.sql.*"%>
  36.     <%@ page import="SessionCount.SessionCounter" %>
  37.     <html>
  38.     <head>
  39.     <meta http-equiv="Content-Type" content="text/html; charset=GBK">
  40.     <title>无标题文档</title>
  41.     <body bgcolor="#FFFFFF">
  42.     在线人数:<%=SessionCounter.getActiveSessions()%>
  43.     </body>
  44.     </html>

    看看可以实现不?我也是找到的,我用了,可以统计出来.
补充:Web开发 , Jsp ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,