Counters are a must-have for ordinary websites. Don’t underestimate them. Whenever the webmaster watches the numbers on the small counter grow rapidly, it feels really great. In the past, we used cgi and asp to write counters, and there were many articles on this. Here, we will use the currently popular jsp technology to demonstrate how to make a counter.
We used two files, which are used to run in the browser, and are a small java bean program in the background, which is used to read the counter value and write the counter value. For the saving of counters, we use a text file. The following is the detailed program code (put it in the web directory and in the class directory):
//document
<%@ page contentType="text/html;charset=gb2312"%>
<HTML>
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<TITLE>Counter Demo Program</TITLE>
</HEAD>
<BODY>
<!--Create and call bean(counter)-->
<jsp:useBean class="counter" scope="request">
</jsp:useBean>
<%
//Call the ReadFile method of the counter object to read the count in the file
String cont=("/");
//Call the ReadFile method of the counter object to add the counter and write it to the file
("/",cont);%>
You are the first visitor to
</BODY>
</HTML>
// A bean that reads and writes files
import .*;
public class counter extends Object {
private String currentRecord = null;//Save the variables for text
private BufferedReader file; //BufferedReader object, used to read file data
private String path;//Full path name of the file
public counter() {
}
//ReadFile method is used to read the data in the file filePath and return this data
public String ReadFile(String filePath) throws FileNotFoundException
{
path = filePath;
//Create a new BufferedReader object
file = new BufferedReader(new FileReader(path));
String returnStr =null;
try
{
//Read a row of data and save it into the currentRecord variable
currentRecord = ();
}
catch (IOException e)
{//Error handling
("Read data error.");
}
if (currentRecord == null)
//If the file is empty
returnStr = "No record";
else
{//The file is not empty
returnStr =currentRecord;
}
//Return the data of the read file
return returnStr;
}
//ReadFile method is used to write data counter+1 to the text file filePath
//To achieve count growth function
public void WriteFile(String filePath,String counter) throws
FileNotFoundException
{
path = filePath;
//Convert counter to int type and add one
int Writestr = (counter)+1;
try {
//Create PrintWriter object to write data into file
PrintWriter pw = new PrintWriter(new FileOutputStream(filePath));
//Print integer Writestr in text format
(Writestr);
//Clear PrintWriter object
();
} catch(IOException e) {
//Error handling
("Write file error"+());
}
}
}
At this point, after the program is finished, it will be compiled into, and placed in the corresponding class directory. Create a file in the root directory, and the file content is a number 0. You can directly type in the address in the browser to see the counter. When you refresh the browser, you will see the changing numbers. (If the file cannot be found during runtime, please run the readfile comment above and the file will be automatically created and then it can run normally.)