Servlet-based JSP page display of online users

Source: Internet
Author: User

Abstract: This article focuses on statistics on the number of people who are interested in JSP programming and servlet.

1. Compile and compile sessioncounter. Java with the following code:

/**
* Write the following sessioncounter. Java
* And compiled as sessiioncounter. Class
* Put It In The classpath of your website.
* Sessioncount (create this directory by yourself)
*/

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;
}
// It Learning Network www.itstudy.cn
}

2. Create an online. jsp file to display the number of online users.

<% @ Page import = "sessioncount. sessioncounter" %>
Online: <% = sessioncounter. getactivesessions () %>

Then you need to create web. XML in the Web-INF of your website. The file content is 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>

3. Restart your Web application server and access online. jsp to check whether the display is correct.

This program is successfully debugged under Windows2003 + tomcat5.5.17 + jdk1.5.0 _ 08!
This program is successfully debugged under FreeBSD resin2.0.4!
 

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.