JavaBean+jsp开发模式 --结合form表单 实例

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

 

posted @ 2017-07-25 18:23  千年虫虫  阅读(1256)  评论(0编辑  收藏  举报