JSP中的JavaBean ,class文件放在哪个目录?
初学JSP,才问这种菜鸟问题。我用的服务器是Tomcat5.5,编写box.java代码以下:
package first;
public class Box
{
double length;//长
double width;//宽
double height;//高
public Box()
{//构造函数
length=0;
width=0;
height=0;
}
public void setLength(double length)
{//设置长
this.length=length;
}
public double getLength()
{//得到长
return length;
}
public void setWidth(double width)
{//设置宽
this.width=width;
}
public double getWidth()
{//得到宽
return width;
}
public void setHeight(double height)
{//设置高
this.height=height;
}
public double getHeight()
{//得到高
return height;
}
public double volumn()
{//求容积
double volumnValue;
volumnValue=length*width*height;
return volumnValue;
}
public double surfaceArea()
{//求表面积
double surfaceAreaValue;
surfaceAreaValue=length*width*2+width*height*2+height*length*2;
return surfaceAreaValue;
}
}
编译成box.class文件后,把box.class文件放于目录
Tomcat 5.5\webapps\ROOT\WEB-INF\classes\first下
然后再编写JavaBeanTest.jsp代码:
<%@ page contentType= "text/html;charset=GB2312 " %>
<%@ page import= "first.* " %>
<HTML>
<BODY>
<jsp:useBean id= "box " class= "Box " scope= "page ">
</jsp:useBean>
<%
box.setLength(10);
box.setWidth(11);
box.setHeight(12);
out.println( "在JSP中使用JavaBean <br> ");
out.println( "盒子的长度为: "+box.getLength()+ " <br> ");
out.println( "盒子的宽度为: "+box.getWidth()+ " <br> ");
out.println( "盒子的高度为: "+box.getHeight()+ " <br> ");
out.println( "盒子的容积为: "+box.volumn()+ " <br> ");
out.println( "盒子的表面积为: "+box.surfaceArea()+ " <br> ");
%>
</BODY>
</HTML>
运行JavaBeanTest.jsp出现以下错误:
HTTP Status 500 -
--------------------------------------------
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: /55/javaBeanJSP1.jsp(5,0) The value for the useBean class attribute Box is invalid.
org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:40)
org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:407)
org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:148)
org.apache.jasper.compiler.Generator$GenerateVisitor.visit(Generator.java:1175)
org.apache.jasper.compiler.Node$UseBean.accept(Node.java:1117)
org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2164)
org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2214)
org.apache.jasper.compiler.Node$Visitor.visit(Node.java:2220)
org.apache.jasper.compiler.Node$Root.accept(Node.java:457)
org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2164)
org.apache.jasper.compiler.Generator.generate(Generator.java:3322)
org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:199)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:296)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:277)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:265)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:564)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:299)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:315)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
note The full stack trace of the root cause is available in the Apache Tomcat/5.5.23 logs.
--------------------------------------------
Apache Tomcat/5.5.23
本人试过把box.java的“package first;”语句去除,也试过把JavaBeanTest.jsp中的 <%@ page import= "first.* " %> 改成 <%@ page import= "first.box " %> ,还是出错。
请高手指教,感激不尽!
[解决办法]
改成这样试一下:
<jsp:useBean id= "box " class= "first.Box " scope= "page ">
</jsp:useBean>
[解决办法]
编写javaBean必须把类文件放在一个包中,编译后放在classes目录下面,当你修改了java文件后,记住一定要重新启动tomcat服务器!例:一个student.java
package first;
.
.
.
编译成功后将会有一个first文件夹,下面有student.class,然后的first文件夹放在classes目录下面
使用 <jsp:useBean id= "student " class= "first.student " scope= "page "> 则可以了
我的异常网推荐解决方案:The server encountered an internal error () that prevented it from fulfilling this request.,http://www.myexception.cn/java-web/317.html