Java获取HttpServletRequest request中所有参数的方法

获取request中单个参数

String value = request.getParameter(name);


获取request中所有的参数

方法1

Enumeration<String> param = request.getParameterNames();
while (param.hasMoreElements()) {
String key = param.nextElement();
String value = request.getParameter(key);

System.out.println(key + ":" + value);
}

方法2

Map map = request.getParameterMap();
Set keSet = map.entrySet();
for (Iterator iterator = keSet.iterator(); iterator.hasNext();) {
Map.Entry me = (Map.Entry) iterator.next();
Object ok = me.getKey();
Object ov = me.getValue();
String[] value = new String[1];
if (ov instanceof String[]) {
value = (String[]) ov;
} else {
value[0] = ov.toString();
}

for (int k=0; k<value.length; k++) {
System.out.println(ok + "=" + value[k]);
}
}


    回到顶部