JavaEE ------- count the number of website refreshes
Each website has its own traffic statistics, but the server may encounter unexpected situations (such as power failure ..)
So we need to save the data some time before the website goes offline. We need to use the largest container of the website, application. We use the observer design mode to achieve this.ServletContextListener Interface. Save the data before destruction.
Ps: it is a bit of knowledge. Please bypass Daniel.
Development steps:
Step 1: implement the ServletContextListener interface.
Step 2: Implement two methods.
ContextInitialized
ContextDestroyed
Step 3: add Node.
Specific implementation:
We need to implement the ServletContextListener interface, which uses two methods. We need to read the interface from the file during initialization and store it during destruction.
Read files:
Public class MyServletContext implements ServletContextListener {// This is the listener, which counts the website traffic/** reads from the file during startup (initialization) and * destroys in servletcontext, extract data from servletcontext and save it to 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); // if an exception occurs, no value is read, so it is set to 0 ;}}
Destroy and store data to files (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"); // obtain the corresponding count value from the container // pw. write (count); // store the pw In the tomcat directory. print (count); System. out. println ("destroyed" + count); pw. close ();} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke. printStackTrace ();}}
Add traffic
We need to write the traffic volume in the filter. we add the traffic volume from the context every time we filter the traffic.
Public void doFilter (ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {// multithreading is taken into account here, so data writing is inaccurate, so multithreading final ServletContext sct = request is required. getServletContext (); // enable a new Thread to count, without affecting the speed of the entire website. This is a good solution: new Thread () {public void run () {MyCount. addcount (sct); // lock the whole of the following ..}}. Start (); chain. doFilter (request, response );}The number of statistics will have the problem of multithreading, So we adopt multithreading, only let the number of statistics thread, does not affect the efficiency of the entire website
// Locking this will only slow the sub-branch and will not affect the speed of the entire website. class MyCount {public synchronized static void addcount (ServletContext sct) {Integer count = Integer. parseInt ("" + sct. getAttribute ("count"); count ++; // after adding, we need to add the container to the sct. setAttribute ("count", count); System. out. println (count );}}MyServletContext will be loaded from the file during initialization. If it does not exist, it will be automatically set as one. Every time the filter is used, 1 will be added, which enables statistics on the number of website refreshes.