BeanSample.java (編譯要用 javac -d . BeanSample.java
-----------------------------------------------------------------------------
package javabean;
import java.io.*;
public class BeanSample{
public BeanSample() {
}
private String username;
private String password;
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
}
usebean.jsp
------------------------------------------------------------------------------------
<%@ page contentType="text/html;charset=big5" %>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=big5">
<title>JavaBean的使用範例</title>
</head>
<body>
<center>
<h2>JavaBean的使用範例</h2>
<hr>
<jsp:useBean id="myBean" scope="page" class="javabean.BeanSample" />
<jsp:setProperty name="myBean" property="username" value="Yeong_Ho" />
<jsp:setProperty name="myBean" property="password" value="myPassword" />
<p>顯示輸入的名稱:<jsp:getProperty name="myBean" property="username" /></p>
<p>顯示輸入的密碼:<jsp:getProperty name="myBean" property="password" /></p>
</center>
</body>
</html>