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