A simple website counter

Source: Internet
Author: User

In the Web site, often need to count how many people visited this site, statistics should be based on whether it is a new answer to determine whether it is a new access to the site users. The Application object can save the number of visitors.

About application objects: The Application object is used to share data between multiple programs or multiple users, and the user uses the same Application object. This is different from the session. Once the server is started, The Application object is automatically created and saved until the server shuts down, and the Application object disappears automatically. The following is a Web site counter using the Application object, the code is as follows:

1 <%@ Page Language="Java"Import="java.util.*"pageencoding="UTF-8"%>2 <!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en">3 <HTML>4   <Body>5       <%!6 synchronized void count () {//Use the Sychronized keyword to serialize the function,7                                         //Make sure that there is no conflict when two users are accessing the page at the same time and modifying the count value.8 ServletContext Application=( (HttpServlet) (this)). Getservletcontext ();9             IntegerCount= (Integer) Application.getattribute ("Count");Ten              One             if(Count==NULL){ A Count= New Integer(1); - Application.setattribute ("Count", count); -             }Else{ the Count= New Integer(Count.intvalue ()+1); - Application.setattribute ("Count", count); -             } -         }       +       %> -        +       <% A           if(Session.isnew ()) {//determine if it is a new reply. at count (); - Out.println ("you are the first"+Application.getattribute ("Count")+"visit this website"); -           } -       %> -   </Body> - </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.