BeansText.jsp
<%@ page language="java" contentType="text/html; charset=gb2312"
pageEncoding="gb2312"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=gb2312">
<META NAME="GENERATOR" CONTENT="Eclipse SDK">
<title>用JavaBeans模块化得文字模式访问计数器</title>
</head>
<body bgcolor="#ffffff">
<jsp:useBean id="counter" scope="request" class="counter.count"/>
<%
counter.path="D:/eclipse/workspace/test/count.txt";
String count=counter.doCount();
%>
<p align="center">
<h1>用JavaBeans模块化得文字模式访问计数器</h1>
<H3 align="center">你是本网页的第
<font color="ff00001" size="5">
<%=count%></font>
名访客!</H3>
</body>
</html>
count.java
package counter;
import java.io.*;
public class count extends Object
{
public String path="";
public String doCount() throws FileNotFoundException
{
BufferedReader file;
String countFile = path;
file = new BufferedReader(new FileReader(countFile));
String readStr="";
int writeStr = 1;
try
{
readStr = file.readLine();
}
catch(IOException e)
{
System.out.println("读取数据错误");
}
if(readStr=="")readStr = "没有任何记录";
else
{
writeStr = Integer.parseInt(readStr)+1;
}
try
{
PrintWriter pw = new PrintWriter(new FileOutputStream(countFile));
pw.println(writeStr);
pw.close();
}
catch(IOException e)
{
System.out.println(e.getMessage());
}
return readStr;
}
}