<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的傳遞過來的參數