In some blogs or forums, there are often some number of visits, which is the usual website counter. The following 3 issues need to be noted for website counter development:
1. The number of visitors to the website is very large, so it must be represented by big integers;
2, each user in the first visit needs to count, repeat refresh page should not repeat count;
3, the site access to the value of the changes in the number of multi-threaded operations, need to synchronize operations.
The JSP code for the simulated Web site counters that you write is as follows:
<%@ page contenttype= "text/html" pageencoding= "GBK"%>
<%@ page import= "java.io.*"%> <%--you must import the Java.io package because you want to use IO operations--%>
<%@ page import= "java.util.*"%> <%--scanner defined java.util in--%>
<%@ page import= "java.math.*"%> <%--biginteger defined in Java.math--%>
<title> website Counters </title>
<body>
<%!
BigInteger count = null;
%>
<%!//the following methods in order to save trouble, directly in the method to deal with the exception, and the actual to be handed over to the call office processing
Public BigInteger Load (file file) {//Read count file
BigInteger count = null; Read the accepted data
try {
if (file.exists ()) {
Scanner scan = null;
Scan = new Scanner (new FileInputStream (file)); Read from File
if (Scan.hasnext ()) {
Count = new BigInteger (Scan.next ()); Put the content in the BigInteger
}
Scan.close (); Close the input stream
} else {
Count = new BigInteger ("0"); First time visit
Save (File,count);
}
} catch (Exception e) {
E.printstacktrace ();
}
return count; Returns the data after reading
}
public void Save (File File,biginteger count) {
try {
PrintStream PS = null; Defining an output Stream object
PS = new PrintStream (new FileOutputStream (file)); Print Stream Object
Ps.println (count);
Ps.close ();
} catch (Exception e) {
E.printstacktrace ();
}
}
%>
<%
String fileName = This.getservletcontext (). Getrealpath ("/") + "count.txt"; File path
File file= new file (FileName);
if (Session.isnew ()) {
Synchronized (this) {
Count = load (file);
Count = Count.add (New BigInteger ("1")); Self-increment operation
Save (File,count);
}
}
%>
</body>
Copy Code
The result of this code is:
When the first visit to this page shows "You are the 1th guest", the Refresh page appears unchanged, as shown in;
650) this.width=650; "id=" aimg_p8978 "class=" Zoom "border=" 0 "src=" http://img.blog.csdn.net/20150413175337654? watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvutm4oti4mtu0mq==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center "width=" "height=" 212 "/>
Close the browser when you open this page with a new browser, "You are the 2nd visitor", as shown in:
650) this.width=650; "id=" Aimg_i58z8 "class=" Zoom "border=" 0 "src=" http://img.blog.csdn.net/20150413175014199? watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvutm4oti4mtu0mq==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center "width=" "height=" 212 "/>
More Java learning, Java data http://techfoxbbs.com
Implementation of Web site count JSP