Home  >  Article  >  Java  >  Statistics of Java web website visits

Statistics of Java web website visits

高洛峰
高洛峰Original
2017-01-23 17:06:501525browse

When a customer accesses the website, read this file, read the count before the server is restarted, increase it by 1, and then write the new count to the file.

The reference code is as follows:

100db36a723c770d327fc0aef2ce13b1

<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> 
    您是第 
    <%=number %> 
    个访问网站的客户。 
  </p> 
</body> 
</html>

The above is the entire content of this article. I hope it will be helpful to everyone’s learning. I also hope that everyone will learn PHP Chinese.网

For more articles related to the statistics of Java web website visits, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn