当前位置: 代码迷 >> Java Web开发 >> jsp中<a href>传递中文参数乱码有关问题
  详细解决方案

jsp中<a href>传递中文参数乱码有关问题

热度:991   发布时间:2016-04-16 21:54:57.0
jsp中<a href>传递中文参数乱码问题
Jsp中代码为<a href="download.action?inputPath=<s:property value="link"/>">,其中<s:property value="link"/>为中文时在action里无法正确接受,取到的值为一大串问号。。。百度了说用URLEncode没能够解决。。求大神支招。。初学jsp希望能有完整的代码,谢谢
------解决思路----------------------
是不是可以在获取后转一下码,String inputPath =new String(inputPath.getBytes(iso-8859-1),"gb2312");
------解决思路----------------------
String inputPath =new String(<s:property value="link"/>">.getBytes(gb2312),"iso-8859-1")
先转再传,直接传和这个意思一样
<a href="download.action?inputPath=<s:property value="inputPath"/>"> 
收到以后转回来
String inputPath =new String(<s:property value="link"/>">.getBytes(iso-8859-1),"gb2312")
------解决思路----------------------
引用:
提供两个方法:
1. 修改tomcat配置文件 server.xml ,在tomcat目录下的conf中 
找到这段代码:
<Connector 
port="8080" 
protocol="HTTP/1.1" 
         connectionTimeout="20000" 
          redirectPort="8443" 
/>
在其中添加一句 URIEncoding="UTF-8" 
即:
<Connector 
port="8080" 
protocol="HTTP/1.1" 
         connectionTimeout="20000" 
          redirectPort="8443" 
         URIEncoding="UTF-8"
/>
重启服务器 ok

2.前后台进行编码统一
前台改为:<a href="download.action?inputPath=encodeURI(encodeURI(<s:property value="link"/>))">
后台获取:try {
   inputPath=java.net.URLDecoder.decode(inputPath, "UTF-8");
    } catch (UnsupportedEncodingException e1) {
  e1.printStackTrace();
   }

一定主意在前台用两次encodeURI
------解决思路----------------------
现在tomcat服务器加URLEncoding=“utf-8”,如果不行就自己在后台获取值之后进行转吧
String inputPath =new String(inputPath.getBytes(iso-8859-1),"utf-8");
  相关解决方案