Use httpsessionListener to implement website online number statistics
The statistics of online number are often required in the website. The general practice of past is to combine the login and exit function, that is, the counter plus 1 when the user enters the username password to log in, and then the counter is reduced when the user clicks the exit button exits the system. This processing method has some shortcomings, for example, after the user logs in, it may forget to click the exit button, and directly turn off the browser, causing the counter minus 1 operation without timely execution; there are often some contents on the website. It can be accessed, and in this case, the above method is not available for online number statistics.
We can use the event listener defined in the servlet specification to solve this problem and achieve more accurate online number statistics. For each user who is being accessed, the J2EE application server creates a corresponding HTTPSession object. When a browser accesses the website for the first time, the J2EE application server will create a httpsession object and trigger HttpSession to create events. If you register an HttpSessionListener event listener, the HTTPSessionListener event listener will call the sessioncreated method. Conversely, when this browser accesses the end timeout, the J2EE application server destroys the corresponding httpsession object, triggers the HTTPSession destroyed event while calling the SESSIONDESTROYED method of the registered HttpSessionListener event listener.
It can be seen that corresponding to the beginning and end of a user access, the corresponding sessioncreated method and the sessionDestroyed method are executed. In this way, we only need to make the counter plus 1 in the sessionCreated method of the HTTPSessionListener implementation class. In the sessionDestroyed method, let the counter minus the counter, and easily implement the statistics of the website online.
Below is an example of using HTTPSessionListener to implement online number statistics, this example has been passed in the J2EE application server InforWeb of Masterware.
First, write a simple counter, the code is as follows:
package gongfei.cmc.articles.onlinecounter; public class OnlineCounter {private static long online = 0; public static long getOnline () {return online;} public static void raise () {online ;} public static void reduce () {online- -;}}
Then, write the HTTPSessionListener implementation class, call the RAISE method for the onlineCounter in this implementation class, call the ONLINECUNTER's Reduce method in the sessionDestroyed method, the code is as follows:
package gongfei.cmc.articles.onlinecounter; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; public class OnlineCounterListener implements HttpSessionListener {public void sessionCreated (HttpSessionEvent hse) {OnlineCounter.raise ();} public void SessionDestroyed (httpsessionever) {onlineCounter.Reduce ();}} then then register this httpsessionListener implementation class to the website application, that is, add the following Web.xml to the website application:
OK, online number statistics have been implemented, as long as the following scripts can be displayed in the JSP page, the number of online people can be displayed:
<% @ Page language = "java" PageEncoding = "GB2312"%> <% @ page import = "gongfei.cmc.Articles.onlineCounter.onlineCounter"%>