How do I achieve statistics on the number of Web page visits?

Source: Internet
Author: User

I use the SPRINGMVC, the general talk about their own ideas bar:

To the page traffic statistics is the PV, the main interception, write an interceptor class to implement the Handlerinterceptor interface, intercept all requests, but it should be noted that the time to configure the Interceptor

Need to exclude the request that does not intercept, because for static resources Css,js, as well as the picture, but also has the backstage management system we do not need to intercept, we only intercepts the user to visit the page, namely you show

To the user of those pages configured to intercept probably this configuration


	<mvc:interceptor>
			<!--intercept path-->
			<mvc:mapping path= "/**"/>
			<mvc:exclude-mapping Path= "Admin URL is not disclosed"/><!--do not intercept background management-->
			<mvc:exclude-mapping path= "/css/**"/> <mvc
			: Exclude-mapping path= "/js/**"/>
			<mvc:exclude-mapping path= "/images/**"/> <mvc
			: Exclude-mapping path= "/fonts/**"/> <bean class= "com.colin.interceptor.InterceptAllRequest"/>
		</mvc:interceptor>


Then intercept what the request needs to do. Remove the amount of traffic from the database. Then add 1. This is obviously unreasonable because there are too many requests, and frequent interactions with the database should be avoided,

I want to write a listener, Servletcontextlistener, listen to the initial ServletContext (only when the server is started to be created), server startup to the database to fetch traffic,

To the ServletContext (global domain, which can be taken out in any other request), and then each time a request adds 1 to the amount of traffic placed in the ServletContext (this is what is done in the Interceptor), When initializing the ServletContext also need to do one thing, opens the thread time to store in the ServletContext the amount of traffic in the database, how to execute regularly. I want to write a loop, run a loop and let the thread hibernate

2 hours (custom: Shorter time is less likely to cause loss of access data), then accidentally saw a JDK document a class, timer, a timer, detailed reference documents, it can be accessed to achieve the task of timing, the principle of the estimate is similar, this is probably the implementation process, the specific code will not be posted. A more serious problem is not taken into account: Since ServletContext is in memory

Only one object, concurrent access can cause thread-safety issues, preliminary thinking in ServletContext when a thread-safe collection, data access is placed in the collection

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.