JSP counter Creation

Source: Internet
Author: User
Tags readfile

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. Previously, we used CGI and ASP to write counters. Article A lot. 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, and counter. Java is a small Java Bean in the background. Program 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 detailed procedures are as follows:Code(Put test. jsp in the web directory and counter. Java 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 demo </title>
</Head>
<Body>
<! -- Create and call bean (Counter) -->
<JSP: usebean id = "counter" class = "counter" Scope = "request">
</Jsp: usebean>
<%
// 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 <font color = "red"> <% = cont %> </font> visitor
</Body>
</Html>

// 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 () {
}< br> // The readfile method is used to read the data in the file filepath and return the data.
Public String readfile (string filepath) throws filenotfoundexception
{< br> Path = filepath;
// create a new bufferedreader object
file = new bufferedreader (New filereader (PATH ));
string returnstr = NULL;
try
{< br> // read a row of data and save it to the currentrecord variable
currentrecord = file. readline ();
}< br> catch (ioexception e)
{// handle errors
system. out. println ("Data reading error. ");
}< br> If (currentrecord = NULL)
// if the file is empty
returnstr =" no record ";
else
{// the file is not empty
returnstr = currentrecord;
}< br> // return the data read from the file
return returnstr;
}< br> // The readfile method is used to write the counter + 1 data to the filepath of the text file.
// The Count increase function
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 .)

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.