Statistics of visits to Java web website

  • 2020-05-30 20:07:02
  • OfStack

When the client visits the site, read the file, read in the pre-restart count, add 1 to it, and then write the new count to the file.

The reference code is as follows:

< html >


<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
<title>Insert title here</title> 
</head> 
<body> 
  <%! 
    int number = 0; 
    File file = new File("count.txt"); 
    synchronized void countPeople() 
    { 
      if(!file.exists()) 
      { 
        number++; 
        try{ 
          file.createNewFile(); 
          FileOutputStream out = new FileOutputStream("count.txt"); 
          DataOutputStream dataOut = new DataOutputStream(out); 
          dataOut.writeInt(number); 
          dataOut.close(); 
        }catch(IOException ex){} 
      } 
      else  
        try{ 
          FileInputStream in = new FileInputStream("count.txt"); 
          DataInputStream dataIn = new DataInputStream(in); 
          number = dataIn.readInt(); 
          number++; 
          in.close(); 
          dataIn.close(); 
          FileOutputStream out = new FileOutputStream("count.txt"); 
          DataOutputStream dataOut = new DataOutputStream(out); 
          dataOut.writeInt(number); 
          out.close(); 
          dataOut.close(); 
        }catch(IOException ex){} 
    } 
  %> 
  <% 
    countPeople(); 
  %> 
  <p> 
     You are the first  
    <%=number %> 
     Five customers who visit the site.  
  </p> 
</body> 
</html> 

Related articles: