Number of online people using Httpsessionlistener statistics

Source: Internet
Author: User

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.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.