public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // test1(request); // test2(request); test3(request); } //获取单个控件的值 public void test1(HttpServletRequest request){ String name = request.getParameter("username"); System.out.println("name=" + name); } //获取同名控件的值 public void test2(HttpServletRequest request){ String[] age = request.getParameterValues("age"); for (String string : age) { System.out.println("string=" + string); } } //获取全部控件 public void test3(HttpServletRequest request){ //获取所有控件的名字 System.out.println("--------request--------" + request.toString()); Enumeration<String> enu = request.getParameterNames(); while(enu.hasMoreElements()){ String name = enu.nextElement();//控件的名字 //可能有多个重复 String[] values = request.getParameterValues(name);//控件的值 for (String value : values) { System.out.println(name + "---" + value); } } }
//将表单中的所有值封装到JavaBean中
第四种方法:将表单中的所有值封装到JavaBean中:采用PropertyDesceipt方法
首先添加一个Javabean:实现序列化接口: implements Serializable 表示对象可以被序列化
public class User implements Serializable{ private String username; private String[] password; 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; } @Override public String toString() { return "User [username=" + username + ", password=" + Arrays.toString(password) + "]"; } }
实现方法:
public void test4(HttpServletRequest request){ User user = new User(); Enumeration<String> enu = request.getParameterNames(); while(enu.hasMoreElements()){ //拿到控件的名字 String name = enu.nextElement(); //控件的值 String[] values = request.getParameterValues(name); try { //拿到属性name的属性描述器 PropertyDescriptor pd = new PropertyDescriptor(name, User.class); //将值注入到属性中 //拿到写描述器 Method md = pd.getWriteMethod(); //为了兼容jdk1.4,invoke方法在执行时将参数拆开进行传递,所以导致IllegalArgumentException if(values.length == 1){ md.invoke(user, values); }else{
//数组被拆开了 //解决办法1: // md.invoke(user, (Object)values); //解决办法2: md.invoke(user, new Object[] {values}); } // md.invoke(user, values); } catch (IntrospectionException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } } System.out.println("封装后的值:"+ user); }
第六种方法:BeanUtils类封装
//将表单中的所有值封装到JavaBean中,采用第三方jar进行:BeanUtils类 //需要:commons-logging-1.1.1.jar commons-beanutils-1.8.3.jar public void test5(HttpServletRequest request){ User user = new User(); System.out.println("封装前:" + user); Enumeration<String> enu = request.getParameterNames(); while(enu.hasMoreElements()){ String name = enu.nextElement(); String[] values = request.getParameterValues(name); //采用BeanUtils类封装 try { BeanUtils.setProperty(user, name, values); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } } System.out.println("封装后" + user); }
第七种:request.getParameterMap(); BeanUtils
//页面所有的数据传递都是String类型 public void test6(HttpServletRequest request){ User user = new User(); System.out.println("封装前:" + user); Map<String, String[]> map = request.getParameterMap(); for (Map.Entry<String , String[]> entry : map.entrySet()) { //拿到控件的名字 String name = entry.getKey(); String[] values = entry.getValue(); //封装数据 try { BeanUtils.setProperty(user, name, values); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } } System.out.println("封装后" + user); }
推荐方法
//推荐方法 public void test7(HttpServletRequest request){ User user = new User(); try { BeanUtils.populate(user, request.getParameterMap()); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("封装后777" + user); }