JSP counter production _jsp programming

Source: Internet
Author: User
Tags error handling readfile
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 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, 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. )



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.