当我们在linux上发布J2ee项目时,经常会碰到让人头疼的乱码问题;根据自己的经历总结有三种常见的问题和原因:
首先,一种情况是,jsp页面你已经设置了编码集为GBK,但个别地方中文(通过传参数过来的得到)为乱码、这个的解决办法就是对于request通过get得到的内容进行编码转换。但这个方法比较累。常用的办法就是写一个过滤器类。
import java.io.IOException; import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse; /**
* @author zhangpeng *
*/
public class EncodeFilter implements Filter { private String encoding = null;
private FilterConfig config = null;
private boolean isUsed; /*
* @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
*/
public void init(FilterConfig arg0) throws ServletException {
this.config = arg0;
this.encoding = arg0.getInitParameter("encoding");
String strUsed = arg0.getInitParameter("isUsed");
if (strUsed != null && strUsed.equalsIgnoreCase("true")) {
this.isUsed = true;
} else {
this.isUsed = false;
}
} /*
*
* @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest,
* javax.servlet.ServletResponse, javax.servlet.FilterChain)
*/
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
if ((this.isUsed || arg0.getCharacterEncoding() == null)
&& this.encoding != null) {
arg0.setCharacterEncoding(this.encoding);
} arg2.doFilter(arg0, arg1); } /*
*
* @see javax.servlet.Filter#destroy()
*/
public void destroy() {
this.config = null;
this.encoding = null;
} }
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse; /**
* @author zhangpeng *
*/
public class EncodeFilter implements Filter { private String encoding = null;
private FilterConfig config = null;
private boolean isUsed; /*
* @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
*/
public void init(FilterConfig arg0) throws ServletException {
this.config = arg0;
this.encoding = arg0.getInitParameter("encoding");
String strUsed = arg0.getInitParameter("isUsed");
if (strUsed != null && strUsed.equalsIgnoreCase("true")) {
this.isUsed = true;
} else {
this.isUsed = false;
}
} /*
*
* @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest,
* javax.servlet.ServletResponse, javax.servlet.FilterChain)
*/
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
if ((this.isUsed || arg0.getCharacterEncoding() == null)
&& this.encoding != null) {
arg0.setCharacterEncoding(this.encoding);
} arg2.doFilter(arg0, arg1); } /*
*
* @see javax.servlet.Filter#destroy()
*/
public void destroy() {
this.config = null;
this.encoding = null;
} }
再配置web.xml文件;
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>com.et.filter.EncodeFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>gbk</param-value>
</init-param>
<init-param>
<param-name>isUsed</param-name>
<param-value>true</param-value>
</init-param>
</filter> 这样所有对于因为request传参和得到参数而出现乱码的问题都会得到解决
<filter-name>Set Character Encoding</filter-name>
<filter-class>com.et.filter.EncodeFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>gbk</param-value>
</init-param>
<init-param>
<param-name>isUsed</param-name>
<param-value>true</param-value>
</init-param>
</filter> 这样所有对于因为request传参和得到参数而出现乱码的问题都会得到解决
其次:上边的问题排除后,还有乱码问题(常出现在页面信息等),这就需要检查你的服务器配置的编码。/conf/server.xml中加上或者修改为
<Connector port="80"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
debug="0" connectionTimeout="20000"
disableUploadTimeout="true" URIEncoding="GBK" />加上粗体的部分;
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
debug="0" connectionTimeout="20000"
disableUploadTimeout="true" URIEncoding="GBK" />加上粗体的部分;
再者。以上的你都做到了但是仍有乱码(多见静态页面,jsp中包含进来静态页面中出现乱码,从页面得到的内容在插到数据库前已经乱码),这时你可以写上一个测试代码在类中System.out.println("========="+System.getProperty("file.encoding"));或者页面上<%=System.getProperty("file.encoding")%>都可以:查看一下系统的编码集是不是GB18030如果不是,打开/etc/sysconfig/i18n文件改为 LANG="zh_CN.GB18030"
LANGUAGE="zh_CN.GB18030:zh_CN.GB2312:zh_CN"
SUPPORTED="zh_CN.GB18030:zh_CN:zh"
SYSFONT="lat0-sun16"
SYSFONTACM="8859-15"
LANGUAGE="zh_CN.GB18030:zh_CN.GB2312:zh_CN"
SUPPORTED="zh_CN.GB18030:zh_CN:zh"
SYSFONT="lat0-sun16"
SYSFONTACM="8859-15"
最后:上边的乱码情况,都是在你没插入数据库前中文就已经乱码的情况和解决办法。如果上边所有你都确信做到了且经过测试插入数据库前已经没有乱码问题了。但是插入数据库后显示出来的又是乱码,那就需要改你的数据库中的配置文件中的编码集了
。