当前位置: 代码迷 >> Java Web开发 >> jsp+javabean计数器有关问题,请大侠帮忙看看异常在哪
  详细解决方案

jsp+javabean计数器有关问题,请大侠帮忙看看异常在哪

热度:63   发布时间:2016-04-17 14:21:42.0
jsp+javabean计数器问题,请大侠帮忙看看错误在哪?
---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){
}
%>
  相关解决方案