JSP Online Headcount Statistics program

Source: Internet
Author: User
Tags xmlns

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>

Related Article

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.