js| counter
The counter is the general website Essential Dongdong, do not underestimate it, each time stationmaster looks at the small counter the numeral to grow rapidly, the feeling is very great. We used CGI, ASP to write counters, this article a lot of, here, we will use the current more popular JSP technology to demonstrate how to do a counter.
We used two files, test.jsp files to run in the browser, Counter.java is a small Java bean program in the background, used to read counter values and write counter values. And for the preservation of the counter, we adopted a text file lyfcount.txt.
The following is a detailed program code (test.jsp to the Web directory, Counter.java to 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 Demo program </TITLE>
</HEAD>
<BODY>
<!--Create and invoke bean (counter)-->
<jsp:usebean id= "Counter" class= "counter" scope= "Request" >
</jsp:useBean>
<%
Call the Counter object's ReadFile method to read the count in the file Lyfcount.txt
String Cont=counter. ReadFile ("/lyfcount.txt");
Calls the Counter object's ReadFile method to add a counter to the file Lyfcount.txt
Counter. WriteFile ("/lyfcount.txt", cont);%>
You are the first <font color= "Red" ><%=cont%></font> site visitor
</BODY>
</HTML>
Counter.java a bean to read and write files
Import java.io.*;
Public class Counter extends Object {
Private String CurrentRecord = null;//variable for saving text
Private BufferedReader file ; BufferedReader object that is used to read the file data
Private String path;//file full pathname
Public counter () {
}
// The ReadFile method is used to read the data in the file FilePath and returns this data
public String ReadFile (string filePath) throws FileNotFoundException
{
Path = FilePath;
//To create a new BufferedReader object
, file = newer bufferedreader (path);
String Returnstr =null;
Try
{
//read a row of data and save to the CurrentRecord variable
CurrentRecord = File.readline ();
}
catch (IOException e)
{//Error handling
System.out.println ("read data error.");
}
if (CurrentRecord = null)
//If the file is empty
Returnstr = "no records";
Else
{//File not empty
Returnstr =currentr Ecord;
}
//Returns the data read from the file
return RETURNSTR
} The
//readfile method is used to write data counter+1 to a text file FilePath
//To achieve the function of counting growth
public void WriteFile (String filepath,string Counter) throws
FileNotFoundException
{
Path = FilePath;
Converts the counter to an int type and adds a
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 integer writestr in text Format
Pw.println (WRITESTR);
Clear PrintWriter Object
Pw.close ();
catch (IOException e) {
Error handling
System.out.println ("Write File Error" +e.getmessage ());
}
}
}
Here, the program is finished, the Counter.java compiled to Counter.class, also placed in the corresponding
Class directory, in the root directory to create a lyfcount.txt file, the contents of the file is a number 0, directly in the
The browser can be typed in the address to see the counter, refresh the browser will see the constantly changing numbers.
(If you cannot find the file at run time, please test.jsp the ReadFile in the above box to run
Once the Lyfcount.txt file is automatically created, and then you can run it correctly. )