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. javascript mvc

    http://www.webjx.com/javascript/jsajax-15996.html http://blog.csdn.net/jjfat/article/details/7963608 ...

  2. 为网站添加一个图标icon

    <link rel="icon" href="/favicon.ico" type="image/x-icon"/> <l ...

  3. http://www.ibm.com/developerworks/cn/opensource/os-cn-cas/

    http://www.ibm.com/developerworks/cn/opensource/os-cn-cas/

  4. DHTMLX 前端框架 建立你的一个应用程序教程(二)--设置布局

    Layout控件的演示 Dhtmlx有很多的组建来组织网页的建设, 这篇主要介绍dhtmlxLayout . 下面图片中 布局将各个组件(1.Menu 2.Toolbar 3.Grid 4.Form ...

  5. HttpResponseCache 网络缓存使用

    Caches HTTP and HTTPS responses to the filesystem so they may be reused, saving time and bandwidth. ...

  6. 使用Common.Logging+log4net规范日志管理

    Common.Logging+(log4net/NLog/) common logging是一个通用日志接口,log4net是一个强大的具体实现,也可以用其它不同的实现,如EntLib的日志.NLog ...

  7. wcf中netTcpBinding的元素构成

    <security> of <netTcpBinding> <transport> of <netTcpBinding> <message> ...

  8. bzoj1085

    肯定是搜索题无疑问, 首先要求在15步以内(包括15步)到达目标状态,也就是限定了搜索的深度,于是我们用dfs更合适 但这样复杂度仍然太大,原因就是我们在搜索中做了很多很不优的尝试 考虑当前状态若与目 ...

  9. 一起啃PRML - 1.2.1 Probability densities 概率密度

    一起啃PRML - 1.2.1 Probability densities @copyright 转载请注明出处 http://www.cnblogs.com/chxer/ 我们之前一直在讨论“谁取到 ...

  10. SharePoint 2010 母版页定制小思路介绍

    转:http://tech.ddvip.com/2013-11/1384521515206064.html 介绍:我们使用SharePoint2010做门户网站,经常需要定制母版页,但是2010提供的 ...