JavaBean的一个小例子

一、创建一个javaBean类: UseBean

package com.oncall24h.ruchi;
import java.io.Serializable;
public class UseBean implements Serializable {

    private String username;
    private String password;
    
    public UseBean() {
        super();
    }
    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;
    }
}

二、创建表单所在的JSP页面 register.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Register</title>
</head>
<body>
    <form action="do_register.jsp" method="post">
        USERNAME: <input type="text" name="yonghuming"/>
        PASSWORD: <input type="password" name="mima"/>
        <input type="submit" value="submit"/>
    </form>
</body>
</html>

三、创建表单数据传到的Jsp页面 do_register.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Handle</title>
</head>
<body>
    <jsp:useBean id="user" class= "com.oncall24h.ruchi.UseBean"></jsp:useBean>
    <%--
            user is an arbitrary identifier , UseBean is the defined class name
            which is the same as: UseBean user= new UseBean();
    --%>
    <jsp:setProperty property="username" name="user" param="yonghuming"/>
    <%--
            the same as:
                user.setUsername("yonghuming");     // yonghuming comes from the form of register.jsp 
     --%>
    <jsp:setProperty property="password" name="user" param="mima"/>
    <%--
            the same as:
                user.setPassword("mima");     // mima comes from the form of register.jsp 
     --%>

    <jsp:getProperty property="username" name="user"/>
    <%--
            the same as:
                user.getUsername("username");    //username is the property of class UseBean 
     --%>
    <jsp:getProperty property="password" name="user"/>
    <%--
            the same as:
                user.getPassword("password");    //password is the property of class UseBean 
     --%>

</body>
</html>

 

posted @ 2015-07-23 21:28  TACHIA  阅读(289)  评论(0编辑  收藏  举报