---jsp代码---
<%@ page language= "java " contentType= "text/html " import= "java.io.* "
pageEncoding= "GB2312 "%>
<html>
<head>
<title> 使用javabean统计的访问计算器 </title>
</head>
<body bgcolor= "#FFFFFF ">
<jsp:useBean id= "counter " scope= "request " class= "counter.counter "/>
<%
counter.path= "D:/count.txt ";
String count=counter.docount();
%>
<p align= "center ">
<H1> 文字模仿式计算器: </H1>
<H3> 你是本页的第 <%=count %> 名访客 </H3>
</body>
</html>
---javabean代码---
package counter;
import java.io.*;
public class counter 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.print( "读取数据错误 ");
}
if (readstr== " ")
{
readstr= "没有任何记录 ";
}
else
{
writestr=Integer.parseInt(readstr)+1;
}
try
{
PrintWriter pw;
pw=new PrintWriter(new FileOutputStream(countfile));
pw.write(writestr);
pw.close();
}
catch(IOException e)
{
System.out.print(e.getMessage());
}
return readstr;
}
}
---错误---
org.apache.jasper.JasperException: Exception in JSP: /beantext.jsp:12
9: <jsp:useBean id= "counter " scope= "request " class= "counter.counter "/>
10: <%
11: counter.path= "D:/count.txt ";
12: String count=counter.docount();
13: %>
14: <p align= "center ">
15: <H1> 文字模仿式计算器: </H1>
------解决方案--------------------
docount有可能会抛出FileNotFoundException的,catch一下
------解决方案--------------------
<%
try{
counter.path= "D:/count.txt ";
String count=counter.docount();
}catch(FileNotFoundException e){
}
%>