访问计数器:(JSP+JavaBean)
icounter.jsp
其代码如下:
%@page contentType="text/html;charset=gb2312"%>   
<%@page import="java.io.*"%>   
<%@page import="java.util.*"%>   
<jsp:useBean id="icounter" scope="request" class="mypack.icounter"/>   
<html>   
<head>   
<meta http-equiv="Content-Type" content="text/html;charset=gb2312">   
<title>图形计数器</title>   
</head>   
<body>   
<%   
     icounter.path="C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\myapp\icounter.txt";   
     char[]counts=icounter.doCount();   
%>     
你是本网站的第   
<%   
   for(int i=0;i<counts.length;i++)   
  {   
   out.print("<img src=\""+counts[i]+".gif\"width=\"38\"height=\"55\">");   
        }   
%>   
位访客   
</body>   
</html>%@page contentType="text/html;charset=gb2312"%>   
<%@page import="java.io.*"%>   
<%@page import="java.util.*"%>   
<jsp:useBean id="icounter" scope="request" class="mypack.icounter"/>   
<html>   
<head>   
<meta http-equiv="Content-Type" content="text/html;charset=gb2312">   
<title>图形计数器</title>   
</head>   
<body>   
<%   
     icounter.path="C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\myapp\icounter.txt";   
     char[]counts=icounter.doCount();   
%>     
你是本网站的第   
<%   
   for(int i=0;i<counts.length;i++)   
  {   
   out.print("<img src=\""+counts[i]+".gif\"width=\"38\"height=\"55\">");   
        }   
%>   
位访客   
</body>   
</html>icounter.java 代码:::package mypack;
import java.io.*;
public class icounter extends Object 
{
 public String path="";
 public char[] counts;
 public char[] 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("读取数据错误");
    }
   // file.close();
    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());
      }
      counts=readStr.toCharArray();
      return counts;
      }
      }
运行后:出现这样的错误
type 异 常 报 告信 息 描 述 服务器遇到内部错误,无法满足这个访问请求异 常 org.apache.jasper.JasperException: /icounter.jsp(4,0) useBean类的属性 mypack.icounter的数值无效。 org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:512)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
起 因 org.apache.jasper.JasperException: /icounter.jsp(4,0) useBean类的属性 mypack.icounter的数值无效。 org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:39)
org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:405)
org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:146)
org.apache.jasper.compiler.Generator$GenerateVisitor.visit(Generator.java:1174)
org.apache.jasper.compiler.Node$UseBean.accept(Node.java:1116)
org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2163)
org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2213)
org.apache.jasper.compiler.Node$Visitor.visit(Node.java:2219)
org.apache.jasper.compiler.Node$Root.accept(Node.java:456)
org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2163)
org.apache.jasper.compiler.Generator.generate(Generator.java:3320)
org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:198)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:295)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:276)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:264)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:563)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:305)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
注 意 完整的起因在Apache Tomcat/5.5.20里。
请问高手,是什么原因???急,谢谢!!!

解决方案 »

  1.   

    文件名 和类名都改成 Icounter试下 
      

  2.   

    <jsp:useBean id="icounter" scope="request" class="mypack.icounter"/>   
    意思是找不到icounter这个类请参考我们教程中使用useBean的例子:http://test.family168.com/tutorial/jsp/html/jsp-ch-05.html
      

  3.   

    个人认为问题可能出在bean里icount的属性调用上~~在jsp用<%page%>也导一下icount类~确认类路径正确~~用bean的set方法代替icount.path这样的直接调用~~最后一个土办法~~插输出语句~~在命令行方式下的tomcat里看执行情况~~