<jsp:useBean id="随便你取一个名字比如 tt" class="具体JavaBean的位置如 com.Test.TT" scope="JavaBean生存周期(page、request、session、application)任选一种"></jsp:useBean>
<%
//正常的函数调用
tt.setXX("dfada");
%>
<jsp:setProperty name="你前面useBean中的id:tt" property="你想传入的类的属性列比如userName" param="从别的地方获取的数据比如userName"/value只能用其中一个 其其中VALUE是固定值>它主要用于从一个页面到另一个页面进行传参比方这个例子吧
javaBean:
package com.chapter2;
public class Login {
public String userName;
public String password;
public Login(){
this.userName = null;
this.password = null;
}
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;
}
}
login.html:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>JavaBean的应用实例</title>
</head>
<body>
<div align="center">
<h1>用户登陆</h1>
<form name="login" method="post" action="login.jsp" target="_self">
<table width="300" border="0">
<tr>
<td width="50%" height="30" align="right">用户名:</td>
<td width="50%" height="30" align="left"><input type="text" name="userName" size="15"></td>
</tr>
<tr>
<td width="50%" height="30" align="right">密码:</td>
<td width="50%" height="30" align="left"><input type="password" name="password" size="15"></td>
</tr>
<tr>
<td width="100%" height="40" align="center" colspan="2"><input type="submit" name="sub" value="登陆"></td>
</tr>
</table>
</form>
</div>
</body>
</html>
login.jsp
<%@ page contentType="text/html; charset=gb2312" language="java" %>
<jsp:useBean id="login" class="com.chapter2.Login" scope="page"/>
<jsp:setProperty name="login" property="userName" param="userName"/>
<jsp:setProperty name="login" property="password" value="password"/>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=gb2312">
<title>JavaBean 的应用实例</title>
</head>
<body>
<div align="center">
<h2>用户登陆信息如下:</h2>
<p> 用户名:<font color="#0000FF"><%=login.getUserName() %></font></p>
<p> 密码:<font color="#0000FF"><%=login.getPassword() %></font></p>
</div>
</body>
</html>
login2.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>JavaBean的应用实例</title>
</head>
<body>
<div align="center">
<h1>用户登陆</h1>
<form name="login2" method="post" action="login2.jsp" target="_self">
<table width="300" border="0">
<tr>
<td width="50%" height="30" align="right">用户名:</td>
<td width="50%" height="30" align="left"><input type="text" name="userName" size="15"></td>
</tr>
<tr>
<td width="50%" height="30" align="right">密码:</td>
<td width="50%" height="30" align="left"><input type="password" name="password" size="15"></td>
</tr>
<tr>
<td width="100%" height="40" align="center" colspan="2"><input type="submit" name="sub" value="登陆"></td>
</tr>
</table>
</form>
</div>
</body>
</html>
login2.jsp
<%@ page contentType="text/html; charset=gb2312" language="java" %>
<jsp:useBean id="login2" class="com.chapter2.Login" scope="page"/>
<jsp:setProperty name="login2" property="userName" param="userName"/>
<jsp:setProperty name="login2" property="password" param="password"/>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=gb2312">
<title>JavaBean 的应用实例</title>
</head>
<body>
<div align="center">
<h2>用户登陆信息如下:</h2>
<p> 用户名:<font color="#0000FF"><jsp:getProperty property="userName" name="login2"/></font></p>
<p> 密码:<font color="#0000FF"><jsp:getProperty property="password" name="login2"/></font></p>
</div>
</body>
</html>
上面我写的例子其实效果一样,相信大家都命名这个过程其实就是将静态页面的数据提交给jsp界面处理的过程,jsp页面处理的时候
先是通过useBean指定相应的对象,再通过setproperty来给对象的属性赋值
最后要么直接通过对象来获取对象值要么可以通过getproperty方法获取setproperty的传递过来的参数