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出