1. 程式人生 > >JavaBean+jsp開發模式 --結合form表單 實例

JavaBean+jsp開發模式 --結合form表單 實例

fff checkbox oct ima ring pub meta 結合 text

1.創建form表單

技術分享
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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=UTF-8"
> <title>Insert title here</title> </head> <body> <div style="margin-left:500px;"> <form action="testBean.jsp" method="post" name="f1"> <table> <tr> <td align="right">用戶名:</td> <td
align="left"> <input type="text" name="userName"></input> </td> </tr> <tr> <td align="right">密碼:</td> <td align="left"> <input type="password"
maxlength="16" id="mima" name="password"></input> </td> </tr> <tr> <td align="right">性別:</td> <td align="left" > <input type="radio" name="sex" value="男"/><input type="radio" name="sex" value="女"/></td> </tr> <tr> <td align="right">年齡:</td> <td align="left" > <input name="age" type="number" /> </td> <tr> <td align="right">個人愛好:</td> <td align="left"> <input type="checkbox" name="hobby" value="運動"/>運動 <input type="checkbox" name="hobby" value="睡覺"/>睡覺 <input type="checkbox" name="hobby" value="購物"/>購物 <input type="checkbox" name="hobby" value="上網"/>上網 </td> </tr> <tr> <td colspan="2" align="center"> <input type="submit"/> <input type="reset"/> </td> </tr> </table> </form> </div> </body> </html>
beanForm.jsp

2.在服務器中創建javaBean類

package com.serv;

public class Student
{
    //屬性
    private String userName;
    private String password;
    private String sex;
    private Integer age;
    private String[] hobby;
    private String hobbys="";
    
    //提供set和get方法
    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 String getSex()
    {
        return sex;
    }
    public void setSex(String sex)
    {
        this.sex = sex;
    }
    public Integer getAge()
    {
        return age;
    }
    public void setAge(Integer age)
    {
        this.age = age;
    }
    public String[] getHobby()
    {
        return hobby;
    }
    public void setHobby(String[] hobby)
    {
        this.hobby = hobby;
    }
    public String getHobbys()
    {
        //將String數組轉化成String字符串,便於取值;
        int len=this.hobby.length;
        if(len>0)
        {
            for(String str:hobby)
            {
                hobbys+=str+",";                
            }
        }
        hobbys=hobbys.substring(0,hobbys.length()-1);
        
        return hobbys;
    }
    public void setHobbys(String hobbys)
    {
        this.hobbys = hobbys;
    }
    
    
    
    
}

3.前段或者後臺取值

技術分享
package com.serv;

public class Student
{
    //屬性
    private String userName;
    private String password;
    private String sex;
    private Integer age;
    private String[] hobby;
    private String hobbys="";
    
    //提供set和get方法
    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 String getSex()
    {
        return sex;
    }
    public void setSex(String sex)
    {
        this.sex = sex;
    }
    public Integer getAge()
    {
        return age;
    }
    public void setAge(Integer age)
    {
        this.age = age;
    }
    public String[] getHobby()
    {
        return hobby;
    }
    public void setHobby(String[] hobby)
    {
        this.hobby = hobby;
    }
    public String getHobbys()
    {
        //將String數組轉化成String字符串,便於取值;
        int len=this.hobby.length;
        if(len>0)
        {
            for(String str:hobby)
            {
                hobbys+=str+",";                
            }
        }
        hobbys=hobbys.substring(0,hobbys.length()-1);
        
        return hobbys;
    }
    public void setHobbys(String hobbys)
    {
        this.hobbys = hobbys;
    }
    
    
    
    
}
testBean.jsp

JavaBean+jsp開發模式 --結合form表單 實例