1.Member.java
package day12;
public class Member {
private String name;
private String gender;
private Integer age;
private String email;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
2. javabean01.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page language="java" import="day12.Member"%>
<jsp:useBean id="mem" class="day12.Member" type="day12.Member" scope="page">
Body**************************
</jsp:useBean>
<!--
id:定javaBean实例对象的引用名称和其存储在域范围中的名称
class属性:指定javaBean的完整类名(包括包名)
type属性:指定javaBean实例对象的引用变量的类型,它必须是javaBean对象的类名称、超类名称和所实现的接口中
的一个,其默认值为class属性的设置值
scope属性:用于指定javaBean实例对象所存储的域范围,
其取值只能是page,request,session和application四个值中的一个,其默认值为page。
分析javabean创建过程:
1.javabean执行时,先在scope属性指定的作用域中查找以id名称的javaBean
若没有找到,根据class属性创建javaBean(Member)的实例
把创建的javaBean实例放置在scope指定的作用域中,使用id的值作为key的名称
若找到,直接使用。
Body部分的内容只在jsp:useBean 标签创建javaBean的实例对象时才执行
jsp:useBean id="mem" class="day12.Member" type="day12.Member" scope="page"这句话等价于:
Member mem=new Member();
-->
<%
mem.setName("西门庆");
mem.setGender("男");
mem.setAge(26);
mem.setEmail("kenni@163.com");
%>
姓名:<%=mem.getName() %><br/>
性别:<%=mem.getGender() %><br/>
年龄:<%=mem.getAge() %><br/
3.javabean02.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ page language="java" import="day12.Member"%> <jsp:useBean id="mem" class="day12.Member" type="day12.Member" scope="page"> Body************************** </jsp:useBean> <!-- id:定javaBean实例对象的引用名称和其存储在域范围中的名称 class属性:指定javaBean的完整类名(包括包名) type属性:指定javaBean实例对象的引用变量的类型,它必须是javaBean对象的类名称、超类名称和所实现的接口中 的一个,其默认值为class属性的设置值 scope属性:用于指定javaBean实例对象所存储的域范围, 其取值只能是page,request,session和application四个值中的一个,其默认值为page。 分析javabean创建过程: 1.javabean执行时,先在scope属性指定的作用域中查找以id名称的javaBean 若没有找到,根据class属性创建javaBean(Member)的实例 把创建的javaBean实例放置在scope指定的作用域中,使用id的值作为key的名称 若找到,直接使用。 Body部分的内容只在jsp:useBean 标签创建javaBean的实例对象时才执行 jsp:useBean id="mem" class="day12.Member" type="day12.Member" scope="page"这句话等价于: Member mem=new Member(); --> <% mem.setName("西门庆"); mem.setGender("男"); mem.setAge(26); mem.setEmail("kenni@163.com"); %> 姓名:<%=mem.getName() %><br/> 性别:<%=mem.getGender() %><br/> 年龄:<%=mem.getAge() %><br/>
4. javabean03.jsp<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ page language="java" import="day12.Member" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <jsp:useBean id="mem" class="day12.Member" type="day12.Member" scope="session"> Body**************************<br/> </jsp:useBean> <!-- 调用Set方法赋值property属性值是去掉set后面的值,与声明的属性无关 --> <jsp:setProperty property="name" name="mem" value="西门庆"/> <jsp:setProperty property="gender" name="mem" value="男"/> <jsp:setProperty property="age" name="mem" value="26"/> <jsp:setProperty property="email" name="mem" value="kenni@163.com"/> 姓名:<%=mem.getName() %><br/> 性别:<%=mem.getGender() %><br/> 年龄:<%=mem.getAge() %><br/>
5. javabean05.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ page contentType="text/html; charset=utf-8" %> <%@ page language="java" import="day12.Member" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <jsp:useBean id="mem" class="day12.Member" type="day12.Member" scope="page"> Body************************** </jsp:useBean> <jsp:setProperty property="name" name="mem" value='<%=request.getParameter("username") %>'/> <jsp:setProperty property="gender" name="mem" value='<%=request.getParameter("gender") %>'/> <jsp:setProperty property="age" name="mem" value='<%=Integer.parseInt(request.getParameter("age")) %>'/> <jsp:setProperty property="email" name="mem" value='<%=request.getParameter("email") %>'/> 姓名:<%=mem.getName() %><br/> 性别:<%=mem.getGender() %><br/> 年龄:<%=mem.getAge() %><br/>
6.member05.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ page contentType="text/html; charset=utf-8" %> <%request.setCharacterEncoding("utf-8"); %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <html> <body> <form action="<%=path %>/javabean06.jsp" method="post"> name:<input type="text" name="username" value=""/><br/> gender:<input type="text" name="gender" value=""/><br/> email:<input type="text" name="email" value=""/><br/> age:<input type="text" name="age" value=""/><br/> <input type="submit" value="保存"> </form> </body> </html>
7. javabean06.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ page contentType="text/html; charset=utf-8" %> <%request.setCharacterEncoding("utf-8"); %> <%@ page language="java" import="day12.Member" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <jsp:useBean id="mem" class="day12.Member" type="day12.Member" scope="page"> Body************************** </jsp:useBean> <jsp:setProperty property="name" name="mem" param="username"/> <jsp:setProperty property="gender" name="mem" param="gender"/> <jsp:setProperty property="age" name="mem" param="age"/> <jsp:setProperty property="email" name="mem" param="email"/> 姓名:<%=mem.getName() %><br/> 性别:<%=mem.getGender() %><br/> 年龄:<%=mem.getAge() %><br/>
8.javabean07.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ page contentType="text/html; charset=utf-8" %> <%request.setCharacterEncoding("utf-8"); %> <%@ page language="java" import="day12.Member" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <jsp:useBean id="mem" class="day12.Member" type="day12.Member" scope="session"> Body**************************<br/> </jsp:useBean> <!-- 使用javaBean的自省机制要求: 只要html<form>标签所传递的参数名称完全对应JavaBean的属性名称(名称必须要一致) 即:email:<input type="text" name="email" value=""/>和 public void setEmail(String email) 就可以利用property="*"一次设定javaBean的所有属性。 --> <jsp:setProperty property="*" name="mem"/> 姓名:<jsp:getProperty name="mem" property="name" /> <br/> 性别:<jsp:getProperty name="mem" property="gender" /> <br/> 年龄:<jsp:getProperty name="mem" property="age" /> <br/>
9.member.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ page contentType="text/html; charset=utf-8" %> <%request.setCharacterEncoding("utf-8"); %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <html> <body> <form action="<%=path %>/zixingProperty/javabean.jsp" method="post"> name:<input type="text" name="name" value=""/><br/> gender:<input type="text" name="gender" value=""/><br/> email:<input type="text" name="email" value=""/><br/> age:<input type="text" name="age" value=""/><br/> <input type="submit" value="保存"> </form> </body> </html>
10. javabean.jsp<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ page contentType="text/html; charset=utf-8" %> <%request.setCharacterEncoding("utf-8"); %> <%@ page language="java" import="day12.Member" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <jsp:useBean id="mem" class="day12.Member" type="day12.Member" scope="session"> Body**************************<br/> </jsp:useBean> <!-- 使用javaBean的自省机制要求: 只要html<form>标签所传递的参数名称完全对应JavaBean的属性名称(名称必须要一致) 即:email:<input type="text" name="email" value=""/>和 public void setEmail(String email) 就可以利用property="*"一次设定javaBean的所有属性。 --> <jsp:setProperty property="*" name="mem"/> 姓名:<jsp:getProperty name="mem" property="name" /> <br/> 性别:<jsp:getProperty name="mem" property="gender" /> <br/> 年龄:<jsp:getProperty name="mem" property="age" /> <br/>