如何解决javarequest请求中有多个参数的问题

如题所述

  通过程序遍历http请求的所有参数放到hashmap中,用的时候方便了。

  如果参数值有中文,那么需要在程序中添加filter转码,或者在下面程序里,对paramValue转码

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

Map map = new HashMap();

Enumeration paramNames = request.getParameterNames();

while (paramNames.hasMoreElements()) {

String paramName = (String) paramNames.nextElement();

String[] paramValues = request.getParameterValues(paramName);

if (paramValues.length == 1) {

String paramValue = paramValues[0];

if (paramValue.length() != 0) {

System.out.println("参数:" + paramName + "=" + paramValue);

map.put(paramName, paramValue);

}

}

}
Map map = new HashMap();
Enumeration paramNames = request.getParameterNames();
while (paramNames.hasMoreElements()) {
String paramName = (String) paramNames.nextElement();

String[] paramValues = request.getParameterValues(paramName);
if (paramValues.length == 1) {
String paramValue = paramValues[0];
if (paramValue.length() != 0) {
System.out.println("参数:" + paramName + "=" + paramValue);
map.put(paramName, paramValue);
}
}
}

}
温馨提示:答案为网友推荐,仅供参考
相似回答