Counters are essential to websites. Don't underestimate them. every time the webmaster looks at the rapid growth of numbers on a small counter, it feels really good. In the past, we used cgi and asp to write counters. There are many articles in this regard. here, we will use the popular jsp technology to demonstrate how to make a counter. Here we use two files: test. js? 1.1.26 counters
Counters are essential to websites. Don't underestimate them. every time the webmaster looks at the rapid growth of numbers on a small counter, it feels really good. In the past, we used cgi and asp to write counters. There are many articles in this regard. here, we will use the popular jsp technology to demonstrate how to make a counter.
Two files are used. the test. jsp file is used to run in the browser. counter. java is a small java bean program in the background to read the counter value and write the counter value. For the storage of the counter, we have stored a file named lyfcount.txt.
The following is the detailed program code (put test. jsp to the web directory, and put counter. java to the class directory ):
// Test. jsp file
<% @ Page contentType = "text/html; charset = gb2312" %>
Counter demo
<%
// Call readfileenders of the counterobject to read the count in lyfcount.txt.
String cont = counter. ReadFile ("/lyfcount.txt ");
// Call the readfilecounter of the counterobject to add the counter to the lyfcount.txt file.
Counter. WriteFile ("/lyfcount.txt", cont); %>
You are the <% = cont %> visitor
// Counter. java: a bean for reading and writing files
Import java. io .*;
Public class counter extends Object {
Private String currentRecord = null; // variable for saving text
Private BufferedReader file; // BufferedReader object, used to read file data
Private String path; // Complete file path name
Public counter (){
}
// The ReadFile method is used to read the data in the file filePath and return the 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)
{// Handle errors
System. out. println ("data reading error .");
}
If (currentRecord = null)
// If the file is empty
ReturnStr = "no record ";
Else
{// The file is not empty.
ReturnStr = currentRecord;
}
// Return the data to read the file
Return returnStr;
}
// The ReadFile method is used to write the counter + 1 data to the filePath of the text file.
// To increase the count
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 a file
PrintWriter pw = new PrintWriter (new FileOutputStream (filePath ));
// Print the integer Writestr in text format
Pw. println (Writestr );
// Clear the PrintWriter object
Pw. close ();
} Catch (IOException e ){
// Handle errors
System. out. println ("file writing error" + e. getMessage ());
}
}
}
At this point, the program is finished, and counter. java is compiled into counter. class, which is also placed in the corresponding
In the classdirectory, create a lyfcount.txt file under the root directory. the file content is a number 0, directly in
Click the address in the browser to view the counter. refresh the browser to see the changing numbers.
(If the system prompts that the file cannot be found during running, run the following comments on readfile in test. jsp.
Then, the lyfcount.txt file is automatically created and then runs properly .)