JSP counter creation Manual

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.ProgramTo 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">
<Meta name = "generator" content = "Oracle jdeveloper">
<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 (){
}
// 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 ());
}
}

}

In the inventory file, the file content is a number 0. You can directly enter the address in the browser to see the counter, and refresh the browser to see the changing numbers.

(If the file cannot be found at runtime, note readfilena in test.jspand run the scripts. Then, the lyfcount.txt file is automatically created and 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.