内省和BeanUtils框架3

内省和BeanUtils框架各种表单输入域数据的获取及中文编码
        注: 需要jar包: commons-beanutils-1.8.3.jar和commons-logging-1.1.1.jar
        表单输入域类型:
          radio checkbox,即使表单中有对应名称输入域,如果一个不选择,则什么数据不会带给服务器。(注意空指针异常)
          如果选择了其中的一个或多个,则把他们的value的取值提交给服务器。
          如果选择了其中的一个或多个,他们又没有value取值,则提交给服务器的值是on.
               
        *********************************************************************************************
        
        public class Student {
                private int id;
                private String name;
                private String password;
                private String gender;
                private String married;
                private String[] hobby;
                private String city;
                private String description;          
                public int getId() {
                        return id;
                }
                public void setId(int id) {
                        this.id = id;
                }
                public String getName() {
                        return name;
                }
                public void setName(String name) {
                        this.name = name;
                }
                public String getPassword() {
                        return password;
                }
                public void setPassword(String password) {
                        this.password = password;
                }
                public String getGender() {
                        return gender;
                }
                public void setGender(String gender) {
                        this.gender = gender;
                }
                public String getMarried() {
                        return married;
                }
                public void setMarried(String married) {
                        this.married = married;
                }
                public String[] getHobby() {
                        return hobby;
                }
                public void setHobby(String[] hobby) {
                        this.hobby = hobby;
                }
                public String getCity() {
                        return city;
                }
                public void setCity(String city) {
                        this.city = city;
                }
                public String getDescription() {
                        return description;
                }
                public void setDescription(String description) {
                        this.description = description;
                }         
                @Override
                public String toString() {
                        return "Student [city=" + city + ", description=" + description
                            + ", gender=" + gender + ", hobby=" + Arrays.toString(hobby)
                            + ", id=" + id + ", married=" + married + ", name=" + name
                            + ", password=" + password + "]";
                }         
        }
        
        *********************************************************************************************
        
        <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
        <html>
                <head>
                        <title>学生信息表</title>                
                        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
                        <meta http-equiv="description" content="this is my page">
                        <meta http-equiv="content-type" content="text/html; charset=UTF-8">                       
                        <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
                </head>                
                <body>
                        <form action="/day07/servlet/RequestDemo4" method="post">
                              <input type="hidden" name="id" value="438" /> <br/><br/>
                          姓名:<input type="text" name="name" /> <br/><br/>
                          密码:<input type="password" name="password" /> <br/><br/>
                          性别:<input type="radio" name="gender" value="1" />男
                            <input type="radio" name="gender" value="0" />女 <br/><br/>
                          已婚: <input type="checkbox" name="married" /> <br/><br/>
                          爱好:<input type="checkbox" name="hobby" value="fb" />足球
                                     <input type="checkbox" name="hobby" value="bb" />篮球
                                     <input type="checkbox" name="hobby" value="tt" />乒乓球 <br/><br/>
                          故乡:<select name="city">
                                          <option value="BJ">北京</option>
                                          <option value="SD">山东</option>
                                          <option value="SH">上海</option>
                                    </select> <br/><br/>
                          简介:<textarea name="description" rows="5" cols="40" ></textarea> <br/><br/>
                          <input type="image" src="submit.png" /> <br/><br/>
                        </form>
                </body>
        </html>


        *********************************************************************************************   
       
          import java.io.IOException;
          import java.lang.reflect.InvocationTargetException;
          import javax.servlet.ServletException;
          import javax.servlet.http.HttpServlet;
          import javax.servlet.http.HttpServletRequest;
          import javax.servlet.http.HttpServletResponse;
          import org.apache.commons.beanutils.BeanUtils;
          import cn.itcst.domain.Student;
          
          public class RequestDemo4 extends HttpServlet {
                  public void doGet(HttpServletRequest request, HttpServletResponse response)
                      throws ServletException, IOException {
                              // HTML采用什么编码,此处设置为什么编码
                              request.setCharacterEncoding("UTF-8");
                              
                              Student s = new Student();
                              System.out.println("封装前:" + s);                             
                              try {
                                BeanUtils.populate(s, request.getParameterMap());
                              } catch(Exception e) {
                                e.printStackTrace();
                              }                              
                              System.out.println("封装后:" + s);
                            }


                            public void doPost(HttpServletRequest request, HttpServletResponse response)
                                throws ServletException, IOException {
                              doGet(request, response);
                            }
          }


        *********************************************************************************************
     
     
     
     
     
     
     
     
     
     
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值