<jsp:useBean>标签用来在jsp页面中创建一个Bean实例,定义语法如下:
一、<jsp:useBean>语法
<jsp:useBean id="id" scope="page|request|session|application" typeSpec/>
二、<jsp:setProperty>语法
<jsp:setProperty name="beanName" propertyDetail />其中,propertyDetail可以是以下中的一个:
1,property="*"
2,property="propertyName" param="parameterName"
3,property="propertyName"
4,property="propertyName" value="property value"
三、<jsp:getProperty>语法
<jsp:getProperty name="beanName" property="propertyName"/>
例:
TestBean.java
package com.jsp.ch3; public class TestBean { private String userName; private String password; private int age; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
register.html
<html> <body> <form action="register.jsp" method="post"> <table> <tr> <td> 姓名:<input type="text" name="userName"> </td> </tr> <tr> <td> 密码:<input type="text" name="password"> </td> </tr> <tr> <td> 年龄:<input type="text" name="age"> </td> </tr> <tr> <td> <input type="submit"> </td> </tr> </table> </form> </body> </html>
register.jsp
<%@ page language="java" pageEncoding="gb2312"%> <jsp:useBean id="user" scope="page" class="com.jsp.ch3.TestBean"/> <jsp:setProperty name="user" property="*"/> 或者用以下,param可以不填写,其中param对应的是提交页面的表单name <jsp:setProperty property="userName" name="user" param="userName"/> <jsp:setProperty property="password" name="user" param="password"/> <jsp:setProperty property="age" name="user" param="age"/> <html> <body> 注册成功:<br> <hr> 使用Bean的属性方法<br> 用户名: <%=user.getUserName()%><br> 密码: <%=user.getPassword()%><br> 年龄: <%=user.getAge()%><br> <hr> 使用getProperty<br> 用户名:<jsp:getProperty name="user" property="userName"/><br> 密码: <jsp:getProperty name="user" property="password"/><br> 年龄: <jsp:getProperty name="user" property="age"/> 客户端名称:<%=request.getRemoteAddr() %> </body> </html>