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>
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!