JSP Realization of Web Visit Statistics

  • 2021-07-10 20:32:26
  • OfStack

Recently, I learned jsp web page development in Jave EE. Need to realize the statistics of web page visits, I didn't know how to achieve it at first, Later, I asked the teacher once, The teacher answered me like this: To realize the statistics of web page access, you can use application object to realize it, but not seesion object, because session belongs to the same session, and the data will be lost when the browser is turned off, while application is under the same browser, so long as it is the same browser, the data will be saved in applicaiton object, so as to ensure the invariance of the data. In fact, I know all this, but I just don't know how to implement it in code in jsp. Later, I can only surf the Internet to see if there is a specific solution. After searching for a long time, I don't have the answer I want. What I want to achieve is simple statistics, but I don't realize more complex functions. Later, I found the answer here in CSDN. Here, I briefly summarize several methods to realize web page access statistics under 1:

1. Using application object to make statistics, the result is that every time you enter the web page, you will make statistics once. But the effect is not very good, because 1 general statistics page visits, refresh is not counted in the statistics, here is this shortcoming.

The concrete realization is:


<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%> 
<html> 
 <head> 
 <title>java  Counter program </title> 
 </head> 
 <body> 
 <% 
 if (application.getAttribute("count") == null) { 
  application.setAttribute("count", new Integer(0)); 
 } 
 Integer count = (Integer) application.getAttribute("count"); 
 application 
   .setAttribute("count", new Integer(count.intValue() + 1)); 
 count = (Integer) application.getAttribute("count"); 
 %> 
  <center> This is the first <%=count.intValue()%> Visitors </center> 
 </body> 
 </html> 

2. In order to solve the above problems, there is another method, which uses application object and session object to make statistics at the same time. The principle of this method is that from opening the browser to closing the browser, it is regarded as one visit, and operations such as refreshing and returning are not regarded as one visit. However, there is still a defect. When the jsp server is restarted, the data is cleared.

The following is the concrete implementation:


<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%> 
<html> 
 <head> 
 <title>java  Counter program </title> 
 </head> 
 <body> 
 <% 
 int n = 0; String counter = (String)application.getAttribute("counter"); 
 if(counter != null){ 
  n = Integer.parseInt(counter); 
 } 
 if(session.isNew()) 
  ++n; 
 %> 
  <center> This is the first <%out.print(n);%> Visitors </center> 
  <% 
  counter = String.valueOf(n); 
  application.setAttribute("counter", counter); 
   %> 
 </body> 
 </html> 

3. The third method is to store the statistics in a local file, such as an txt file.

This is to solve the problem that data will not be lost after restarting the server.
Create 1 class: JSPCount


import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileReader; 
import java.io.FileWriter; 
import java.io.IOException; 
import java.io.PrintWriter; 
 
 
public class JSPCount { 
 // Method of writing files  
 public static void write2File(String filename, long count){ 
  try{ 
   PrintWriter out = new PrintWriter(new FileWriter(filename)); 
   out.println(count); 
   out.close(); 
  } catch (IOException e) { 
   // TODO: handle exception 
   e.printStackTrace(); 
  } 
 } 
  
 // The method of reading files  
 public static long readFromFile(String filename){ 
  File file = new File(filename); 
  long count = 0; 
  if(!file.exists()){ 
   try { 
    file.createNewFile(); 
   } catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
   } 
   write2File(filename, 0); 
  } 
  try{ 
   BufferedReader in = new BufferedReader(new FileReader(file)); 
   try{ 
    count = Long.parseLong(in.readLine()); 
   } 
   catch (NumberFormatException e) { 
    // TODO: handle exception 
    e.printStackTrace(); 
   } catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
   } 
  } catch (FileNotFoundException e) { 
   // TODO: handle exception 
   e.printStackTrace(); 
  } 
  return count; 
 } 
} 

Build jsp file under WebRoot directory: count. jsp


<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%> 
<%@ page import="org.wwj.count.JSPCount" %> 
<html> 
 <head> 
 <title>java  Counter program </title> 
 </head> 
 <body> 
 <% 
 JSPCount CountFileHandler = new JSPCount(); 
 // Read a file  
 long count = CountFileHandler.readFromFile(request.getRealPath("/") + "count.txt"); 
 count = count + 1; // Modify record  +1 
 out.print(count); // Display data  
 // Update file contents.  
 CountFileHandler.write2File(request.getRealPath("/") + "count.txt", count); 
  
 %> 
 </body> 
 </html> 

After the program runs, a text file of count. txt will be generated in the corresponding web project under the webapps directory under tomcat

4. The fourth method only saves the statistical data of visits, but there is no guarantee that it will not increase itself when refreshing the page, which is still not good. Of course, there will always be a solution. A general solution is to combine the advantages of various schemes. The following is the session Object + application Object + txt Text to achieve website access statistics.


import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileReader; 
import java.io.FileWriter; 
import java.io.IOException; 
import java.io.PrintWriter; 
 
import javax.servlet.http.HttpServlet; 
 
public class Counter extends HttpServlet{ 
 // Method of writing files  
 public static void write2File(String filename, long count){ 
  try{ 
   PrintWriter out = new PrintWriter(new FileWriter(filename)); 
   out.println(count); 
   out.close(); 
  } catch (IOException e) { 
   // TODO: handle exception 
   e.printStackTrace(); 
  } 
 } 
  
 // The method of reading files  
 public static long readFromFile(String filename){ 
  File file = new File(filename); 
  long count = 0; 
  if(!file.exists()){ 
   try { 
    file.createNewFile(); 
   } catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
   } 
   write2File(filename, 0); 
  } 
  try{ 
   BufferedReader in = new BufferedReader(new FileReader(file)); 
   try{ 
    count = Long.parseLong(in.readLine()); 
   } 
   catch (NumberFormatException e) { 
    // TODO: handle exception 
    e.printStackTrace(); 
   } catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
   } 
  } catch (FileNotFoundException e) { 
   // TODO: handle exception 
   e.printStackTrace(); 
  } 
  return count; 
 } 
} 

jsp file code:


<%@page import="org.servlet.count.Counter"%> 
<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%> 
<html> 
 <head> 
  <title>java  Counter program </title> 
 </head> 
 <body> 
 <% 
 Counter CountFileHandler = new Counter(); 
 long count = 0; 
 if(application.getAttribute("count") == null){ 
  count = CountFileHandler.readFromFile(request.getRealPath("/") + "count.txt"); 
  application.setAttribute("count", new Long(count)); 
 }  
 count = (Long)application.getAttribute("count"); 
 if(session.isNew()){ 
  count++; 
  application.setAttribute("count", count); 
  // Update the file directory  
  CountFileHandler.write2File(request.getRealPath("/") + "count.txt",count); 
  } 
 %> 
  Number of visitors: <%=count %> 
  </body> 
</html> 

The above four methods, It is a method obtained every time it is improved. If you want to implement site access statistics, Of course, the last one is the best. Knowledge is not a step to heaven. It needs to be continuously improved on the problem to get the final solution. Of course, the last one is the best. In terms of implementation strategy, it is also possible to use the database, but I think it is necessary to read and write the database every time I visit the website, so the efficiency is reduced.


Related articles: