1.useBean标签的使用

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>jsp:useBean标签的使用</title>
</head>

<body>

<!-- 如果没有实例化person对象,那么就将id存入scope对应的对象中,下次访问时从对象中取出相应的实例
如果实例化了,就取出了person实例(其实都详单与new一个person类)-->
<jsp:useBean id="person" class="cn.edu.domain.Person" scope="session">
<h4>创建了一个person对象!!!</h4> <!-- 如果是session,这句话只出现一次,就是创建session那一次 -->
</jsp:useBean>

<%=person.getName() %>
</body>
</html>


2.setProperty标签的使用

名为Person的JavaBean:

package cn.edu.domain;

import java.util.Date;


public class Person {
private String name="aaa";
private int age;
private Date brithday;
private Address address;

public Person(){}

public Person(String name) {
this.name = name;
}

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Date getBrithday() {
return brithday;
}
public void setBrithday(Date brithday) {
this.brithday = brithday;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}



}



使用bean的jsp页面


<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>jsp:setProperty标签的使用</title>
</head>

<body>

<jsp:useBean id="person" class="cn.edu.domain.Person" scope="page"/>

<!-- 手工为bean赋值 -->
<jsp:setProperty property="name" name="person" value="XXXXX"/>
<%=person.getName() %><br/>

<!-- 请求参数为bean的属性赋值 (URL路径带的值,如http://localhost:8080/day09/Jsp2.jsp?name=jack&&age=12)-->
<jsp:setProperty property="name" name="person" param="name"/>
<!-- 支持8中基础数据类型的转换,把客户机提交的字符串,转换成相应的8中基础类型,赋到bean的属性中 -->
<jsp:setProperty property="age" name="person" param="age"/>
<jsp:setProperty property="brithday" name="person" value="<%=new Date() %>"/>

<%=person.getName() %><br/>
<%=person.getAge() %><br/>
<%=person.getBrithday()%><br/>

<hr/>
<!-- 用所有的请求参数为bean赋值 -->
<jsp:setProperty property="*" name="person"/>

<%=person.getName() %><br/>
<%=person.getAge() %><br/>


<hr/>
<!-- 用getproperty标签取bean的值 -->
<jsp:getProperty property="name" name="person"/><br/>
<jsp:getProperty property="age" name="person"/><br/>
<jsp:getProperty property="brithday" name="person"/><br/>
</body>
</html>