1.修改web.xml
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8" useBodyEncodingForURI="true"/>
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" URIEncoding="UTF-8" />
2.页面单独转码
传参时:action=login?username=encodeURI(encodeURI("+小明+")); //encodeURI转码一次或两次 此处转码两次
接收参数时:String username = URLDecoder.decode(request.getParameter("username"),"UTF-8");
转载:http://blog.csdn.net/superch0054/article/details/9325793
大家知道tomcat5.0开始,对网页的中文字符的post或者get,经常会出现乱码现象。
具体是因为Tomcat默认是按ISO-8859-1进行URL解码,ISO-8859-1并未包括中文字符,这样的话中文字符肯定就不能被正确解析了。
常见的解决方法是在tomcat的server.xml下的connetor属性中增加URIEncoding或者useBodyEncodingForURI属性。
但是,这两种方式有什么区别呢?
我简单谈一下自己的理解:
按照tomcat-docs/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.
useBodyEncodingForURI:This specifies if the encoding specified in contentType should be used for URI query parameters, instead of using the URIEncoding.
也就是说,
useBodyEncodingForURI参数表示是否用request.setCharacterEncoding
参数对URL提交的数据和表单中GET方式提交的数据进行重新编码,在默认情况下,该参数为false。
URIEncoding参数指定对所有GET方式请求进行统一的重新编码(解码)的编码。
URIEncoding和useBodyEncodingForURI区别是,
URIEncoding是对所有GET方式的请求的数据进行统一的重新编码,
而useBodyEncodingForURI则是根据响应该请求的页面的request.setCharacterEncoding参数对数据进行的重新编码,不同的页面可以有不同的重新编码的编码