javaBean的工作机制是什么?我的javabean怎么总是报错?
jsp页面:--------------
<%@ page contentType= "text/html; charset=gb2312 " language= "java " import= "java.sql.* " errorPage= "error.jsp " %>
<!--创建并调用bean(counter)-->
<jsp:useBean id= "counter " scope= "request " class= "Counter ">
</jsp:useBean>
<html>
<head>
<title> 出错演示 </title>
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 ">
</head>
<body>
</body>
</html>
javabean文件:-----------
//counter.java 读写文件的一个bean
import java.io.*;
public class counter extends object {
private string currentrecord = null;//保存文本的变量
private bufferedreader file; //bufferedreader对象,用于读取文件数据
private string path;//文件完整路径名
public counter() {
}
//readfile方法用来读取文件filepath中的数据,并返回这个数据
public string readfile(string filepath) throws filenotfoundexception
{
path = filepath;
//创建新的bufferedreader对象
file = new bufferedreader(new filereader(path));
string returnstr =null;
try
{
//读取一行数据并保存到currentrecord变量中
currentrecord = file.readline();
}
catch (ioexception e)
{//错误处理
system.out.println( "读取数据错误. ");
}
if (currentrecord == null)
//如果文件为空
returnstr = "没有任何记录 ";
else
{//文件不为空
returnstr =currentrecord;
}
//返回读取文件的数据
return returnstr;
}
//readfile方法用来将数据counter+1后写入到文本文件filepath中
//以实现计数增长的功能
public void writefile(string filepath,string counter) throws
filenotfoundexception
{
path = filepath;
//将counter转换为int类型并加一
int writestr = integer.parseint(counter)+1;
try {
//创建printwriter对象,用于写入数据到文件中
printwriter pw = new printwriter(new fileoutputstream(filepath));
//用文本格式打印整数writestr
pw.println(writestr);
//清除printwriter对象
pw.close();
} catch(ioexception e) {
//错误处理
system.out.println( "写入文件错误 "+e.getmessage());
}
}
}
最后报错的内容是:--------
org.apache.jasper.JasperException: Unable to compile class for JSP
An error occurred at line: 3 in the jsp file: /test.jsp
Generated servlet error:
[javac] Compiling 1 source file
D:\server\Tomcat\work\Catalina\localhost\jsq\org\apache\jsp\test_jsp.java:42: cannot resolve symbol
symbol : class Counter
location: class org.apache.jsp.test_jsp
Counter counter = null;
^
An error occurred at line: 3 in the jsp file: /test.jsp
Generated servlet error:
D:\server\Tomcat\work\Catalina\localhost\jsq\org\apache\jsp\test_jsp.java:44: cannot resolve symbol
symbol : class Counter
location: class org.apache.jsp.test_jsp
counter = (Counter) pageContext.getAttribute( "counter ", PageContext.REQUEST_SCOPE);
^
An error occurred at line: 3 in the jsp file: /test.jsp
Generated servlet error:
D:\server\Tomcat\work\Catalina\localhost\jsq\org\apache\jsp\test_jsp.java:46: cannot resolve symbol
symbol : class Counter
location: class org.apache.jsp.test_jsp
counter = new Counter();
^
3 errors
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:127)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:351)
org.apache.jasper.compiler.Compiler.generateClass(Compiler.java:415)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:458)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:439)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:552)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:291)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
[解决办法]
package com.jsq;
import java.io.*;
public class Counter extends Object {
private String currentRecord = null;
private BufferedReader file;
private String path;
public Counter() {
}
public String ReadFile(String filePath) throws FileNotFoundException {
path = filePath;
file = new BufferedReader(new FileReader(path));
String returnStr = null;
try {
currentRecord = file.readLine();
} catch (IOException e) {
System.out.println( "读取错务 ");
}
if (currentRecord == null)
returnStr = "没有任何记录 ";
else {
returnStr = currentRecord;
}
return returnStr;
}
public void WriteFile(String filePath, String counter)
throws FileNotFoundException {
path = filePath;
int Writestr = Integer.parseInt(counter) + 1;
try {
PrintWriter pw = new PrintWriter(new FileOutputStream(filePath));
pw.println(Writestr);
pw.close();
} catch (IOException e) {
System.out.println( "错误 " + e.getMessage());
}
}
public String getCurrentRecord() {
return currentRecord;
}
public void setCurrentRecord(String currentRecord) {
this.currentRecord = currentRecord;
}
public BufferedReader getFile() {
return file;
}
public void setFile(BufferedReader file) {
this.file = file;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
}
换成上面的 还有
string cont=counter.readfile( "lyfcount.txt ");
改成String
我的异常网推荐解决方案:org.apache.jasper.JasperException: Unable to compile class,http://www.myexception.cn/j2ee/2308.html