First write a class:
Import javax.servlet.*;
Import javax.servlet.http.*;
public class Sessioncounter implements Httpsessionlistener {
private static int activesessions = 0;
public void sessioncreated (Httpsessionevent se) {
activesessions++;
}
public void sessiondestroyed (Httpsessionevent se) {
if (activesessions > 0)
activesessions--;
}
public static int getactivesessions () {
return activesessions;
}
}
Then configure Web.xml
<?xml version= "1.0" encoding= "UTF-8"?>
<web-app version= "2.4"
Xmlns= "HTTP://JAVA.SUN.COM/XML/NS/J2EE"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
Xsi:schemalocation= "Http://java.sun.com/xml/ns/j2ee
Http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd ">
****************************************
<!--Listeners-->
<listener>
<listener-class>
Sessioncount.sessioncounter (note here)
</listener-class>
</listener>
*****************************************
</web-app>
To build a JSP test:
test.jsp Tutorial
<%@ page language= "Java" contenttype= "TEXT/HTML;CHARSET=GBK"%>
<%@ page import= "java.sql.*"%>
<%@ page import= "Sessioncount.sessioncounter"%>
<meta http-equiv= "Content-type" content= "text/html; CHARSET=GBK ">
<title> Untitled Document </title>
<body bgcolor= "#FFFFFF" >
Number online: <%=sessioncounter.getactivesessions ()%>
</body>