/*
* javaBean代码
*/
package bean;
public class Box {
double length;
double width;
double height;
public Box()
{
length=0;
width=0;
height=0;
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double volum()
{
return length*width*height;
}
public double surfaceArea()
{
return length*width*2+length*height*2+width*height*2;
}
}
<!-- jsp页面 -->
<!-- 使用javaBean计算长方体的容积和表面积 -->
<%@ page contentType="text/html; charset=gb2312" %>
<%@ page import="bean.Box" %> <!-- Box是要导入的类名,bean是包含这个类文件的包名 -->
<jsp:useBean id="box" class="bean.Box" scope="page" />
<!-- <jsp:useBean id="给javaBean实例取的名称" class="javaBean类名" scope="javaBean实例的有效范围" /> -->
<html>
<body>
<%
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.volum()+"<br>");
out.println("盒子的表面积为:"+box.surfaceArea()+"<br>");
%>
</body>
</html>
JSP代码还可以这样写:
<!-- jsp页面 -->
<!-- 使用javaBean计算长方体的容积和表面积 -->
<%@ page contentType="text/html; charset=gb2312" %>
<%@ page import="bean.Box" %> <!-- Box是要导入的类名,bean是包含这个类文件的包名 -->
<jsp:useBean id="box" class="bean.Box" scope="page" />
<!-- id="给javaBean实例取的名称" class="javaBean类名" scope="javaBean实例的有效范围" -->
<jsp:setProperty property="length" name="box" value="10" />
<jsp:setProperty property="width" name="box" value="11" />
<jsp:setProperty property="height" name="box" value="12" />
<html>
<body>
在JSP中使用javaBean<br>
盒子的长度为:<jsp:getProperty property="length" name="box"/><br>
盒子的宽度为:<jsp:getProperty property="width" name="box"/><br>
盒子的高度为:<jsp:getProperty property="height" name="box"/><br><br><br>
<%
out.println("盒子的容积为:"+box.volum()+"<br>");
out.println("盒子的表面积为:"+box.surfaceArea()+"<br>");
%>
</body>
</html>
也可以用HTML表单设置javaBean的属性值,代码如下:
<!-- 用HTML表单设置javaBean的属性值 -->
<%@ page contentType="text/html; charset=gb2312" %>
<%@ page import="bean.Box" %>
<jsp:useBean id="box" class="bean.Box" scope="page" />
<jsp:setProperty property="*" name="box"/>
<html>
<head>
<title>用HTML表单设置JavaBean的属性</title>
</head>
<body>
<div align="center">
<center>
<table border="1" width="66%">
<form name="form1" action="" method="post">
<tr>
<td width="44%">请输入盒子的长:</td>
<td width="56%"><input type="text" name="length" size="20"></td>
</tr>
<tr>
<td width="44%">请输入盒子的宽:</td>
<td width="56%"><input type="text" name="width" size="20"></td>
</tr>
<tr>
<td width="44%">请输入盒子的高:</td>
<td width="56%"><input type="text" name="height" size="20"></td>
</tr>
<tr>
<td width="100%" colspan="2">
<p align="center">
<input type="submit" name="T1" size="20" value="提交" >
<input type="reset" name="T1" size="20" value="重置" >
</td>
</tr>
</form>
<tr>
<td width="44%">你输入的盒子的长是:</td>
<td width="56%"><%=box.getLength() %></td>
</tr>
<tr>
<td width="44%">你输入的盒子的宽是:</td>
<td width="56%"><%=box.getWidth() %></td>
</tr>
<tr>
<td width="44%">你输入的盒子的高是:</td>
<td width="56%"><%=box.getHeight() %></td>
</tr>
<tr>
<td width="44%">盒子的体积为:</td>
<td width="56%"><%=box.volum() %></td>
</tr>
<tr>
<td width="44%">盒子的表面积为:</td>
<td width="56%"><%=box.surfaceArea() %></td>
</tr>
</table>
</center>
</div>
</body>
</html>