JSP web page simple counter code snippet sharing

Source: Internet
Author: User

It's so cool to share a simple web page counter code I 've written for a while. These are also some basics, mainly involving I/O operations, code is the best mentor. If someone doesn't understand the code I wrote by my brother, you should hurry and forget IT. Don't mix IT up in the IT industry and delay your future.

Er... you are too far away from the topic. The listed code is as follows)

 

Note: For reference, please respect the original labor achievements. Don't explain, everyone on Earth knows. ^-^

 

1. First write a count bean (Name: Counter. java)

[Java]
Package com. songyanjun. utils;
 
Import java. io. BufferedReader;
Import java. io. FileNotFoundException;
Import java. io. FileOutputStream;
Import java. io. FileReader;
Import java. io. IOException;
Import java. io. PrintWriter;
 
/**
* Description: TODO count BEAN.
*
* @ Class Name: Counter
* @ Author: Song yanjun
* @ Mailbox: songyanjun_stars@126.com
* @ Date: Dec 25,201 1 11:10:23
*/
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
 
/*
* Empty Constructor
*/
Public Counter (){}
 

/**
* Description: The TODO ReadFile method is used to read the data in the file filePath and return the data.
*
* @ Title: ReadFile
* @ Param filePath
* @ Return
* @ Throws FileNotFoundException
* @ Return type: String
*/
Public String ReadFile (String filePath) throws FileNotFoundException {
Path = filePath;

File = new BufferedReader (new FileReader (path); // create a new BufferedReader object
String returnStr = null;
Try {
CurrentRecord = file. readLine (); // read a row of data and save it to the currentRecord variable.
}
Catch (IOException e ){
System. out. println ("Data reading error."); // handle the error
}


If (currentRecord = null ){
ReturnStr = "no record"; // if the file is empty
} Else {
ReturnStr = currentRecord; // The file is not empty.
}

Return returnStr; // return the data of the read object.
}
 
/**
* Description: The TODO ReadFile method is used to write the counter + 1 data to the filePath of a text file to increase the count.
*
* @ Title: WriteFile
* @ Param filePath
* @ Param counter
* @ Throws FileNotFoundException
* @ Return type: void
*/
Public void WriteFile (String filePath, String counter) throws FileNotFoundException {
 
Path = filePath;
Int Writestr = Integer. parseInt (counter) + 1; // convert counter to int type and Add 1
Try {
PrintWriter pw = new PrintWriter (new FileOutputStream (filePath); // creates a PrintWriter object for writing data to a file.
Pw. println (Writestr); // print the integer Writestr in text format
Pw. close (); // clear the PrintWriter object
}
Catch (IOException e) {System. out. println ("file write error" + e. getMessage (); // handle the error
}
}
 
} <Span style = "font-family: Arial Black;"> <strong>
</Strong> </span>


2. Create a call page, that is, a JSP page (named WebpageSimpleCounter. jsp)

[Html]
<% @ Page language = "java" pageEncoding = "UTF-8" %>
<% @ Page import = "com. songyanjun. utils. Counter" %>
 
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
<Html>
<Head>

<Title> JSP web page simple counter code snippet </title>
<Meta http-equiv = "X-Ua-Compatible" content = "IE = 7"/>
<Meta http-equiv = "keywords" content = "Jun studio, Song yanjun studio, Song yanjun">
<Meta http-equiv = "description" content = ", create a personalized homepage oh man !!! ">
<Meta http-equiv = "content-type" content = "text/html; charset = UTF-8">
<Meta name = "Copyright" content = "Jun studio http://www.songyanjun.net/"/>
<Link rel = "shortcut icon" href = "favicon. ico"/>
<! --
<Link rel = "stylesheet" type = "text/css" href = "styles.css">
-->
 
</Head>

<Body>
<%
// Create a counting object
Counter counter = new Counter ();

// Call readfileenders of the counterobject to read the count in jishucount.txt.
String cont = counter. ReadFile ("d: \ jishuCount.txt ");

// Call the readfilecounter of the counterobject to add the counter to the jishucount.txt file.
Counter. WriteFile ("d: \ jishuCount.txt", cont );

%>

You are the <font color = "red"> <% = cont %> </font> visitor

</Body>
</Html> <span style = "font-family: Arial Black;"> <strong>
</Strong> </span>

Then directly publish it to Tomcat. Open the browser and you will see that the counter is accumulating with each refresh.



 

O (really idle) o alas, it really hurts .. The code is placed above .. You need to watch it yourself...

 


Over !!!

 

From SongYanJun2011

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.