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 == ){
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);
}

request对象多种方法封装表单数据的更多相关文章

  1. 框架学习之Struts2(二)---基本配置和封装表单数据

    一.结果页面配置 1.局部结果页面配置 <!-- 局部结果页面配置--> <package name = "demo" extends = "strut ...

  2. 利用BeanUtils工具类封装表单数据

    一.BeanUtils工具类的使用 1.首先导入BeanUtils工具类的jar包 commons-beanutils-1.8.0.jar commons-logging-1.1.1.jar 2.se ...

  3. httpclient模拟post请求json封装表单数据

    好长时间不更博了,主要肚子里没什么好墨水,哈哈.废话不说上代码. public static String httpPostWithJSON(String url) throws Exception ...

  4. SpringMVC封装表单数据

    1.domain类 package com.xiaostudy.domain; public class User { private int id; private String username; ...

  5. 【flask】处理表单数据

     表单数据的处理涉及很多内容,除去表单提交不说,从获取数据到保存数据大致会经历以下步骤: 解析请求,获取表单数据. 对数据进行必要的转换,比如将勾选框的植转换为Python的布尔值. 验证数据是否符合 ...

  6. Servlet学习笔记(二):表单数据

    很多情况下,需要传递一些信息,从浏览器到 Web 服务器,最终到后台程序.浏览器使用两种方法可将这些信息传递到 Web 服务器,分别为 GET 方法和 POST 方法. 1.GET 方法:GET 方法 ...

  7. IT兄弟连 JavaWeb教程 Servlet表单数据

    很多情况下,需要传递一些信息,从浏览器到Web服务器,最终到后台程序.浏览器使用两种方法可将这些信息传递到Web服务器,分别为GET方法和POST方法. 1.GET方法 GET 方法向页面请求发送已编 ...

  8. Servlet表单数据

    1.GET 方法 GET 方法向页面请求发送已编码的用户信息.页面和已编码的信息中间用 ? 字符分隔,如下所示: http://www.test.com/hello?key1=value1&k ...

  9. jQuery使用serialize(),serializeArray()方法取得表单数据+字符串和对象类型两种表单提交的方法

    原始form表单值获取方式(手动): $.ajax({ type: "POST", url: "ajax.php", data: "Name=摘取天上 ...

随机推荐

  1. BZOJ 1003: [ZJOI2006]物流运输trans DP+最短路

    Description 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才能运完.货物运输过程中一般要转停好几个码头.物流公司通常会设计一条固定的运输路线,以便对整个运输过程实施严格 ...

  2. hdu 4764 && 2013长春网赛题解

    一个组合游戏题. 解答: 从后面往前面推,首先n-1是必胜位,然后前面的k位是必败位,如此循环下去.所以题目就容易了! 代码: #include<cstdio> using namespa ...

  3. hdu 2767

    这也是道强连通分量的题: 题目要求我们求出最少需要添加多少条边让整个图变成一个强连通分量: 思路很简单,直接缩点,然后找出所有点中有多少出度为0,入度为0的点,最大的那个就是题目所求: 贴代码: #i ...

  4. 练习生产者与消费者-PYTHON多线程中的条件变量同步-Queue

    以前练习过,但好久不用,手生,概念也生了, 重温一下.. URL: http://www.cnblogs.com/holbrook/tag/%E5%A4%9A%E7%BA%BF%E7%A8%8B/ ~ ...

  5. 【Xamarin开发 Android 系列 5】 Xamarin 的破解

    原文:[Xamarin开发 Android 系列 5] Xamarin 的破解 有关这个话题,十分敏感,公司开发还是支持下商业版权吧,毕竟一帮猴子辛辛苦苦没日没夜的干活,不说开宝马奔驰,吃饭还是必须的 ...

  6. 解决linux下导入数据库乱码问题

    引言:在windows下的mysql数据库导出SQL文件,在Linux下导入后显示为乱码. 1.启动Mysql服务及创建数据库(下面uushop为我将创建的数据库名) service mysqld s ...

  7. 从tcp原理角度理解Broken pipe和Connection reset by peer的区别

    从tcp原理角度理解Broken pipe和Connection reset by peer的区别 http://lovestblog.cn/blog/2014/05/20/tcp-broken-pi ...

  8. Java for selenium(webdriver) 环境搭建

    开发环境 1. jdk1.7 2. Eclipse 3. selenium(selenium-java-2.42.2.zip) 将下载下来的 selenium-java-2.42.2.zip 解压, ...

  9. Selenium终极自动化测试环境搭建(二):Selenium+Eclipse+Python

    前面举例了Selenium+Eclipse+Junit+TestNG自动化测试环境的搭建,在前一篇的基础上,下面再举例Selenium+Eclipse+Python测试环境搭建. 第一步:安装Pyth ...

  10. linux下jdk的卸载与安装

    JDK的卸载 1.检查jdk的是否安装,显示如下表示安装: [root@localhost ~]# rpm -aq|grep java tzdata-java-2010l-1.el6.noarch j ...