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!