Tomcat 中文乱码 设置UTF-8编码 问题解决办法

在Java Web开发中,http请求带有中文字符的URI如果不处理容易出现乱码问题;这是因为Tomcat容器默认编码是iso-8859-1引起的,因此要避免出现乱码就要需要做相应的处理。解决办法如下:

一、在tomcat的 server.xml中设置

打开server.xml文件,对文件中设置如下:

在HTTP/1.1中增加URIEncoding="utf-8;

<Connector port="8098" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" URIEncoding="utf-8"/>

---------------------------------------------------------------------------

...

<!-- A "Connector" represents an endpoint by which requests are received
and responses are returned. Documentation at :
Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
Java AJP Connector: /docs/config/ajp.html
APR (HTTP/AJP) Connector: /docs/apr.html
Define a non-SSL HTTP/1.1 Connector on port 8080
-->
<Connector port="8098" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" URIEncoding="utf-8"/>
<!-- A "Connector" using the shared thread pool-->
<!--
<Connector executor="tomcatThreadPool"
port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
-->
<!-- Define a SSL HTTP/1.1 Connector on port 8443
This connector uses the BIO implementation that requires the JSSE
style configuration. When using the APR/native implementation, the
OpenSSL style configuration is required as described in the APR/native
documentation -->
<!--
<Connector port="8443" protocol="org.apache.coyote.http11.Http11Protocol"
maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS" />
-->

...

...

另外,若是使用的其它的容器,只需确认容器的默认编码,做相应的设置即可。

二、配置servlet 可在服务程序中添加拦截器,当然也可用其它方式设置

示例:

public class CommonInterceptor implements Interceptor {
public void intercept(Invocation inv) {
Controller c=inv.getController();


try {
// 设置Request中汉字的编码,该方法只对post请求有效,对get请求无效;

// 对于get请求,应该在server.xml中指定:URIEncoding=utf-8;

c.getRequest().setCharacterEncoding("utf-8");

c.getResponse().setCharacterEncoding("utf-8");

inv.invoke();
} catch (Exception e) {
e.printStackTrace();
}

}

}

三、servlet ---- response.setCharacter和request.setCharacterEncoding详解

1、request.setCharacterEncoding():用来确保发往服务器的参数以汉字的编码来提取,设置从request中取得的值或从数据库中取出的值。

指定后可以通过request.getParameter()获取自己想要的字符串,如果没有提前指定,则会按照服务器端默认的“iso-8859-1”来进行编码;该方法只对post请求有效,对get请求无效;对于get请求,应该在server.xml中指定:URIEncoding=utf-8;

注意:在执行request.setCharacterEncoding()之前不能执行request.getParameter()方法;

原因:应该是在执行第一个getParameter()的时候,java将会按照编码分析所有的提交内容,而后续的getParameter()不再进行分析,所以setCharacterEncoding()无效。而对于GET方法提交表单是,提交的内容在URL中,一开始就已经按照编码分析提交内容,setCharacterEncoding()自然就无效。

2、response.setCharacterEncoding():设置HTTP 响应的编码,用于设置服务器给客户端的数据的编码

一般不会用这个方法来设置响应编码,

一般使用response.setContentType()方法来设置HTTP 响应的编码,同时指定了浏览器显示的编码;

因为他在执行该方法通知服务器端以指定编码进行编码后,会自动调用response.setCharacterEncoding()方法来通知浏览器以指定编码来解码;使用此方法要在response.getWriter()执行之前或response提交之前;

四、如果确实是要处理get请求

可在参数获取时作转码处理

String string = request.getParamers("");
String = new String(string.getBytes("ISO8859-1","utf-8"));

提示:如果已经在tomcat的 server.xml中设置了,可以不对servlet再作配置,但为了稳妥起见(避免忘记配置tomcat),最好也在代码中对servlet作下设置。

猜你喜欢

转载自www.cnblogs.com/panchanggui/p/9431975.html