圆的半径是:<%=circle.getRadius()%>
圆的周长是:<%=circle.circlLength()%>
圆的面积是:<%=circle.circleArea()%>
例子2
bean1.jsp
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="tom.jiafei.Circle"%>
圆的半径是: <%=girl.getRadius()%>
bean2.jsp
bean2.jsp
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="tom.jiafei.Circle"%>
圆的半径是: <%=girl.getRadius()%>
<%girl.setRadius(400);%>
修改后的圆的半径是:<%=girl.getRadius()%>
bean1.jsp
例子3
appbean.jsp
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="tom.jiafei.*"%>
圆的初始半径是:<%=girl.getRadius()%>
<%girl.setRadius(1000);%>
修改后的圆的半径是:<%=girl.getRadius()%>
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"%>
提交表单才能设置商品的信息
商品的名称:
商品的代号:
商品的价格:
扩展名是 Welcome欢迎您访问本站,这是本网站的comeOne.jsp页面
Welcome欢迎您访问本站,这是本网站的comeTwo.jsp页面
三角形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
else triangle=false;
return triangle;
}
}
triangle.jsp
<%@ page contentType="text/html;Charset=GB2312" %>
<%@ page import="red.star.Triangle"%>
三角形的三边是:
边A:
边B:
边C:
这三个边能构成一个三角形吗?
面积是:
四则运算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.*" %>
计数器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.*" %>
您是第
个访问本网站的客户。
欢迎去comeTwo.jsp参观
comeTwo.jsp
<%@ page contentType="text/html;Charset=GB2312" %>
<%@ page import="red.star.*" %>
您是第
个访问本网站的客户。
欢迎去comeOne.jsp参观
浏览图像 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("
height=200>");
return playImage;
}
}
play.jsp
<%@ page contentType="text/html;Charset=GB2312" %>
<%@ page import="red.star.*" %>
单击"上一张"或"下一张"按钮浏览图像
成绩单 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("");
");
scoreWatch.append("");
");
scoreWatch.append(studentName);
scoreWatch.append("
scoreWatch.append("");
");
scoreWatch.append(""+score);
scoreWatch.append("
scoreWatch.append("
studentName="";
score="";
}
return scoreWatch;
}
}
studentScore.jsp
<%@ page contentType="text/html;Charset=GB2312" %>
<%@ page import="red.star.*" %>
课程名称:
value=
学生姓名:
学生成绩:
日历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("");
calendar=new String(buffer);
buffer.append("") ;
") ;
String weekday[]={"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
for(int k=0;k<7;k++)
{ buffer.append(""+weekday[k]+" ");
}
buffer.append("
for(int k=0;k<42;k=k+7)
{ buffer.append("") ;
") ;
for(int j=k;j"+a[j]+" ");
}
buffer.append("
}
buffer.append("
return calendar;
}
}
showCalendar.jsp
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="red.star.CalendarBean" %>
输入日历的年份:
选择日历的月份: