【java】怎样解决tomcat中get提交中文参数为乱码的问题
2012-11-02 14:38 Loull 阅读(834) 评论(0) 编辑 收藏 举报详解:
http://www.360doc.com/content/10/0815/14/2736180_46209475.shtml
老是碰到中文问题,再解决一小点。
http://topic.csdn.net/t/20061230/16/5267105.html
http://www.cnblogs.com/maxupeng/archive/2010/11/26/1889258.html
这次碰到的问题是,浏览器把url的get参数值编码为GBK了,而我在tomcat的serve.xml文件中设置的是URIEncoding=“UTF-8”
问题:看这样一个URI
http://localhost:8080/ZNServer/getDoctorInfoByIllName?illName=心脏病
看过一篇文章说,url部分都是用utf8编码的,而后面的参数值——心脏病 的编码非常不确定,跟操作系统环境、浏览器本身、网页编码有关系。而后台get方法拿到的都会是乱码,就算确定知道前台传过来的是GBK编码。
request.setCharacterEncoding()只能解决post方式提交的中文数据, 而get方法好象我如何该都是乱码,后来改了server.xml里的URLEncoding属性后才好
一、加一个过滤器
package asatapp
import javax.servlet.Filter;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.FilterChain;
import java.io.IOException;
import javax.servlet.http.*;
public class EncodingFilter implements Filter {
private String encoding;
public EncodingFilter() {
}
public void init(FilterConfig fconfig) throws ServletException {
encoding = fconfig.getInitParameter("charset ");
}
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain fchain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
request.setCharacterEncoding(encoding);
fchain.doFilter(req, resp);
}
public void destroy() {
}
}
WEB.XML中
<filter>
<filter-name> encoding </filter-name>
<filter-class> asatapp.EncodingFilter </filter-class>
<init-param>
<param-name> charset </param-name>
<param-value> gb2312 </param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name> encoding </filter-name>
<url-pattern> /* </url-pattern>
</filter-mapping>
同学做的一个项目中,也是用过滤器解决的。不过他们做的get请求基本都没有中文,不知道是否有效。
二、我在web.xml中配置spring的过滤器了,但是无效
<!-- 字符过滤器 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
三、还是配置tomcat的server.xml文件,有效
<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"
URIEncoding="UTF-8"/>
加了URIEncoding="UTF-8"
四、有人提出字符转换函数,自己试了下,无效
public String toGBK(String str){ try { byte bt[] = str.getBytes( "ISO-8859-1 "); str = new String(bt); } catch (Exception ex) { } return str; }
最后、解释
http://lonvea.iteye.com/blog/1392492
tomcat server.xml中有2个参数是配置编码的,一个是URIEncoding,另一个是useBodyEncodingForURI.
apache tomcat官方网站对这两个参数的解释:
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. This setting is present for compatibility with Tomcat 4.1.x, where the encoding specified in the contentType, or explicitly set using Request.setCharacterEncoding method was also used for the parameters from the URL. The default value is false
.
如果http请求中,在contentType中指定了编码方式,那么将采用contentType的编码方式去解码url之后的查询参数,将忽略URIEncoding的配置.
所以在get请求时,如果contentType指定了编码方式,将会带来一定的问题(最大的问题就是,服务器端对查询字符串的编码方式无法统一),最后我决定让 useBodyEncodingForURI 参数保持默认(false):即所有的URL查询字符串的编码采用URIEncoding的参数(UTF-8),服务器端编码保持原来的统一方式.