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 ">
<meta name= "generator" content= "Oracle jdeveloper" >
<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 to save text
private BufferedReader file; BufferedReader object, for reading file data
Private String path;//file full 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
{
Reads a row of data and saves it 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 record";
Else
{//File not empty
Returnstr =currentrecord;
}
Returns the data read from a 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, file content on a number 0, directly in the browser typing address can see the counter, refresh the browser will see the number of constantly changing.
(If you cannot find the file at run time, please readfile the comment in test.jsp above and run it once lyfcount.txt the file is automatically established, and then you can run it correctly.) )