A JSP Web Access counter using TXT text (without any database)

Source: Internet
Author: User
Tags format count empty error handling integer readfile variable
js| Access | counter | data | A bean for database//counter.java read and write files


===========================


//website read-write txt format counter


package net.com.util;


 


import java.io.*;

 public class Counter extends Object {
 private String CurrentRecord = null;//Save text variable
 private BufferedReader file; BufferedReader object that is used to read the file data
 private String path;//file full pathname
 public Counter () {
 }
// The ReadFile method is used to read the data in the file FilePath and returns this data
 public string ReadFile (String filePath) throws FileNotFoundException
 {
 path = FilePath;
Creates a new BufferedReader object,
 file = newer bufferedreader (path);
 string Returnstr =null;
 try
 {
//reads a row of data and saves it to the CurrentRecord variable
 currentrecord = File.readline ();
&NBSP}
 catch (IOException e)
 {//error handling
 system.out.println ("read data error.");
 }
 if (CurrentRecord = = null)
//If the file is empty
 returnstr = "no records";
 else
 {//file is not empty
 returnstr =currentrecord;
&NBSP}
//Returns the data read from the file
 return returnstr
&NBSP} The
//ReadFile method is used to write data counter+1 to a text file filepath
//to implementCount growth feature
 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 PrintWriter object to write data to file
 printwriter pw = new PrintWriter (new FileOutputStream (FilePath) );
//print integer writestr in text Format
 pw.println (WRITESTR);
Clear the PrintWriter object
 pw.close ();
&NBSP} catch (IOException e) {
//error handling
 system.out.println ("Write File Error" +e.getmessage ());
&NBSP}
 }

}

====================================
counter.jsp file
<%@ page contenttype= "TEXT/HTML;CHARSET=GBK"%>
<!--Create and invoke bean (counter)-->
<jsp:usebean id= "Counter" scope= "page" class= "Net.com.util.Counter"/>
<%
Call the Counter object's ReadFile method to read the count in the file Lyfcount.txt
String Url=request.getrealpath ("Count.txt");
String Cont=counter. ReadFile (URL);
Calls the Counter object's ReadFile method to add a counter to the file Lyfcount.txt
Counter. WriteFile (Url,cont);%>
You are the first <font color= "Red" > <%=cont%> </font> site visitor

======================================
Note: Create a count.txt file in the same directory as counter. The initial number is 0.
======================================










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.