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

Posted 岸芷兰香

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

 

以上是关于JavaBean+jsp开发模式 --结合form表单 实例的主要内容,如果未能解决你的问题,请参考以下文章

jsp的开发模式

JSP开发模式

JavaWeb实现用户登录注册功能实例代码(基于Servlet+JSP+JavaBean模式)

project

代写Servlet代写JSP代写JavaBean程序作业

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