1、tomcat服务器配置server.xml文件
主要用户处理与页面交互时产生的乱码问题,例如提交表单等。
<!-- Define a non-SSL HTTP/1.1 Connector on port 80-->
<Connector acceptCount="100" connectionTimeout="20000" disableUploadTimeout="true" enableLookups="false" maxHttpHeaderSize="8192" maxSpareThreads="75" maxThreads="150" minSpareThreads="25" port="80" redirectPort="8443"
URIEncoding="GBK"
useBodyEncodingForURI="true"
/>
URIEncoding:
用来设定通过
URI
传递的内容使用的编码,
tomcat
将使用这里指定的编码对客户端传送的内容进行编码
。我们通过
get
方法提交的参数实际上都是通过
uri
提交的,由这个参数管理
,如果没有设定这个参数,则
tomcat
将使用默认的
iso8859-1
对客户端的内容进行编码。
useBodyEncodingForURI:
使用与
Body
一样的编码来处理
URI,
这个设定是为了与
tomcat4
保持兼容,原来在
tomcat4
和
tomcat5
中队参数的处理是不一样的,在
tomcat4
中
get
与
post
的编码是一样的,所以只要在过滤器中通过
request.setCharacterEncoding
设定一次就可以解决
get
与
post
的问题。然而,在
tomcat5
中,
get
与
post
的处理是分开进行的,对
get
的处理通过前面的
URIEncoding
进行处理,对
post
的内容依然通过
request.setCharacterEncoding
处理,为了保持兼容,就有了这个设定。
所以,设置
URIEncoding
解决
get
中的参数问题,配置过滤器解决
post
的参数问题;或者设置
useBodyEncodingForURI为true
,
get
、
post
都使用过滤器来解决参数问题。
2、数据库bean配置
主要针对与数据库交互时产生的乱码问题,例如插入中文记录或读取中文数据。
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/newfang?useUnicode=true&
characterEncoding=gbk
&autoReconnect=true</value>
</property>
3、代码中局部解决乱码问题
主要用于解决个别乱码问题,例如网页显示时中文均显示正常除一两条语句外,那么针对这一两条语句的乱码问题可用该方法。
qString = new String(qString.
getBytes
("ISO8859_1"), "GBK");
或
qString = URLEncoder.encode(qString, "GBK"); //java url
编码方法
4、web.xml配置,添加filter过滤器
用于处理全站乱码问题,其实主要也是用于
action
与
jsp
页面交互时使用。
<!--
用于解决中文乱码问题
-->
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>
com.qa.util.SetEncodingFilter
</filter-class>
<init-param>
<param-name>
encoding
</param-name>
<param-value>
GBK
</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
SetEncodingFilter
代码:
package com.qa.util;
import javax.servlet.*;
import java.io.*;
public class SetEncodingFilter implements Filter
{
protected String encoding=null;//
定义缺省字符编码方式
protected boolean ignore=true;//
定义客户端指定的编码方式是否应被忽略
protected FilterConfig filterConfig=null;//
定义过滤器配置对象
,
若为
null,
则说明过滤器未配置
public void destroy()//
停止过滤器的工作
{
this.encoding=null;
this.filterConfig=null;
}
//
设置字符编码
public void doFilter(ServletRequest req,ServletResponse res,FilterChain chain)
throws IOException,ServletException
{
if(ignore||(req.getCharacterEncoding()==null))
{
req.
setCharacterEncoding
(selectEncoding(req));
}
chain.doFilter(req,res);
}
//
启动过滤器
public void init(FilterConfig filterConfig)throws ServletException
{
this.filterConfig=filterConfig;
this.encoding=filterConfig.
getInitParameter
("encoding");
String value=filterConfig.getInitParameter("ignore");
if(value==null) this.ignore=true;
else if(value.equalsIgnoreCase("true")
||value.equalsIgnoreCase("yes")) this.ignore=true;
else this.ignore=false;
}
//
选择合适的字符编码方式
protected String selectEncoding(ServletRequest req)
{
return this.encoding;
}
//
返回
filterConfig
对象
public FilterConfig getFilterConfig()
{
return filterConfig;
}
//
设置
filterConfig
对象
public void setFilterConfig(FilterConfig filterConfig)
{
this.filterConfig=filterConfig;
}
}
5、服务器apache上的乱码。
除了以上的情况外,还有
apache
的配置问题,注意的方面有以下几点:
1
)
conf/httpd.conf
把
AddDefaultCharset ISO-8859-1
改成
AddDefaultCharset GBK
2
)
apache
进行了
rewrite
把需要
rewrite
的
url
中的中文参数进行
两次
编码
(encode)
,因为
apache
在
rewrite
时会做一次
url
解码,这时
jk
进行请求转发时,就不会再是编码后的字符串了;
或者在接收请求时先用
ISO-8859-1
取字节流,再使用
UFT-8
来
new String
。
(new String(str.getBytes("ISO-8859-1"),"UFT-8")
)