jsp counter production
Author:Eve Cole
Update Time:2009-07-02 17:12:55
Counters are a must-have for general websites. Don't underestimate them. Whenever the webmaster sees the numbers on the small counter growing rapidly, he feels really good. In the past, we used cgi and asp to write counters. There are many articles in this area. Here, we will use the currently popular jsp technology to demonstrate how to make a counter.
We used two files, the test.jsp file is used to run in the browser, and counter.java is a small java bean program in the background, used to read the counter value and write the counter value. For saving the counter, we use a text file lyfcount.txt.
The following is the detailed program code (test.jsp is placed in the web directory, counter.java is placed in the class directory):
//test.jsp file
<%@ page contentType="text/html;charset=gb2312"%>
<HTML>
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<TITLE>Counter demonstration program</TITLE>
</HEAD>
<BODY>
<!--Create and call bean(counter)-->
<jsp:useBean id="counter" class="counter" scope="request">
</jsp:useBean>
<%
//Call the ReadFile method of the counter object to read the count in the file lyfcount.txt
String cont=counter.ReadFile("/lyfcount.txt");
//Call the ReadFile method of the counter object to increment the counter and write it to the file lyfcount.txt
counter.WriteFile("/lyfcount.txt",cont);%>
You are the <font color="red"><%=cont%></font> visitor
</BODY>
</HTML>
//counter.java is a bean that reads and writes files
import java.io.*;
public class counter extends Object {
private String currentRecord = null;//Variable to save text
private BufferedReader file; //BufferedReader object, used to read file data
private String path;//Full path name of 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 to the currentRecord variable
currentRecord = file.readLine();
}
catch (IOException e)
{//Error handling
System.out.println("Error reading data.");
}
if (currentRecord == null)
//If the file is empty
returnStr = "No records";
else
{//The file is not empty
returnStr =currentRecord;
}
//Return the data from the read file
return returnStr;
}
//ReadFile method is used to write the data counter+1 to the text file filePath.
//To achieve the function of counting growth
public void WriteFile(String filePath,String counter) throws
FileNotFoundException
{
path = filePath;
//Convert counter to int type and add one
int Writestr = Integer.parseInt(counter)+1;
try {
//Create a PrintWriter object for writing data to the file
PrintWriter pw = new PrintWriter(new FileOutputStream(filePath));
//Print integer Writestr in text format
pw.println(Writestr);
//Clear the PrintWriter object
pw.close();
} catch(IOException e) {
//Error handling
System.out.println("Writing file error"+e.getMessage());
}
}
}
At this point, the program is finished, compile counter.java into counter.class, and also put it in the corresponding
In the class directory, create a lyfcount.txt file in the root directory. The content of the file is a number 0, directly in
You can see the counter by typing the address into the browser. Refresh the browser and you will see the changing numbers.
(If it prompts that the file cannot be found when running, please comment the readfile sentence in test.jsp above and run it.
Once the lyfcount.txt file is automatically created, it can run normally. )