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

Statistics of Java web website visits

高洛峰
Release: 2017-01-23 17:06:50
Original
1585 people have browsed it

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:

<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>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template