1.JavaBean是一个可重复使用的软件组件,是遵循一定标准、用Java语言编写的一个类,该类的一个实例称为一个JavaBean,简称bean .
2.编写bean
1.如果类的成员变量的名字是xxx,那么为了获取或更改成员变 量的值,即获取或更改属性,类中必须提供两个方法:
getXxx() 用来获取属性xxx
setXxx() 用来修改属性xxx
即方法的名字用get或set为前缀,后缀是将成员变量名字的首字母大写的字符序列。
2.对于boolean类型的成员变量,即布尔逻辑类型的属性,允许使用“is”代替上面的“get”和“set”。
3.类中声明的方法的访问属性都必须是public的。
4.类中声明的构造方法必须是public、无参数的。
3. Bean的保存
在当前Web服务目录下建立如下目录结构:
Web服务目录\WEB-INF\classes
根据类的包名,在目录classes下建立相应的子目录.比如:
Web服务目录\WEB-INF\classes\tom\jiafei
4. JSP页面中使用bean
使用JSP动作标记:useBean。useBean标记的格式:
<jsp:useBean id= "给bean起的名字" class= "创建bean的类" scope= "bean有效范围">
</jsp:useBean>
或
<jsp:useBean id= "给bean起的名字" class= "创建bean的类" scope= "bean有效范围"/>
5.动作标签getProperty
使用该标记可以获得bean的属性值 .
格式如下:
<jsp:getProperty name= "bean的名字" property= "bean的属性" />
或
<jsp:getProperty name= "bean的名字" property= "bean的属性"/>
</jsp:getProperty>
6.动作标记setProperty
(1)使用该标记可以设置bean的属性值
(2)设置bean的属性为一个表达式的值的格式如下:
<jsp:setProperty name="bean的名字" property= "bean的属性"
value= "<%=expression%>" />
(3)通过HTTP表单的参数的值来设置bean的相应属性的值:
<jsp:setProperty name="bean的名字" property="*" />
(4)通过request的参数的值来设置bean的相应属性的值,要求request参数名字必须与bean属性的名字相同其格式如下:
<jsp:setProperty name= "bean的名字" property="属性名" param= "参数名" />
7. bean的辅助类
有时在写一个bean的时候,可能还需要自己编写的其他类,那么只要将这些类和创建bean的类写在一个Java源中即可,但必须按将源文件编译后产生的全部字节码文件复制到相应的目录中 .
8.JSP与bean结合的简单例子
结合一些具体的实际问题,进一步熟悉掌握bean的使用。在本节中,创建bean的类的包名都是red.star,使用的Web服务目录仍然是ch4,因此,需要在ch4下建立如下的目录结构:
ch4\WEB-INF\classes\red\star
创建bean的字节码文件都需要保存在上述目录中。为了便于调试类文件,可将创建bean的Java源文件保存到D:\2000中,然后将编译得到的字节码文件复制到上述目录中。
Circle.java
package tom.jiafei
import java.io.*;
public class Circle
{ int radius;
public Circle()
{ radius=1;
}
public int getRadius()
{ return radius;
}
public void setRadius(int newRadius)
{radius=newRadius;
}
public double circleArea()
{return Math.PI*radius*radius;
}
public double circlLength()
{return 2.0*Math.PI*radius;
}
}
例子1
useBean.jsp
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="tom.jiafei.Circle"%>
<HTML><BODY bgcolor=cyan><Font size=3>
<jsp:useBean id="circle" class="tom.jiafei.Circle" scope="page" />
<%--通过上述JSP标记,客户获得了一个作用域是page,名字是circle的bean --%>
<% circle.setRadius(888);
%>
<P>圆的半径是:<%=circle.getRadius()%>
<P>圆的周长是:<%=circle.circlLength()%>
<P>圆的面积是:<%=circle.circleArea()%>
</BODY></HTML>
例子2
bean1.jsp
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="tom.jiafei.Circle"%>
<HTML><BODY bgcolor=cyan><Font size=3>
<jsp:useBean id="girl" class="tom.jiafei.Circle" scope="session" />
<P>圆的半径是: <%=girl.getRadius()%>
<A href="bean2.jsp"><BR>bean2.jsp </A>
</BODY></HTML>
bean2.jsp
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="tom.jiafei.Circle"%>
<HTML><BODY bgcolor=cyan><Font size=3>
<jsp:useBean id="girl" class="tom.jiafei.Circle" scope="session" />
<P>圆的半径是: <%=girl.getRadius()%>
<%girl.setRadius(400);%>
<P>修改后的圆的半径是:<%=girl.getRadius()%>
<A href="bean1.jsp"><BR>bean1.jsp </A>
</BODY></HTML>
例子3
appbean.jsp
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="tom.jiafei.*"%>
<HTML> <BODY>
<jsp:useBean id="girl" class="tom.jiafei.Circle" scope="application" >
</jsp:useBean>
<P>圆的初始半径是:<%=girl.getRadius()%>
<%girl.setRadius(1000);%>
<P>修改后的圆的半径是:<%=girl.getRadius()%>
</BODY></HTML>
NewCircle.java
package tom.jiafei;
import java.io.*;
public class NewCircle
{ double radius=1,circleArea,circleLength;
public double getRadius()
{ return radius;
}
public void setRadius(double newRadius)
{ radius=newRadius;
}
public double getCircleArea()
{ circleArea=Math.PI*radius*radius;
return circleArea;
}
public double getCircleLength()
{ circleLength=2.0*Math.PI*radius;
return circleLength;
}
}
例子4
beanGetProperty.jsp
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="tom.jiafei.NewCircle"%>
<HTML><BODY bgcolor=cyan><Font size=3>
<jsp:useBean id="apple" class="tom.jiafei.NewCircle" scope="request"/>
<% apple.setRadius(100);%>
圆的半径是:<jsp:getProperty name="apple" property= "radius"/>
<BR>圆的面积是:<jsp:getProperty name="apple" property= "circleArea"/>
<BR>圆的周长是:<jsp:getProperty name="apple" property= "circleLength"/>
</BODY></HTML>
Student.java
public class Student
{ String name=null;
long number;
double height,weight;
public String getName()
{ return name;
}
public void setName(String newName)
{name=newName;
}
public long getNumber()
{return number;
}
public void setNumber(long newNumber)
{ number=newNumber;
}
public double getHeight()
{return height;
}
public void setHeight(double newHeight)
{ height=newHeight;
}
public double getWeight()
{return weight;
}
public void setWeight(double newWeight)
{ weight=newWeight;
}
}
例子5
student.jsp
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="tom.jiafei.Student"%>
<HTML><BODY bgcolor=cyan><Font size=3>
<jsp:useBean id="zhang" class="tom.jiafei.Student" scope="page"/>
<jsp:setProperty name="zhang" property="name" value="张三"/>
名字是:<jsp:getProperty name="zhang" property="name"/>
<jsp:setProperty name="zhang" property="number" value="2008001"/>
<BR>学号是:<jsp:getProperty name="zhang" property="number"/>
<jsp:setProperty name="zhang" property="height" value="<%=1.78%>"/>
<BR>身高是:<jsp:getProperty name="zhang" property="height"/> 米
<jsp:setProperty name="zhang" property="weight" value="67.65"/>
<BR>体重是:<jsp:getProperty name="zhang" property="weight"/> 公斤
</FONT></BODY></HTML>
Goods.java
package tom.jiafei;
public class Goods
{ String name,goodsNumber;
double price;
public String getName()
{ return name;
}
public void setName(String newName)
{ name=newName;
}
public String getGoodsNumber()
{ return goodsNumber;
}
public void setGoodsNumber(String s)
{ goodsNumber=s;
}
public double getPrice()
{ return price;
}
public void setPrice(double newPrice)
{ price=newPrice;
}
}
例子6
goods.jsp
<%@ page contentType="text/html;Charset=GB2312" %>
<%@ page import="tom.jiafei.*"%>
<jsp:useBean id="goods" class="tom.jiafei.Goods" scope="page"/>
<HTML><BODY bgcolor=yellow><Font size=3>
<FORM action="" Method="post" >
输入商品的名称:<Input type=text name="name">
<BR>输入商品的代号:<Input type=text name="goodsNumber">
<BR>输入商品的价格: <Input type=text name="price">
<Input type=submit value="提交">
</FORM>
<P>提交表单才能设置商品的信息
<jsp:setProperty name="goods" property="*"/>
<BR>商品的名称:<jsp:getProperty name="goods" property="name"/>
<BR>商品的代号:<jsp:getProperty name="goods" property="goodsNumber"/>
<BR>商品的价格:<jsp:getProperty name="goods" property="price"/>
</FONT></BODY></HTML>
ListFile.java
package tom.jiafei;
import java.io.*;
class FileName implements FilenameFilter
{ String str=null;
FileName (String s)
{ str="."+s;
}
public boolean accept(File dir,String name)
{ return name.endsWith(str);
}
}
public class ListFile
{ String extendsName=null;
StringBuffer allFileName;
public void setExtendsName(String s)
{ extendsName=s;
}
public String getExtendsName()
{ return extendsName;
}
public StringBuffer getFileName()
{ File dir=new File("d:/2000");
FileName help=new FileName(extendsName);
String file_name[]=dir.list(help);
for(int i=0;i<file_name.length;i++)
{ allFileName.append(file_name[i]+" ");
}
return allFileName;
}
}
例子7
file.jsp
<%@ page contentType="text/html;Charset=GB2312" %>
<%@ page import="tom.jiafei.*"%>
<jsp:useBean id="file" class="tom.jiafei.ListFile" scope="page"/>
<HTML><BODY bgcolor=cyan><Font size=2>
<FORM action="" Method="post">
输入文件的扩展名:<Input type=text name="extendsName">
<Input type=submit value="提交">
</FORM>
<jsp:setProperty name="file" property="extendsName" param="extendsName"/>
<P>扩展名是 <jsp:getProperty name="file" property="extendsName"/> 的文件有:
<BR><jsp:getProperty name="file" property="allFileName"/>
</BODY></HTML>
三角形bean
Triangle.java
package red.star;
public class Triangle
{ double sideA=-1,sideB=-1,sideC=-1, area=-1;
boolean triangle;
public void setSideA(double a)
{ sideA=a;
}
public double getSideA()
{ return sideA;
}
public void setSideB(double b)
{ sideB=b;
}
public double getSideB()
{ return sideB;
}
public void setSideC(double c)
{ sideC=c;
}
public double getSideC()
{ return sideC;
}
public double getArea()
{ double p=(sideA+sideB+sideC)/2.0;
if(triangle)
area=Math.sqrt(p*(p-sideA)*(p-sideB)*(p-sideC));
return area;
}
public boolean isTriangle()
{ if(sideA<sideB+sideC&&sideB<sideA+sideC&&sideC<sideA+sideB)
triangle=true;
else triangle=false;
return triangle;
}
}
triangle.jsp
<%@ page contentType="text/html;Charset=GB2312" %>
<%@ page import="red.star.Triangle"%>
<jsp:useBean id="tri" class="red.star.Triangle" scope="page"/>
<HTML><BODY bgcolor=yellow><Font size=3>
<FORM action="" Method="post" >
输入三角形三边:
边A:<Input type=text name="sideA" value=0 size=5>
边B:<Input type=text name="sideB" value=0 size=5>
边C:<Input type=text name="sideC" value=0 size=5>
<Input type=submit value="提交">
</FORM>
<jsp:setProperty name="tri" property="*"/>
三角形的三边是:
边A:<jsp:getProperty name="tri" property="sideA"/>,
边B:<jsp:getProperty name="tri" property="sideB"/>,
边C:<jsp:getProperty name="tri" property="sideC"/>.
<BR>这三个边能构成一个三角形吗?<jsp:getProperty name="tri" property="triangle"/>
<BR>面积是: <jsp:getProperty name="tri" property="area"/>
</FONT></BODY></HTML>
四则运算bean
ComputerBean.java
package red.star;
public class ComputerBean
{ double numberOne,numberTwo,result;
String operator="+";
public void setNumberOne(double n)
{ numberOne=n;
}
public double getNumberOne()
{ return numberOne;
}
public void setNumberTwo(double n)
{ numberTwo=n;
}
public double getNumberTwo()
{ return numberTwo;
}
public void setOperator(String s)
{ operator=s.trim();;
}
public String getOperator()
{ return operator;
}
public double getResult()
{ if(operator.equals("+"))
{ result=numberOne+numberTwo;
}
else if(operator.equals("-"))
{ result=numberOne-numberTwo;
}
else if(operator.equals("*"))
{ result=numberOne*numberTwo;
}
else if(operator.equals("/"))
{ result=numberOne/numberTwo;
}
return result;
}
}
computer.jsp
<%@ page contentType="text/html;Charset=GB2312" %>
<%@ page import="red.star.*" %>
<HTML><BODY bgcolor=yellow><Font size=2>
<jsp:useBean id="computer" class="red.star.ComputerBean" scope="session"/>
<jsp:setProperty name="computer" property="*"/>
<FORM action="" method=post name=form>
<Input type=text name="numberOne"
value=<jsp:getProperty name="computer" property="numberOne"/> size=6>
<Select name="operator">
<Option value="+">+
<Option value="-">-
<Option value="*">*
<Option value="/">/
</Select>
<Input type=text name="numberTwo"
value=<jsp:getProperty name="computer" property="numberTwo"/> size=6>
=<jsp:getProperty name="computer" property="result"/>
<BR> <INPUT TYPE="submit" value="提交你的选择" name="submit">
</FORM>
</BODY></HTML>
计数器bean
CounterCount.java
package red.star;
import java.io.*;
public class ComputerCount
{ int number=0;
boolean isCome=false;
File file=new File("count.dat") ;
private void countPeople()
{ if(!file.exists())
{ number++;
try { file.createNewFile();
FileOutputStream out=new FileOutputStream(file);
DataOutputStream dataOut=new DataOutputStream(out);
dataOut.writeInt(number);
out.close();
dataOut.close();
}
catch(IOException ee){}
}
else
{ try{ FileInputStream in=new FileInputStream(file);
DataInputStream dataIn=new DataInputStream(in);
number=dataIn.readInt();
number++;
in.close();
dataIn.close();
FileOutputStream out=new FileOutputStream(file);
DataOutputStream dataOut=new DataOutputStream(out);
dataOut.writeInt(number);
out.close();
dataOut.close();
}
catch(IOException ee){}
}
isCome=true;
}
public int getNumber()
{ if(isCome==false)
countPeople();
return number;
}
}
comeOne.jsp
<%@ page contentType="text/html;Charset=GB2312" %>
<%@ page import="red.star.*" %>
<jsp:useBean id="count" class="red.star.ComputerCount" scope="session"/>
<HTML><BODY bgcolor=yellow><Font size=3>
<P>Welcome欢迎您访问本站,这是本网站的comeOne.jsp页面
<BR>您是第
<jsp:getProperty name="count" property="number"/>
个访问本网站的客户。
<A href="comeTwo.jsp">欢迎去comeTwo.jsp参观</A>
</BODY></HTML>
comeTwo.jsp
<%@ page contentType="text/html;Charset=GB2312" %>
<%@ page import="red.star.*" %>
<jsp:useBean id="count" class="red.star.ComputerCount" scope="session"/>
<HTML><BODY bgcolor=cyan><Font size=3>
<P>Welcome欢迎您访问本站,这是本网站的comeTwo.jsp页面
<BR>您是第
<jsp:getProperty name="count" property="number"/>
个访问本网站的客户。
<A href="comeOne.jsp">欢迎去comeOne.jsp参观</A>
</BODY></HTML>
浏览图像 bean
Play.java
package red.star;
import java.io.*;
class FileName implements FilenameFilter
{ public boolean accept(File dir,String name)
{ boolean boo=false;
if(name.endsWith(".jpg")||name.endsWith(".JPG"))
boo=true;
return boo;
}
}
public class Play
{ int imageNumber=0,max;
String pictureName[],playImage;
public Play()
{ File dir=new File("D:/apache-tomcat-5.5.20/webapps/ch4/image");
pictureName=dir.list(new FileName());
max=pictureName.length;
}
public void setImageNumber(int n)
{ if(n<0)
n=max-1;
if(n==max)
n=0;
imageNumber=n;
}
public int getImageNumber()
{ return imageNumber;
}
public String getPlayImage()
{ playImage=new String("<img src=image/"+pictureName[imageNumber]+" "+" width=200
height=200></img>");
return playImage;
}
}
play.jsp
<%@ page contentType="text/html;Charset=GB2312" %>
<%@ page import="red.star.*" %>
<jsp:useBean id="play" class="red.star.Play" scope="session" />
<jsp:setProperty name="play" property="imageNumber" param="imageNumber"/>
<HTML><BODY bgcolor=cyan><Font size=2>
单击"上一张"或"下一张"按钮浏览图像
<Table ><FORM action="" method=post>
<tr>
<td><Input type=submit name="ok" value="上一张"></td>
<Input type="hidden" name="imageNumber" value="<%=play.getImageNumber()-1%>">
</FORM>
<FORM action="" method=post>
<td><Input type=submit name="ok" value="下一张"></td>
<Input type="hidden" name="imageNumber" value="<%=play.getImageNumber()+1 %>">
</tr>
</FORM>
</Table>
<jsp:getProperty name="play" property="playImage"/>
</Font></BODY></HTML>
成绩单 bean
ScoreList.java
package red.star;
public class ScoreList
{ String studentName,courseName="",score="";
StringBuffer scoreWatch=new StringBuffer();
public void setScore(String n)
{ score=n;
}
public void setStudentName(String s)
{ studentName=s;
}
public void setCourseName(String s)
{ courseName=s;
}
public String getCourseName()
{ return courseName;
}
public StringBuffer getScoreWatch()
{ if(studentName!=null&&studentName.length()>0&&score.length()>0)
{ scoreWatch.append("<tr>");
scoreWatch.append("<td width=150 Align=Center>");
scoreWatch.append(studentName);
scoreWatch.append("</td>");
scoreWatch.append("<td width=60 Align=Center>");
scoreWatch.append(""+score);
scoreWatch.append("</td>");
scoreWatch.append("</tr>");
studentName="";
score="";
}
return scoreWatch;
}
}
studentScore.jsp
<%@ page contentType="text/html;Charset=GB2312" %>
<%@ page import="red.star.*" %>
<jsp:useBean id="list" class="red.star.ScoreList" scope="session" />
<jsp:setProperty name="list" property="courseName" param="courseName"/>
<jsp:setProperty name="list" property="studentName" param="studentName"/>
<jsp:setProperty name="list" property="score" param="score"/>
<HTML><BODY bgcolor=cyan><Font size=2>
<FORM action="" method=post>
课程名称:<Input type=text name="courseName"
value=<jsp:getProperty name="list" property="courseName"/> >
<BR>学生姓名:<Input type=text name="studentName" >
<BR>学生成绩:<Input type=text name="score" >
<Input type=submit name="ok" value="提交">
</FORM>
<h2><jsp:getProperty name="list" property="courseName"/></h2>
<Table border=2>
<jsp:getProperty name="list" property="scoreWatch"/>
</Table>
</Font></BODY></HTML>
日历bean
CalendarBean.java
package red.star;
import java.util.*;
public class CalendarBean
{ String calendar=null;
int year=-1,month=-1;
public void setYear(int year)
{ this.year=year;
}
public int getYear()
{ return year;
}
public void setMonth(int month)
{ this.month=month;
}
public int getMonth()
{ return month;
}
public String getCalendar()
{ StringBuffer buffer=new StringBuffer();
Calendar rili=Calendar.getInstance();
rili.set(year,month-1,1); //将日历翻到year年month月1日,注意0表示一月,
//依次类推,11表示12月。
//获取1日是星期几(get方法返回的值是1表示星期日,返回的值是7表示星期六):
int 星期几=rili.get(Calendar.DAY_OF_WEEK)-1;
int day=0;
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
{ day=31;
}
if(month==4||month==6||month==9||month==11)
{ day=30;
}
if(month==2)
{ if(((year%4==0)&&(year%100!=0))||(year%400==0))
{ day=29;
}
else
{ day=28;
}
}
String a[]=new String[42];
for(int i=0;i<星期几;i++)
{ a[i]="**";
}
for(int i=星期几,n=1;i<星期几+day;i++)
{ a[i]=String.valueOf(n) ;
n++;
}
for(int i=星期几+day,n=1;i<42;i++)
{ a[i]="**" ;
}
//用表格显示数组:
buffer.append("<table border=1>");
buffer.append("<tr>") ;
String weekday[]={"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
for(int k=0;k<7;k++)
{ buffer.append("<td>"+weekday[k]+"</td>");
}
buffer.append("</tr>") ;
for(int k=0;k<42;k=k+7)
{ buffer.append("<tr>") ;
for(int j=k;j<Math.min(7+k,42);j++)
{ buffer.append("<td>"+a[j]+"</td>");
}
buffer.append("</tr>") ;
}
buffer.append("</table");
calendar=new String(buffer);
return calendar;
}
}
showCalendar.jsp
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="red.star.CalendarBean" %>
<HTML><BODY bgcolor=cyan><Font size=2>
<jsp:useBean id="rili" class="red.star.CalendarBean" scope="request"/>
<jsp:setProperty name="rili" property="*"/>
<FORM action="" method=post name=form>
输入日历的年份:<INPUT TYPE="text" value="2008" name="year" size=5>
选择日历的月份:
<Select name="month">
<Option value="1">1月 <Option value="2">2月
<Option value="3">3月 <Option value="4">4月
<Option value="5">5月 <Option value="6">6月
<Option value="7">7月 <Option value="8">8月
<Option value="9">9月 <Option value="10">10月
<Option value="11">11月 <Option value="12">12月
</Select>
<BR><INPUT TYPE="submit" value="提交" name="submit">
</FORM>
<font color="blue"><jsp:getProperty name="rili" property="year"/></font>年
<font color="green"><jsp:getProperty name="rili" property="month"/></font>月的日历:
<jsp:getProperty name= "rili" property="calendar" />
</Font></BODY></HTML>
2.编写bean
1.如果类的成员变量的名字是xxx,那么为了获取或更改成员变 量的值,即获取或更改属性,类中必须提供两个方法:
getXxx() 用来获取属性xxx
setXxx() 用来修改属性xxx
即方法的名字用get或set为前缀,后缀是将成员变量名字的首字母大写的字符序列。
2.对于boolean类型的成员变量,即布尔逻辑类型的属性,允许使用“is”代替上面的“get”和“set”。
3.类中声明的方法的访问属性都必须是public的。
4.类中声明的构造方法必须是public、无参数的。
3. Bean的保存
在当前Web服务目录下建立如下目录结构:
Web服务目录\WEB-INF\classes
根据类的包名,在目录classes下建立相应的子目录.比如:
Web服务目录\WEB-INF\classes\tom\jiafei
4. JSP页面中使用bean
使用JSP动作标记:useBean。useBean标记的格式:
<jsp:useBean id= "给bean起的名字" class= "创建bean的类" scope= "bean有效范围">
</jsp:useBean>
或
<jsp:useBean id= "给bean起的名字" class= "创建bean的类" scope= "bean有效范围"/>
5.动作标签getProperty
使用该标记可以获得bean的属性值 .
格式如下:
<jsp:getProperty name= "bean的名字" property= "bean的属性" />
或
<jsp:getProperty name= "bean的名字" property= "bean的属性"/>
</jsp:getProperty>
6.动作标记setProperty
(1)使用该标记可以设置bean的属性值
(2)设置bean的属性为一个表达式的值的格式如下:
<jsp:setProperty name="bean的名字" property= "bean的属性"
value= "<%=expression%>" />
(3)通过HTTP表单的参数的值来设置bean的相应属性的值:
<jsp:setProperty name="bean的名字" property="*" />
(4)通过request的参数的值来设置bean的相应属性的值,要求request参数名字必须与bean属性的名字相同其格式如下:
<jsp:setProperty name= "bean的名字" property="属性名" param= "参数名" />
7. bean的辅助类
有时在写一个bean的时候,可能还需要自己编写的其他类,那么只要将这些类和创建bean的类写在一个Java源中即可,但必须按将源文件编译后产生的全部字节码文件复制到相应的目录中 .
8.JSP与bean结合的简单例子
结合一些具体的实际问题,进一步熟悉掌握bean的使用。在本节中,创建bean的类的包名都是red.star,使用的Web服务目录仍然是ch4,因此,需要在ch4下建立如下的目录结构:
ch4\WEB-INF\classes\red\star
创建bean的字节码文件都需要保存在上述目录中。为了便于调试类文件,可将创建bean的Java源文件保存到D:\2000中,然后将编译得到的字节码文件复制到上述目录中。
Circle.java
package tom.jiafei
import java.io.*;
public class Circle
{ int radius;
public Circle()
{ radius=1;
}
public int getRadius()
{ return radius;
}
public void setRadius(int newRadius)
{radius=newRadius;
}
public double circleArea()
{return Math.PI*radius*radius;
}
public double circlLength()
{return 2.0*Math.PI*radius;
}
}
例子1
useBean.jsp
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="tom.jiafei.Circle"%>
<HTML><BODY bgcolor=cyan><Font size=3>
<jsp:useBean id="circle" class="tom.jiafei.Circle" scope="page" />
<%--通过上述JSP标记,客户获得了一个作用域是page,名字是circle的bean --%>
<% circle.setRadius(888);
%>
<P>圆的半径是:<%=circle.getRadius()%>
<P>圆的周长是:<%=circle.circlLength()%>
<P>圆的面积是:<%=circle.circleArea()%>
</BODY></HTML>
例子2
bean1.jsp
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="tom.jiafei.Circle"%>
<HTML><BODY bgcolor=cyan><Font size=3>
<jsp:useBean id="girl" class="tom.jiafei.Circle" scope="session" />
<P>圆的半径是: <%=girl.getRadius()%>
<A href="bean2.jsp"><BR>bean2.jsp </A>
</BODY></HTML>
bean2.jsp
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="tom.jiafei.Circle"%>
<HTML><BODY bgcolor=cyan><Font size=3>
<jsp:useBean id="girl" class="tom.jiafei.Circle" scope="session" />
<P>圆的半径是: <%=girl.getRadius()%>
<%girl.setRadius(400);%>
<P>修改后的圆的半径是:<%=girl.getRadius()%>
<A href="bean1.jsp"><BR>bean1.jsp </A>
</BODY></HTML>
例子3
appbean.jsp
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="tom.jiafei.*"%>
<HTML> <BODY>
<jsp:useBean id="girl" class="tom.jiafei.Circle" scope="application" >
</jsp:useBean>
<P>圆的初始半径是:<%=girl.getRadius()%>
<%girl.setRadius(1000);%>
<P>修改后的圆的半径是:<%=girl.getRadius()%>
</BODY></HTML>
NewCircle.java
package tom.jiafei;
import java.io.*;
public class NewCircle
{ double radius=1,circleArea,circleLength;
public double getRadius()
{ return radius;
}
public void setRadius(double newRadius)
{ radius=newRadius;
}
public double getCircleArea()
{ circleArea=Math.PI*radius*radius;
return circleArea;
}
public double getCircleLength()
{ circleLength=2.0*Math.PI*radius;
return circleLength;
}
}
例子4
beanGetProperty.jsp
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="tom.jiafei.NewCircle"%>
<HTML><BODY bgcolor=cyan><Font size=3>
<jsp:useBean id="apple" class="tom.jiafei.NewCircle" scope="request"/>
<% apple.setRadius(100);%>
圆的半径是:<jsp:getProperty name="apple" property= "radius"/>
<BR>圆的面积是:<jsp:getProperty name="apple" property= "circleArea"/>
<BR>圆的周长是:<jsp:getProperty name="apple" property= "circleLength"/>
</BODY></HTML>
Student.java
public class Student
{ String name=null;
long number;
double height,weight;
public String getName()
{ return name;
}
public void setName(String newName)
{name=newName;
}
public long getNumber()
{return number;
}
public void setNumber(long newNumber)
{ number=newNumber;
}
public double getHeight()
{return height;
}
public void setHeight(double newHeight)
{ height=newHeight;
}
public double getWeight()
{return weight;
}
public void setWeight(double newWeight)
{ weight=newWeight;
}
}
例子5
student.jsp
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="tom.jiafei.Student"%>
<HTML><BODY bgcolor=cyan><Font size=3>
<jsp:useBean id="zhang" class="tom.jiafei.Student" scope="page"/>
<jsp:setProperty name="zhang" property="name" value="张三"/>
名字是:<jsp:getProperty name="zhang" property="name"/>
<jsp:setProperty name="zhang" property="number" value="2008001"/>
<BR>学号是:<jsp:getProperty name="zhang" property="number"/>
<jsp:setProperty name="zhang" property="height" value="<%=1.78%>"/>
<BR>身高是:<jsp:getProperty name="zhang" property="height"/> 米
<jsp:setProperty name="zhang" property="weight" value="67.65"/>
<BR>体重是:<jsp:getProperty name="zhang" property="weight"/> 公斤
</FONT></BODY></HTML>
Goods.java
package tom.jiafei;
public class Goods
{ String name,goodsNumber;
double price;
public String getName()
{ return name;
}
public void setName(String newName)
{ name=newName;
}
public String getGoodsNumber()
{ return goodsNumber;
}
public void setGoodsNumber(String s)
{ goodsNumber=s;
}
public double getPrice()
{ return price;
}
public void setPrice(double newPrice)
{ price=newPrice;
}
}
例子6
goods.jsp
<%@ page contentType="text/html;Charset=GB2312" %>
<%@ page import="tom.jiafei.*"%>
<jsp:useBean id="goods" class="tom.jiafei.Goods" scope="page"/>
<HTML><BODY bgcolor=yellow><Font size=3>
<FORM action="" Method="post" >
输入商品的名称:<Input type=text name="name">
<BR>输入商品的代号:<Input type=text name="goodsNumber">
<BR>输入商品的价格: <Input type=text name="price">
<Input type=submit value="提交">
</FORM>
<P>提交表单才能设置商品的信息
<jsp:setProperty name="goods" property="*"/>
<BR>商品的名称:<jsp:getProperty name="goods" property="name"/>
<BR>商品的代号:<jsp:getProperty name="goods" property="goodsNumber"/>
<BR>商品的价格:<jsp:getProperty name="goods" property="price"/>
</FONT></BODY></HTML>
ListFile.java
package tom.jiafei;
import java.io.*;
class FileName implements FilenameFilter
{ String str=null;
FileName (String s)
{ str="."+s;
}
public boolean accept(File dir,String name)
{ return name.endsWith(str);
}
}
public class ListFile
{ String extendsName=null;
StringBuffer allFileName;
public void setExtendsName(String s)
{ extendsName=s;
}
public String getExtendsName()
{ return extendsName;
}
public StringBuffer getFileName()
{ File dir=new File("d:/2000");
FileName help=new FileName(extendsName);
String file_name[]=dir.list(help);
for(int i=0;i<file_name.length;i++)
{ allFileName.append(file_name[i]+" ");
}
return allFileName;
}
}
例子7
file.jsp
<%@ page contentType="text/html;Charset=GB2312" %>
<%@ page import="tom.jiafei.*"%>
<jsp:useBean id="file" class="tom.jiafei.ListFile" scope="page"/>
<HTML><BODY bgcolor=cyan><Font size=2>
<FORM action="" Method="post">
输入文件的扩展名:<Input type=text name="extendsName">
<Input type=submit value="提交">
</FORM>
<jsp:setProperty name="file" property="extendsName" param="extendsName"/>
<P>扩展名是 <jsp:getProperty name="file" property="extendsName"/> 的文件有:
<BR><jsp:getProperty name="file" property="allFileName"/>
</BODY></HTML>
三角形bean
Triangle.java
package red.star;
public class Triangle
{ double sideA=-1,sideB=-1,sideC=-1, area=-1;
boolean triangle;
public void setSideA(double a)
{ sideA=a;
}
public double getSideA()
{ return sideA;
}
public void setSideB(double b)
{ sideB=b;
}
public double getSideB()
{ return sideB;
}
public void setSideC(double c)
{ sideC=c;
}
public double getSideC()
{ return sideC;
}
public double getArea()
{ double p=(sideA+sideB+sideC)/2.0;
if(triangle)
area=Math.sqrt(p*(p-sideA)*(p-sideB)*(p-sideC));
return area;
}
public boolean isTriangle()
{ if(sideA<sideB+sideC&&sideB<sideA+sideC&&sideC<sideA+sideB)
triangle=true;
else triangle=false;
return triangle;
}
}
triangle.jsp
<%@ page contentType="text/html;Charset=GB2312" %>
<%@ page import="red.star.Triangle"%>
<jsp:useBean id="tri" class="red.star.Triangle" scope="page"/>
<HTML><BODY bgcolor=yellow><Font size=3>
<FORM action="" Method="post" >
输入三角形三边:
边A:<Input type=text name="sideA" value=0 size=5>
边B:<Input type=text name="sideB" value=0 size=5>
边C:<Input type=text name="sideC" value=0 size=5>
<Input type=submit value="提交">
</FORM>
<jsp:setProperty name="tri" property="*"/>
三角形的三边是:
边A:<jsp:getProperty name="tri" property="sideA"/>,
边B:<jsp:getProperty name="tri" property="sideB"/>,
边C:<jsp:getProperty name="tri" property="sideC"/>.
<BR>这三个边能构成一个三角形吗?<jsp:getProperty name="tri" property="triangle"/>
<BR>面积是: <jsp:getProperty name="tri" property="area"/>
</FONT></BODY></HTML>
四则运算bean
ComputerBean.java
package red.star;
public class ComputerBean
{ double numberOne,numberTwo,result;
String operator="+";
public void setNumberOne(double n)
{ numberOne=n;
}
public double getNumberOne()
{ return numberOne;
}
public void setNumberTwo(double n)
{ numberTwo=n;
}
public double getNumberTwo()
{ return numberTwo;
}
public void setOperator(String s)
{ operator=s.trim();;
}
public String getOperator()
{ return operator;
}
public double getResult()
{ if(operator.equals("+"))
{ result=numberOne+numberTwo;
}
else if(operator.equals("-"))
{ result=numberOne-numberTwo;
}
else if(operator.equals("*"))
{ result=numberOne*numberTwo;
}
else if(operator.equals("/"))
{ result=numberOne/numberTwo;
}
return result;
}
}
computer.jsp
<%@ page contentType="text/html;Charset=GB2312" %>
<%@ page import="red.star.*" %>
<HTML><BODY bgcolor=yellow><Font size=2>
<jsp:useBean id="computer" class="red.star.ComputerBean" scope="session"/>
<jsp:setProperty name="computer" property="*"/>
<FORM action="" method=post name=form>
<Input type=text name="numberOne"
value=<jsp:getProperty name="computer" property="numberOne"/> size=6>
<Select name="operator">
<Option value="+">+
<Option value="-">-
<Option value="*">*
<Option value="/">/
</Select>
<Input type=text name="numberTwo"
value=<jsp:getProperty name="computer" property="numberTwo"/> size=6>
=<jsp:getProperty name="computer" property="result"/>
<BR> <INPUT TYPE="submit" value="提交你的选择" name="submit">
</FORM>
</BODY></HTML>
计数器bean
CounterCount.java
package red.star;
import java.io.*;
public class ComputerCount
{ int number=0;
boolean isCome=false;
File file=new File("count.dat") ;
private void countPeople()
{ if(!file.exists())
{ number++;
try { file.createNewFile();
FileOutputStream out=new FileOutputStream(file);
DataOutputStream dataOut=new DataOutputStream(out);
dataOut.writeInt(number);
out.close();
dataOut.close();
}
catch(IOException ee){}
}
else
{ try{ FileInputStream in=new FileInputStream(file);
DataInputStream dataIn=new DataInputStream(in);
number=dataIn.readInt();
number++;
in.close();
dataIn.close();
FileOutputStream out=new FileOutputStream(file);
DataOutputStream dataOut=new DataOutputStream(out);
dataOut.writeInt(number);
out.close();
dataOut.close();
}
catch(IOException ee){}
}
isCome=true;
}
public int getNumber()
{ if(isCome==false)
countPeople();
return number;
}
}
comeOne.jsp
<%@ page contentType="text/html;Charset=GB2312" %>
<%@ page import="red.star.*" %>
<jsp:useBean id="count" class="red.star.ComputerCount" scope="session"/>
<HTML><BODY bgcolor=yellow><Font size=3>
<P>Welcome欢迎您访问本站,这是本网站的comeOne.jsp页面
<BR>您是第
<jsp:getProperty name="count" property="number"/>
个访问本网站的客户。
<A href="comeTwo.jsp">欢迎去comeTwo.jsp参观</A>
</BODY></HTML>
comeTwo.jsp
<%@ page contentType="text/html;Charset=GB2312" %>
<%@ page import="red.star.*" %>
<jsp:useBean id="count" class="red.star.ComputerCount" scope="session"/>
<HTML><BODY bgcolor=cyan><Font size=3>
<P>Welcome欢迎您访问本站,这是本网站的comeTwo.jsp页面
<BR>您是第
<jsp:getProperty name="count" property="number"/>
个访问本网站的客户。
<A href="comeOne.jsp">欢迎去comeOne.jsp参观</A>
</BODY></HTML>
浏览图像 bean
Play.java
package red.star;
import java.io.*;
class FileName implements FilenameFilter
{ public boolean accept(File dir,String name)
{ boolean boo=false;
if(name.endsWith(".jpg")||name.endsWith(".JPG"))
boo=true;
return boo;
}
}
public class Play
{ int imageNumber=0,max;
String pictureName[],playImage;
public Play()
{ File dir=new File("D:/apache-tomcat-5.5.20/webapps/ch4/image");
pictureName=dir.list(new FileName());
max=pictureName.length;
}
public void setImageNumber(int n)
{ if(n<0)
n=max-1;
if(n==max)
n=0;
imageNumber=n;
}
public int getImageNumber()
{ return imageNumber;
}
public String getPlayImage()
{ playImage=new String("<img src=image/"+pictureName[imageNumber]+" "+" width=200
height=200></img>");
return playImage;
}
}
play.jsp
<%@ page contentType="text/html;Charset=GB2312" %>
<%@ page import="red.star.*" %>
<jsp:useBean id="play" class="red.star.Play" scope="session" />
<jsp:setProperty name="play" property="imageNumber" param="imageNumber"/>
<HTML><BODY bgcolor=cyan><Font size=2>
单击"上一张"或"下一张"按钮浏览图像
<Table ><FORM action="" method=post>
<tr>
<td><Input type=submit name="ok" value="上一张"></td>
<Input type="hidden" name="imageNumber" value="<%=play.getImageNumber()-1%>">
</FORM>
<FORM action="" method=post>
<td><Input type=submit name="ok" value="下一张"></td>
<Input type="hidden" name="imageNumber" value="<%=play.getImageNumber()+1 %>">
</tr>
</FORM>
</Table>
<jsp:getProperty name="play" property="playImage"/>
</Font></BODY></HTML>
成绩单 bean
ScoreList.java
package red.star;
public class ScoreList
{ String studentName,courseName="",score="";
StringBuffer scoreWatch=new StringBuffer();
public void setScore(String n)
{ score=n;
}
public void setStudentName(String s)
{ studentName=s;
}
public void setCourseName(String s)
{ courseName=s;
}
public String getCourseName()
{ return courseName;
}
public StringBuffer getScoreWatch()
{ if(studentName!=null&&studentName.length()>0&&score.length()>0)
{ scoreWatch.append("<tr>");
scoreWatch.append("<td width=150 Align=Center>");
scoreWatch.append(studentName);
scoreWatch.append("</td>");
scoreWatch.append("<td width=60 Align=Center>");
scoreWatch.append(""+score);
scoreWatch.append("</td>");
scoreWatch.append("</tr>");
studentName="";
score="";
}
return scoreWatch;
}
}
studentScore.jsp
<%@ page contentType="text/html;Charset=GB2312" %>
<%@ page import="red.star.*" %>
<jsp:useBean id="list" class="red.star.ScoreList" scope="session" />
<jsp:setProperty name="list" property="courseName" param="courseName"/>
<jsp:setProperty name="list" property="studentName" param="studentName"/>
<jsp:setProperty name="list" property="score" param="score"/>
<HTML><BODY bgcolor=cyan><Font size=2>
<FORM action="" method=post>
课程名称:<Input type=text name="courseName"
value=<jsp:getProperty name="list" property="courseName"/> >
<BR>学生姓名:<Input type=text name="studentName" >
<BR>学生成绩:<Input type=text name="score" >
<Input type=submit name="ok" value="提交">
</FORM>
<h2><jsp:getProperty name="list" property="courseName"/></h2>
<Table border=2>
<jsp:getProperty name="list" property="scoreWatch"/>
</Table>
</Font></BODY></HTML>
日历bean
CalendarBean.java
package red.star;
import java.util.*;
public class CalendarBean
{ String calendar=null;
int year=-1,month=-1;
public void setYear(int year)
{ this.year=year;
}
public int getYear()
{ return year;
}
public void setMonth(int month)
{ this.month=month;
}
public int getMonth()
{ return month;
}
public String getCalendar()
{ StringBuffer buffer=new StringBuffer();
Calendar rili=Calendar.getInstance();
rili.set(year,month-1,1); //将日历翻到year年month月1日,注意0表示一月,
//依次类推,11表示12月。
//获取1日是星期几(get方法返回的值是1表示星期日,返回的值是7表示星期六):
int 星期几=rili.get(Calendar.DAY_OF_WEEK)-1;
int day=0;
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
{ day=31;
}
if(month==4||month==6||month==9||month==11)
{ day=30;
}
if(month==2)
{ if(((year%4==0)&&(year%100!=0))||(year%400==0))
{ day=29;
}
else
{ day=28;
}
}
String a[]=new String[42];
for(int i=0;i<星期几;i++)
{ a[i]="**";
}
for(int i=星期几,n=1;i<星期几+day;i++)
{ a[i]=String.valueOf(n) ;
n++;
}
for(int i=星期几+day,n=1;i<42;i++)
{ a[i]="**" ;
}
//用表格显示数组:
buffer.append("<table border=1>");
buffer.append("<tr>") ;
String weekday[]={"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
for(int k=0;k<7;k++)
{ buffer.append("<td>"+weekday[k]+"</td>");
}
buffer.append("</tr>") ;
for(int k=0;k<42;k=k+7)
{ buffer.append("<tr>") ;
for(int j=k;j<Math.min(7+k,42);j++)
{ buffer.append("<td>"+a[j]+"</td>");
}
buffer.append("</tr>") ;
}
buffer.append("</table");
calendar=new String(buffer);
return calendar;
}
}
showCalendar.jsp
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="red.star.CalendarBean" %>
<HTML><BODY bgcolor=cyan><Font size=2>
<jsp:useBean id="rili" class="red.star.CalendarBean" scope="request"/>
<jsp:setProperty name="rili" property="*"/>
<FORM action="" method=post name=form>
输入日历的年份:<INPUT TYPE="text" value="2008" name="year" size=5>
选择日历的月份:
<Select name="month">
<Option value="1">1月 <Option value="2">2月
<Option value="3">3月 <Option value="4">4月
<Option value="5">5月 <Option value="6">6月
<Option value="7">7月 <Option value="8">8月
<Option value="9">9月 <Option value="10">10月
<Option value="11">11月 <Option value="12">12月
</Select>
<BR><INPUT TYPE="submit" value="提交" name="submit">
</FORM>
<font color="blue"><jsp:getProperty name="rili" property="year"/></font>年
<font color="green"><jsp:getProperty name="rili" property="month"/></font>月的日历:
<jsp:getProperty name= "rili" property="calendar" />
</Font></BODY></HTML>