url中文乱码

 

背景:在JAVA类中输出一个URL,此URL中含有参数&UserName=刘德华

 

然后JSP中用request.getParameter('UserName')时,刘德华变成乱码。

 

剖析:在用GET方式传送时,默认用的是ISO8859-1。

而且tomcat在接受时也是默认用的ISO8859-1。

 

解决:在JAVA类中必须对刘德华进行转码,转成UTF8,如下:

 

"&nickName="+URLEncoder.encode(nickName, "UTF-8");

 

然后在JSP中接受旱,先用ISO8859-1解码得到字节,再拼接成UTF-8。

 

代码如下:

<input type="text" name="nickName" value="<%= new String(request.getParameter("nickName").getBytes("ISO-8859-1"),"utf-8") %>"  placeholder="用户昵称" required="required" maxlength="20"  readonly="readonly">

 

 

refurl:http://blog.csdn.net/jinxinxin1314/article/details/4453390

 

 

 

 

 

 

 

你可能感兴趣的:(中文乱码)