request.getParameterMap 乱码问题

veardn 2009-07-15 04:34:42
不知道大家有没有用过request.getParameterMap()这个方法,我的代码如下:
HashMap hm=(HashMap)request.getParameterMap();
Iterator it=hm.entrySet().iterator();
while(it.hasNext())
{
Map.Entry element=(Map.Entry)it.next();
System.out.println(element.getKey().toString()+" "+element.getValue().toString());
}
打印出来的key是正常的,但value是乱码:
name [Ljava.lang.String;@1205d8d
pwd [Ljava.lang.String;@1de152f
有没有遇到过同样问题的朋友?
...全文
695 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
javason 2011-04-13
  • 打赏
  • 举报
回复
其实6楼可能只是解决了如何从request.getParameterMap();获取值的问题,而真正要解决乱码问题还得从源头开始,假如你提交了一个请求,使用GBK编码,而j2ee服务器默认使用utf-8解码,那么就需要在提交请求前将编码设置好,可以在服务器端设置:request.setCharactorEncoding("utf-8");也可以在客户端提前设置,以jquery的ajax为例:
$.ajax({
type: "post",
url: url,
contentType: "application/x-www-form-urlencoded; charset=utf-8",
dataType: "json",
data: {mydata: mydata},
success: successHandler,
error: errorHandler
}
shentaoo 2009-12-02
  • 打赏
  • 举报
回复
fdsfsdfsdf
veardn 2009-07-15
  • 打赏
  • 举报
回复
用了6楼的方法就好了
然后我改了下jsp
<%@page contentType="text/html; charset=UTF-8"%>
<form action="/test1/cx/cx" method="post">
name: <input type=text name="name"> <br>
name: <input type=text name="name"> <br>
password: <input type="password" name="pwd"> <br>
<input type="submit" value="submit">
</form>

结果打印出来是
name [aaa, bbb]
pwd [ccc]

也就是说这个方法考虑到了相同name的情况
jastby 2009-07-15
  • 打赏
  • 举报
回复
Object obj = element.getValue();
String val = "";
if (obj instanceof String[]){
String[] strs = (String[])obj;
val = Arrays.toString(strs);//jdk1.5以上才支持,1.4的话就自己循环
}else{
val = obj.toString();
}
sphsyv 2009-07-15
  • 打赏
  • 举报
回复
在HashMap hm=(HashMap)request.getParameterMap(); 之前加上request.setCharactorEncoding("utf-8");能行么?
veardn 2009-07-15
  • 打赏
  • 举报
回复
request.getParameterNames()我看了下说明:
Returns an Enumeration of String objects containing the names of the parameters contained in this request. If the request has no parameters, the method returns an empty Enumeration.
它返回的是form里所有的name,而没有value
veardn 2009-07-15
  • 打赏
  • 举报
回复
jsp是这样的:
<%@page contentType="text/html; charset=UTF-8"%>
<form action="/test1/cx/cx" method="post">
name:<input type=text name="name"><br>
password:<input type="password" name="pwd"><br>
<input type="submit" value="submit">
</form>
jastby 2009-07-15
  • 打赏
  • 举报
回复
Enumeration enu=request.getParameterNames();
String param = "";
while(enu.hasMoreElements()){
String paraName=(String)enu.nextElement();
param += ("".equals(param)?"":"&");
param += (paraName+"="+request.getParameter(paraName));
}
wangdawei722 2009-07-15
  • 打赏
  • 举报
回复
你的value是对象吗????

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧