浏览器get请求到java后台的值是乱码

JavaScript014

浏览器get请求到java后台的值是乱码,第1张

编码问题首先检查编码和解码是否一致。看下你的jsp是否编码设置了utf-8,默认是iso-8859-1,

然后就是解码的时候的编码,在你的servlet或者controller中检查request和response是否设置了编码,一般是request.setCharacterEncoding("utf-8")response.set...

如果都设置了的话,post发送数据应该是不会乱码的。get发送数据乱码,是因为你的jsp中设置的编码utf-8没有生效在get请求下,解决方案两种:1、修改服务器中的默认编码,

在tomcat安装目录下的conf/server.xml中,有如下的配置:

<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>可以设置端口

这里呢,也可以设置另外一个跟上述编码问题有关的参数信息:URIEncoding,该配置决定了使用get请求通过浏览器地址栏访问tomcat时的编码方式,默认的编码方式使ISO8859-1,这一点我们可以从官网文档(https://tomcat.apache.org/tomcat-7.0-doc/config/http.html) 获悉:

URIEncoding:This specifies the character encoding used to decode the URI bytes, after %xx decoding the URL. If not specified, ISO-8859-1 will be used.

知道了这点,接下来就简单了,我们可以这样配置,则上述代码中,就不需要再从ISO8859-1转为UTF-8了:

URIEncoding="UTF-8"

就是改成这样: <Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" URIEncoding="UTF-8"/>

即可。

或者使用后台手动进行解码:

String s=new String(str.getbyte("iso-8859-1"),"utf-8")

先解码后编码。

如果嫌每次这样麻烦,可以写过滤器拦截你的get清秀将request中的parameter转换编码

===================================================

*************** javaScript 处理中文参数乱码 ***************

encodeURIComponent(txt)方法。

试试将url修改为:

url=<%=basePath %>print.do?method=print&pbrid=" + encodeURIComponent(pbrid)

希望对你有用~~~

===================================================

那不叫乱码,而是url编码,js本身就是读取url编码的

对于js获取url的中文可以尝试用escape() encodeURI() encodeURIComponent() decodeURI()

来使js停止或者转换url编码