欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Javaweb获取表单数据的多种方式

程序员文章站 2024-03-06 11:01:49
javaweb获取表单数据的几种方式 一、通过键值对的形式获取表单数据 getparameter(string name):通过key,返回一个value。 getp...

javaweb获取表单数据的几种方式

一、通过键值对的形式获取表单数据

getparameter(string name):通过key,返回一个value。
getparametervalues(string name):通过key返回一个string数组(多个值)
getparameternames():返回form表单中的所有key值。

下面介绍通过键值对获取form表单数据的数据的方法:

@webservlet({ "/formservlet", "/form" })
public class formservlet extends httpservlet {
 private static final long serialversionuid = 1l;

 public formservlet() {
 super();
 }

 protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {

 response.setcontenttype("text/json;charset=utf-8");

 printwriter out = response.getwriter();

 enumeration<string> paramnames = request.getparameternames();

 while (paramnames.hasmoreelements()) {
 string name = paramnames.nextelement();

 string[] values = request.getparametervalues(name);

 if(values!=null && values.length>0){
 stringbuilder builder = new stringbuilder(); 
 for (int i = 0; i < values.length; i++) {
  builder.append(values[i]+" ");
 }
 out.println(name+" : "+builder.tostring());
 }
 }
 }

 protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
 doget(request, response);
 }

}

form表单:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>insert title here</title>
</head>
<body>
 <form action="/tomcatdemo/form" method="post">

 用户名<input type="text" name="username"/><br/>
 密码 <input type="password" name="password"/><br/>
 性别 <input type="radio" name="sex" value="male" checked="checked"/>男
 <input type="radio" name="sex" value="female"/>女<br/>
 爱好 <input type="checkbox" name="hobby" value="basketball"/>篮球
 <input type="checkbox" name="hobby" value="football"/>足球
 <input type="checkbox" name="hobby" value="game"/>游戏
 <input type="checkbox" name="hobby" value="media"/>电影<br/>
 城市 <select name="city">
  <option value="bj">北京</option>
  <option value="sh">上海</option>
  <option value="sz">深圳</option>
  <option value="hz">杭州</option>
  </select><br/>
  <input type="submit" value="注册"/>
  <input type="submit" value="登入"/><br/>

 </form>
</body>
</html>

二、通过map的形式获取表单数据

getparametermap():获取form表单的数据,以map的格式封装起来

示例:

@webservlet({ "/formservlet", "/form" })
public class formservlet extends httpservlet {
 private static final long serialversionuid = 1l;

 public formservlet() {
 super();
 }

 protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {

 response.setcontenttype("text/json;charset=utf-8");

 printwriter out = response.getwriter();

 user user = new user();
 out.println("获取表单数据之前:"+user.tostring());

 map<string,string[]> map = request.getparametermap();

 for (map.entry<string, string[]> m : map.entryset()) {

 string name = m.getkey();
 string[] values = m.getvalue();
 //属性描述器:表示javabean类通过存储器导出一个属性
 propertydescriptor pd=null;
 try {
  pd = new propertydescriptor(name, user.class);
 } catch (introspectionexception e) {
 e.printstacktrace();
 }

 if (values!=null&& pd !=null) {

 method setter = pd.getwritemethod();
 try {
  if (values.length==1) {
  setter.invoke(user, values[0]);
  }else {
  setter.invoke(user, (object)values);
  }
 } catch (illegalaccessexception e) {
  e.printstacktrace();
 } catch (illegalargumentexception e) {
  e.printstacktrace();
 } catch (invocationtargetexception e) {
  e.printstacktrace();
 }
 }
 }
 out.println("获取表单数据之后:"+user.tostring());
 }

 protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
 doget(request, response);
 }
}

propertydescriptor的使用,

user类

public class user {
 private string username;//属性名称需要和表单数据中的name值保持一致
 private string password;
 private string sex;
 private string[] hobby;
 private string city;
 ...
 //set get方法省略
}

三、通过第三方jar包获取封装表单数据

使用第三方jar包:commons-beanutils-1.8.3.jar

@webservlet({ "/formservlet", "/form" })
public class formservlet extends httpservlet {
 private static final long serialversionuid = 1l;

 public formservlet() {
 super();
 }

 protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {

 response.setcontenttype("text/json;charset=utf-8");

 printwriter out = response.getwriter();

 user user = new user();
 out.println("获取表单数据之前:"+user.tostring());

 try {
 //通过第三方jar包处理
 beanutils.populate(user, request.getparametermap());
 } catch (illegalaccessexception e) {
 e.printstacktrace();
 } catch (invocationtargetexception e) {
 e.printstacktrace();
 }

 out.println("获取表单数据之后:"+user.tostring());

 }


 protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
 doget(request, response);
 }

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。