Use the HttpSessionListener listener interface to listen to the current number of online users

Source: Internet
Author: User

Programming: using the HttpSessionListener listener interface to listen to the current number of online users
Requirements:
(1) Compile a class to implement the HttpSessionListener interface. In the public void sessionCreated (HttpSessionEvent se) method of this interface, declare a variable to record the number of online users, and save it as an attribute in the ServletContext object.
(2) configure the listener in web. xml.
(3) Compile a JSP page to obtain the property value in the application Object and display it on the page.
[Html]
<% @ Page language = "java" import = "java. util. *" pageEncoding = "ISO-8859-1" %>
 
<Html>
<Head>
</Head>
 
<Body>
<% Application. getAttribute ("a"); %>
</Body>
</Html>


[Html]
<? 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>
<Listener>
<Listener-class> com. mars. SessionCount </listener-class>
</Listener>
</Web-app>

[Java]
Package com. mars;
 
Import java. util .*;
Import javax. servlet .*;
Import javax. servlet. http .*;
 
Public class SessionCount implements HttpSessionListener {
Int a = 0;
Public void sessionCreated (HttpSessionEvent hse ){
A ++;
ServletContext application = hse. getSession (). getServletContext ();
Application. setAttribute ("a", new Integer ());
 
}
 
Public void sessionDestroyed (HttpSessionEvent red ){
 
}
 
 
}


From learning IT from Mars

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.