目录
前言
本次讲述主要的内容为编写和使用javaBean、获取和修改bean的属性、bean的辅助类
其中获取和修改bean的属性为难点,需反复练习,融会贯通。
一、编写和使用javaBean
1.1编写javaBean
编写javaBean就是编写一个java的类。这个类创建的一个对象称为一个javaBean,简称bean,分配给bean的变量,称为bean的属性。为了能正确使用bean,类的命名应遵循以下规则:
①为了获取和修改bean的属性的值类中必须提供两个方法:
·getXxx(),用来获取属性xxx;
·setXxx(),用来修改属性xxx;
xxx为bean的属性名;
②类中定义的方法的访问权限必须都是public的。
③类中必须有一个构造方法是public,无参数的。
1.2保存bean的字节码文件
为了使JSP页面使用bean,Tomcat服务器必须使用相应的字节码文件创建一个对象。即创建一个bean、为了让Tomcat服务器能找到字节码文件,字节码文件必须保存在特定的目录中。
ch5/WEN-INF/classes目录下,根据包名对应的路径,在classes目录下在建立相应的子目录。
将bean的字节码文件复制到jiafei文件夹中。
1.3创建和使用bean
①使用bean
使用JSP动作标记useBean加载使用bean,语法格式是:
<jsp:useBean idd = "bean的名字"class="创建bean的类" scope = "bean有效范围"/>
或
<jsp:useBean idd = "bean的名字"class="创建bean的类" scope = "bean有效范围">
</jsp:useBean>
②bean的有效范围和生命周期
scope的取值给出了bean的生命周期:
1)page bean :有效范围是用户访问的当前页面,存活时间直到当前页面执行完毕。
不同用户的page bean是互不相同的。
2)session bean:有效范围是用户访问的Web服务目录下的各个页面,存活时间是用户的会话期间,直到用户的会话消失对象达到了最大生存时间或者用户关闭了自己的浏览器以及服务器。
不同用户的session bean是互不相同的
3)request bean:有效范围是用户访问的Web服务目录下的各个页面,存活时间是用户的会话期间,直到用户的会话消失对象达到了最大生存时间或者用户关闭了自己的浏览器以及服务器。
不同用户的request bean也是互不相同的
4)application bean:有效范围是当前Web服务目录下的各个页面,存活时间直到Tomcat服务器关闭。不同用户的application bean是相同的。
例5.1
Circle.java
package tom.jiafei;
public class Circle {
double radius;
public Circle() {
radius=1;
}
public double getRadius() {
return radius;
}
public void setRadius(double newRadius) {
radius=newRadius;
}
public double circleArea() {
return Math.PI*radius*radius;
}
public double circleLength() {
return 2.0*Math.PI*radius;
}
}
example5.1
<%@ page contentType="text/html" %>
<%@ page pageEncoding = "utf-8" %>
<style>
#textStyle{
font-family:宋体;font-size:36;color:blue
}
</style>
<HTML><body bgcolor = #ffccff>
<p id = "textStyle">
<jsp:useBean id="circle" class="tom.jiafei.Circle" scope="page" />
<%--通过useBean标记,获得有效期限是page,名字是circle的bean --%>
圆的初始半径是:<%=circle.getRadius()%>
<% double newRadius =100;
circle.setRadius(newRadius); //修改半径。
%>
<br>修改半径为<%= newRadius %>
<br><b>圆的半径是:<%=circle.getRadius()%>
<br><b>圆的周长是:<%=circle.circleLength()%>
<br><b>圆的面积是:<%=circle.circleArea()%>
</p></body></HTML>
运行结果:
二、获取和修改bean的属性
2.1 getProperty动作标记
使用getproperty动作标记可以获得bean的属性值,并将这个值用串的形式发送给用户的浏览器。使用getProperty动作标记之前,必须使用useBean动作标记获得相应的bean。
getProperty动作标记的语法格式是:
<jsp:getProperty name = "bean 的id" property="bean的属性"/>
或
<jsp:getProperty name = "bean 的id" property="bean的属性">
</jsp:getProperty>
2.2 setProperty动作标记
使用setPropertyd动作标记可以设置bean的属性值。使用这个标记之前,必须使用useBean标记得到一个相应的bean。
setProperty动作标记可以通过以下两种方式设置bean属性值
(1)将bean属性值设置为一个表达式的值或字符系列。
<jsp:setProperty name ="bean 的id" property="bean的属性" value = "<% = expression%>"/>
<jsp:setProperty name ="bean 的id" property="bean的属性" value = "字符系列"/>
(2)通过HTTP表单的参数值来设置bean的相应属性值。
①用form表单的所有参数值设置bean相应属性值。
<jsp:setProperty name = "bean 的id的名字"property=" * "/>
②用form表单的某个参数的值设置bean的某个属性值。
<jsp:setProperty name = "bean 的名字"property=" 属性名 " param="参数名" />
例5.4
Good.java
package tom.jiafei;
public class Goods {
String name="无名";
double price=0;
public String getName() {
return name;
}
public void setName(String newName){
name = newName;
}
public double getPrice() {
return price;
}
public void setPrice(double newPrice) {
price = newPrice;
}
}
example5.4
a.jsp
<%@ page contentType="text/html" %>
<%@ page pageEncoding = "utf-8" %>
<% request.setCharacterEncoding("utf-8");
%>
<jsp:useBean id="phone" class="tom.jiafei.Goods" scope="request"/>
<HTML><body bgcolor = #ffccff>
<style>
#textStyle{
font-family:宋体;font-size:20;color:blue
}
</style>
<p id = "textStyle">
<form action="" Method="post" >
手机名称:<input type=text id=textStyle name="name">
<br>手机价格:<input type=text id=textStyle name="price"/>
<br><input type=submit id=textStyle value="提交给本页面"/>
</form>
<form action="example5_4_b.jsp" Method="post" >
手机名称:<input type= text name="name" id=textStyle>
<br>手机价格:<input type=text name="price" id=textStyle>
<br><input type=submit id=textStyle value="提交给example5_4_b.jsp页面">
</form>
<jsp:setProperty name="phone" property="name" param ="name" />
<jsp:setProperty name="phone" property="price" param ="price"/>
<br><b>名称:<jsp:getProperty name="phone" property="name"/>
<br><b>名称:<%= phone.getName()%><br>
<br><b>价格:<jsp:getProperty name="phone" property="price"/>
<br><b>价格:<%=phone.getPrice()%>
</p></body></HTML>
b.jsp
<%@ page contentType="text/html" %>
<%@ page pageEncoding = "utf-8" %>
<% request.setCharacterEncoding("utf-8");
%>
<jsp:useBean id="phone" class="tom.jiafei.Goods" scope="page"/>
<HTML><body bgcolor= cyan>
<p style="font-family:黑体;font-size:20;color:red">
<jsp:setProperty name="phone" property="name" param ="name" />
<jsp:setProperty name="phone" property="price" param ="price"/>
<br><b>名称:<jsp:getProperty name="phone" property="name"/>
<br><b>名称:<%= phone.getName()%><br>
<br><b>价格:<jsp:getProperty name="phone" property="price"/>
<br><b>价格:<%=phone.getPrice()%>
</p></body></HTML>
运行结果:
三、bean的辅助类
在写一个创建bean的类时,除了需要用import语句引入JDK提供的类,可能还需要自己编写一些其他的类。只要将这些类的包名和bean类的包名一致即可(也可以和创建bean的类写在一个Java源文件中)。
例5.5
ListFile.java
package tom.jiafei;
import java.io.*;
class FileExtendName implements FilenameFilter {
String str=null;
FileExtendName (String s) {
str="."+s;
}
public boolean accept(File dir,String name) {
return name.endsWith(str);
}
}
public class ListFile {
String extendsName=null;
String [] allFileName=null;
String dir=null;
public void setDir(String dir) {
this.dir =dir;
}
public String getDir() {
return dir;
}
public void setExtendsName(String s) {
extendsName=s;
}
public String getExtendsName() {
return extendsName;
}
public String [] getAllFileName() {
if(dir!=null) {
File mulu=new File(dir);
FileExtendName help=new FileExtendName(extendsName);
allFileName=mulu.list(help);
}
return allFileName;
}
}
example5.5.jsp
<%@ page contentType="text/html" %>
<%@ page pageEncoding = "utf-8" %>
<% request.setCharacterEncoding("utf-8");
%>
<style>
#textStyle{
font-family:宋体;font-size:36;color:blue
}
</style>
<jsp:useBean id="file" class="tom.jiafei.ListFile" scope="request"/>
<HTML><body id=textStyle bgcolor = #ffccff>
<form action="" Method="post">
输入目录名(例如D:/2000)<input type=text name="dir" id=textStyle size=15/><br>
输入文件的扩展名(例如java)<input type=text name="extendsName" id=textStyle size=6>
<input type=submit id=textStyle value="提交"/>
</form>
<jsp:setProperty name="file" property="dir" param="dir"/>
<jsp:setProperty name="file" property="extendsName" param="extendsName"/>
<br><b>目录 <jsp:getProperty name="file" property="dir"/>中
扩展名是 <jsp:getProperty name="file" property="extendsName"/> 的文件有:
<% String [] fileName = file.getAllFileName();
if(fileName!=null) {
for(int i=0;i<fileName.length;i++) {
out.print("<br>"+fileName[i]);
}
}
%>
</body></HTML>
运行结果:
总结
以上就是jsp与javaBean的全部内容了,内容并不难,但依然要理解透彻。