用 request.getParameter() 得到的是乱码问题(谢谢高手指点)
从一页面传过来一个参数 userId=${element.id} 但在Action 中打印出来是乱码
请问各位 这是什么问题 以前没遇到过,希望高手指点!
页面代码:
<logic:iterate id= "element " name= "account ">
<tr>
<td>
<html:link href= "update.do?userId=${element.id} "> <font color= "red "> ${element.id} </font> </html:link>
</td>
<td>
<bean:write name= "element " property= "userName " ignore= "true "/>
</td>
<td>
<bean:write name= "element " property= "userPass " ignore= "true "/>
</td>
<td>
<bean:write name= "element " property= "description " ignore= "true "/>
</td>
<td>
<h2 align= "center "> <a href= "delete.do?userId=${element.id} "> <font color= "red "> 删除该用户 </font> </a> </h2>
</td>
</tr>
</logic:iterate>
Action 中 :
String id = (String)request.getParameter( "userId ") ;
System.out.println( "删除操作的测试 ! " + id) ;
id 打印出来是乱码
------解决方案--------------------在JSP有 <%@ page language= "java " contentType= "text/html;charset=GBK "%> 吗?
ID是什么类型?如果是中文,按下面的方法试试
String id = (String)request.getParameter( "userId ") ;
String id= new String(id.getBytes( "ISO-8859-1 "), "GBK ");//
System.out.println( "删除操作的测试 ! " + id) ;
------解决方案--------------------URL中文参数传递问题
(1)确定JSP页面头部是否有: <%@ page contentType= "text/html; charset=GBK " %>
(2)用这个转码:
String param= new String(request.getParameter( "param ").getBytes( "ISO-8859-1 "), "GBK ");
(3)添加filter字符过滤器
(4)如果是通过 "a.jsp?param=中文 "传递参数,则需要:
a.在传参数之前先把参数进行转码:java.net.URLEncoder.encode(param);
取值用java.net.URLDncoder.decode(param);再转回中文
b.在你的Tomcat目录--> conf目录--> server.xml里找出这段:
<Connector
port= "8080 " maxThreads= "150 " minSpareThreads= "25 " maxSpareThreads= "75 "
enableLookups= "false " redirectPort= "8443 " acceptCount= "100 "
debug= "0 " connectionTimeout= "20000 "
disableUploadTimeout= "true " <!--在里边加上这个参数--> URIEncoding= "gb2312 "
/>