Using httpsessionlistener to achieve online website count statistics function from http://www.web521.com/web/558068/T645231.shtml)

Source: Internet
Author: User

Statistics on the number of online users are often required on websites. In the past, the usual practice was to combine the login and exit functions, that is, when the user enters the user name and password for login, the counter is added with 1, and then when the user clicks the exit button to exit the system, the counter is reduced by 1. This processing method has some disadvantages. For example, after a user logs on normally, the user may forget to click the exit button and directly close the browser. As a result, the operations on the counter minus 1 are not performed in time; the website often has some content that can be accessed without logon. In this case, the above method cannot be used for online statistics.
We can use the event listener defined in the servlet specification to solve this problem and achieve more accurate online count statistics. For each user being accessed, the J2EE Application Server creates an httpsession object for the user. When a browser accesses a website for the first time, the J2EE Application Server creates an httpsession object and triggers an httpsession creation event. If an httpsessionlistener event listener is registered, the sessioncreated method of the httpsessionlistener event listener is called. On the contrary, when the browser access ends, the J2EE application server will destroy the corresponding httpsession object, trigger the httpsession destruction event, and call the sessiondestroyed method of the registered httpsessionlistener event listener.
It can be seen that the sessioncreated method and sessiondestroyed method are executed at the beginning and end of a user's access. In this way, we only need to add 1 to the counter in the sessioncreated method of the httpsessionlistener implementation class, and reduce the counter by 1 in the sessiondestroyed method to easily implement the online count statistics function of the website.
The following is an example of using httpsessionlistener to calculate the number of online users. This example has passed the test on the J2EE Application Server inforweb of the software.
First, write a simple counter with the following code:

  1. PackageGongfei. CMC. Articles. onlinecounter;
  2. Public ClassOnlinecounter {
  3. Private Static LongOnline = 0;
  4. Public Static LongGetonline (){
  5. ReturnOnline;
  6. }
  7. Public Static VoidRaise (){
  8. Online ++;
  9. }
  10. Public Static VoidReduce (){
  11. Online --;
  12. }
  13. }

Then, compile the httpsessionlistener implementation class, call the onlinecounter raise method in the sessioncreated method of this implementation class, and call the onlinecounter reduce method in the sessiondestroyed method. The Code is as follows:

  1. PackageGongfei. CMC. Articles. onlinecounter;
  2. ImportJavax. servlet. http. httpsessionevent;
  3. ImportJavax. servlet. http. httpsessionlistener;
  4. Public ClassOnlinecounterlistenerImplementsHttpsessionlistener {
  5. Public VoidSessioncreated (httpsessionevent HSE ){
  6. Onlinecounter. Raise ();
  7. }
  8. Public VoidSessiondestroyed (httpsessionevent HSE ){
  9. Onlinecounter. Reduce ();
  10. }
  11. }

Then, register the httpsessionlistener implementation class to the website application, that is, add the following content to the Web. xml of the website application:

  1. <Web-app>
  2. ......
  3. <Listener>
  4. <Listener-Class>
  5. Gongfei. CMC. Articles. example. onlinecounterlistener
  6. </Listener-Class>
  7. </Listener>
  8. ......
  9. </Web-app>

OK. The online count statistics function has been implemented. You only need to add the following script to the JSP page to display the number of online users:

  1. <% @ Page Language = "Java" pageencoding = "gb2312" %>
  2. <% @ PageImport= "Gongfei. CMC. Articles. onlinecounter. onlinecounter" %>
  3. <HTML>
  4. <Head> <title> On Line counert </title>
  5. <Body bgcolor = "# ffffff">
  6. On line: <% = onlinecounter. getonline () %>
  7. </Body>
  8. </Html>

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.