获取表单数据的四种方法

表单:

< html>
< head>
< meta charset= "UTF-8" >
< title> Insert title here 

< body>
     
用户名: < input type= "text" name ="userName" >< br> 密   码:< input type = "password" name ="pwd" >< br> < input type= "radio" value = "男" name ="sex" checked= "checked"> 男 < input type= "radio" value = "女" name ="sex" >女 < br> 爱好: < input type= "checkbox" name = "hobby" value ="篮球" >篮球 < input type= "checkbox" name = "hobby" value ="唱歌" >唱歌 < input type= "checkbox" name = "hobby" value ="画画" >画画 < br> 所在城市: < select name= "city" > < option> ===请选择=== < option value= "sh" >上海 < option value= "bj" >北京 < option value= "gz" >广州 < br > < input type= "submit" value = "注册">



方法一: request  . getParameter()

//获取表单中的信息
        String userName = request .getParameter( "userName");
        String sex = request .getParameter( "sex");
        String[] hobby = request .getParameterValues( "hobby");
        String city = request .getParameter( "city");
         //转换编码
         userName= new String(userName .getBytes("iso-8859-1" ),"UTF-8" );
        System. out .println(userName );
         //遍历hobby
         for (int i = 0; hobby != null&& i < hobby .length ; i ++) {
            System. out .print(hobby [i ]+"\t" );
        }
        System. out .println();
        System. out .println(city );


方法二: request  .getParameterNames()

request .setCharacterEncoding("UTF-8" );
         Enumeration names = request .getParameterNames();
         while (names .hasMoreElements()) {
            String name = (String) names .nextElement();
            String[] values = request .getParameterValues(name );
             //遍历values
             for (int i = 0;values != null&& i < values .length ; i ++) {
                System. out .println(name +":\t" +values [i ]);
            }
        }



方法三:    request .getParameterMap()
    
 //创建User对象
         try {
            User user= new User();
            System. out .println(user );
             //获取map集合
            Map map = request .getParameterMap();
             //遍历map
             for (Map.Entry m   : map .entrySet()) {
                String name= m.getKey();
                String[] value= m.getValue();
                 //创建一个属性描述器
                PropertyDescriptor pd= new PropertyDescriptor(name , User.class );
                 //获得 Uer实体的设置器
                Method setter = pd .getWriteMethod();
                 //得到etter属性
                 if (value .length ==1){
                     setter.invoke( user, value [0]);
                } else {
                     setter.invoke( user, (Object)value );
                }
                
            }
            System. out .println("用户信息" +user .getCity());
        } catch (Exception e ) {
             // TODO: handle exception
             e.printStackTrace();
        }


package day08_response.entity;
//User实体类
public class User {
    String userName; //用户名
    String pwd; //密码
    String[] hobby; //爱好
    String sex; //性别
     public String getSex() {
         return sex ;
    }
     public void setSex(String sex ) {
         this .sex = sex ;
    }
    String city; //城市
     public String getUserName() {
         return userName ;
    }
     public void setUserName(String userName ) {
         this .userName = userName ;
    }
     public String getPwd() {
         return pwd ;
    }
     public void setPwd(String pwd ) {
         this .pwd = pwd ;
    }
     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 ;
    }

}



方法四:使用BeanUtils框架

将BeanUtils.jar和 commons-logging.jar 导入项目中

try {
            User user= new User();
            System. out .println(user );
             request.setCharacterEncoding( "UTF-8" );
            BeanUtils. populate( user, request .getParameterMap());
            System. out .println("user信息:" +user );
        } catch (Exception e ) {
             // TODO Auto-generated catch block
             e.printStackTrace();
        }



你可能感兴趣的:(javaWeb)