JSP Display online number code
Write the following Sessioncounter.java and compile it for sessiioncounter.class and then put it on your site classpath sessioncount (build this directory yourself) below
package SessionCount;
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 create a online.jsp file to display the number of people online
<%@ page import="SessionCount.SessionCounter" %>
Online:
<%= SessionCounter.getActiveSessions() %>
Then you need to create a web.xml file in the Web-inf of your website as follows:
<!-- Web.xml -->
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/j2ee/dtds/web-app_2.3.dtd">
<web-app>
<!-- Listeners -->
<listener>
<listener-class>
SessionCount.SessionCounter
</listener-class>
</listener>
</web-app>
Then restart your application server and access the online.jsp check to see if the display is correct.