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 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表单:
insert title here用户名
密码
性别 男
女
爱好 篮球
足球
游戏
电影
城市
北京
上海
深圳
杭州
二、通过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 map = request.getparametermap();
for (map.entry 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);
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持萬仟网。
希望与广大网友互动??
点此进行留言吧!