Java EE-------Statistics website Refresh Volume

Source: Internet
Author: User

Each site has its own statistical traffic, but there are unexpected things that happen to the server, such as a power outage. )

So we need to save this data for a while before the website hangs up. We need to use the largest container of the site, application, we use the observer design pattern to implement the Servletcontextlistener interface . Then save the data before destroying it.

PS: A bit of knowledge, Daniel, please take a detour.

Development steps:

The first step: Implement the Servletcontextlistener interface.
The second step: Implement two methods.
Contextinitialized
Contextdestroyed

Step three: Add the <listener/> node in Web. Xml.


Specific implementation:

We need to implement the Servletcontextlistener interface , which uses two methods, which we need to read from the file during initialization, and then save it at the time of the destruction.

Read file:

public class Myservletcontext implements Servletcontextlistener {//This is the listener that counts the number of visits to the Web site */* * when started (initialized) read from the file, In the presence of ServletContext * destruction, the data is removed from the ServletContext and stored in the file */string filename = ""; @Overridepublic void Contextinitialized ( Servletcontextevent SCE) {ServletContext context=sce.getservletcontext (); String Filename=context.getrealpath ("/count.txt"); try {bufferedreader br =new BufferedReader (new FileReader (filename )); String num =br.readline (); Integer numer =integer.valueof (num); Context.setattribute ("Count", numer);// Store the read value in the ServletContext container Br.close ();} catch (Exception e) {e.printstacktrace (); Context.setattribute ("Count", new Integer (0));//Out Exception description No value read, so set to 0;}}

Destroy stores data to a file (only files are permanently stored)

@Overridepublic void contextdestroyed (Servletcontextevent sce) {ServletContext context=sce.getservletcontext (); String Filename=context.getrealpath ("/count.txt"), try {printwriter pw =new printwriter (filename), Integer count= ( Integer) Context.getattribute ("Count");//Get the corresponding count value//pw.write (count) from the container, or//save the file in the Tomcat directory under Pw.print (count); System.out.println ("destroyed" +count);p w.close ();} catch (FileNotFoundException e) {//TODO auto-generated catch Blocke.printstacktrace ();}}

Add the amount of access

Traffic we need to write in the filter, each time we filter, we add one from the context

public void DoFilter (ServletRequest request, Servletresponse Response,filterchain chain) throws IOException, Servletexception {//Here considering multi-threading, so write data is not accurate, so need to adopt multi-threaded final ServletContext sct=request.getservletcontext ();//enable new thread to count, Does not affect the speed of the entire site, so doing well new Thread () {public void run () {mycount.addcount (SCT);//Lock the whole of the following: }}.start (); Chain.dofilter (request, response);}
The number of statistics there will be multi-threaded problem, so we use multithreading, only to let the statistics of the number of threads, does not affect the efficiency of the entire site

Lock this up, only the CBD branch will be slow and will not affect the speed of the entire site class mycount{public synchronized static void  Addcount (ServletContext SCT) {Integer Count=integer.parseint ("" +sct.getattribute ("Count")) count++;//added, we need to add the container inside to Sct.setattribute ("Count", Count ); System.out.println (count);}}
Myservletcontext at the time of initialization will be loaded from the file, does not exist will be automatically set to one, each time the filter, will add 1, which enables the site refresh statistics.

Java EE-------Statistics website Refresh Volume

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.