Using Httpsessionlistener to realize the statistic function of the online number of website

Source: Internet
Author: User
Tags event listener
Online statistics are often required on the website. In the past, the common practice is to combine login and exit functions, that is, when the user entered the user name password to log in when the Counter plus 1, and then when the user clicks the Exit button to exit the system when the counter minus 1. There are some drawbacks to this approach, for example: After the user log in normally, may forget to click the Exit button, but closes the browser directly, causes the counter to reduce 1 the operation not to carry out in time; there are often some content on the Web site that you do not need to log on to, and in this case you cannot use the above method for online
We can use event listeners (Listener) defined in the servlet specification to solve this problem and achieve more accurate online demographics. For each user that is being accessed, the Java EE Application Server creates a corresponding HttpSession object for it. When a browser first accesses the Web site, the Java application Server creates a new HttpSession object and triggers httpsession to create events, if the Httpsessionlistener event listener is registered, The sessioncreated method of the Httpsessionlistener event listener is invoked. Conversely, when this browser access end times out, the Java EE application Server destroys the corresponding HttpSession object, triggering the HttpSession destroy event, Calls the Sessiondestroyed method of the registered Httpsessionlistener event listener at the same time.
Visible, corresponding to the start and end of a user access, corresponding to the Sessioncreated method and sessiondestroyed method execution. In this way, we only need in the Httpsessionlistener implementation class sessioncreated method to let the counter add 1, in the Sessiondestroyed method to reduce the counter by 1, it is easy to achieve the number of online Web site statistics function.
The following is an example of using Httpsessionlistener to achieve online demographics, an example that has been tested in the Java Application Server Inforweb software.
First, write a simple counter with the following code:
PackageGongfei.cmc.articles.onlinecounter; Public classOnlinecounter { Private Static LongOnline = 0; Public Static LongGetonline () { returnOnline } Public Static voidRaise () {online++; } Public Static voidReduce () {online--; } }
Then, write the Httpsessionlistener implementation class, which calls the Onlinecounter raise method in the Sessioncreated method of the implementation class, Call the Onlinecounter reduce method in the Sessiondestroyed method, as follows:
PackageGongfei.cmc.articles.onlinecounter; ImportJavax.servlet.http.HttpSessionEvent; ImportJavax.servlet.http.HttpSessionListener; Public classOnlinecounterlistener ImplementsHttpsessionlistener { Public voidsessioncreated (httpsessionevent HSE) {onlinecounter.raise (); } Public voidSessiondestroyed (httpsessionevent HSE) {onlinecounter.reduce (); } }
Then, register the Httpsessionlistener implementation class into the Web application, which is to add the following content to the Web.xml of the Web application:
<web-app> ... <listener> <listener- class> Gongfei.cmc.articles.example.OnlineCounterListener </listener- class> </listener> ... </web-app>

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.