Java EE-------Statistics site Refresh Volume

Source: Internet
Author: User

Each site has its own statistical access, but the server will have unexpected circumstances, such as power outages. )

So we need to save this data in the time before the site hangs up.

We need to use the largest container of the site, application, we adopt the observer design pattern to implement the Servletcontextlistener interface . Then save the data before destroying it.

PS: Belongs to the point 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: Join the <listener/> node in Web. Xml.


Detailed implementation:

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

Read file:

public class Myservletcontext implements Servletcontextlistener {//This is the listener. The number of visitors to the statistics site */* is read from the file when it is started (initialized). 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 to store 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");//Gets the corresponding count value from the container//pw.write (count);//The file is stored in the Tomcat folder under Pw.print (count); System.out.println ("destroyed" +count);p w.close ();} catch (FileNotFoundException e) {//TODO auto-generated catch Blocke.printstacktrace ();}}

Join the number of visitors

We need to write the amount of the interview in the filter. Every 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 you need to adopt multithreading final ServletContext sct=request.getservletcontext ();//enable new thread to count, Does not affect the speed of the entire site, so doing very well new Thread () {public void run () {mycount.addcount (SCT);//Lock the following overall:

}}.start (); Chain.dofilter (request, response);}

There is a problem with multithreading in the number of statistics. So we use multithreading, just let the statistics number of threads, do not affect the efficiency of the entire site

Lock this up, just CBD the 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")); after count++;//joins, we need to add the container inside Sct.setattribute ("Count", Count ); System.out.println (count);}}
Myservletcontext is loaded from the file at initialization time, does not exist to be self-set to one, each time filter. Will add 1, this will achieve the number of site refresh statistics.

Java EE-------Statistics site 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.