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