Learning Notes 5_day09_ website visits statistics little Exercise

Source: Internet
Author: User

Exercise: Traffic statistics

All of the resources in a project are accessed to accumulate the amount of traffic!

Create a variable of type int to save the traffic and then save it to the ServletContext domain so that all the servlets can be accessed!

   1, initially, the ServletContext does not save the traffic related attributes;

2, when the site was first accessed, create a variable, set its value to 1; save to ServletContext;

3, when the subsequent access, you can get this variable from the ServletContext, and then add 1 on its base.

4, get the ServletContext object, see if there is a property named Count, if present, the description is not the first access, if not present, the description is the first time access;

    • First access: Call ServletContext's setattribute () to pass an attribute named Count, which has a value of 1;
    • 2~n Access: Call ServletContext's GetAttribute () method to get the original traffic, add 1 to the traffic, and then call ServletContext's SetAttribute () method to complete the setup.  

I believe you have seen a lot of traffic statistics site, that is, "this page has been visited xxx times." Because no matter which user accesses the specified page, the amount of traffic is accumulated,

So this traffic statistics should be shared throughout the project! Obviously, this requires the use of ServletContext to save the traffic.

ServletContext application = this. Getservletcontext (); [Cui 1]

Integer count = (integer) application.getattribute ("Count")[Cui 2] ;

if (count = = null) {

Count = 1; [Cui 3]

} Else {

count++[Cui 4] ;

}

Response.setcontenttype ("Text/html;charset=utf-8");

Response.getwriter (). Print ("[Cui 5] ;

Application.setattribute ("Count", count); [Cui 6]

[Cui 1] get ServletContext Object

[Cui 2] gets the Count property in the ServletContext object

[Cui 3] if the Count property does not exist in ServletContext, setting the value of count to 1 means that the first time it is accessed.

[Cui 4] if the Count attribute is present in ServletContext, indicating that it was previously accessed, Count is added 1 on the original basis.

[Cui 5] the number of times this page was accessed to the client.

[Cui 6] saves the value of Count to the ServletContext object.

Learning Notes 5_day09_ website visits statistics little Exercise

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.