在实际的Java Web项目开发过程中,经常会遇到中文乱码的问题,那么,今天我们就来分析一下项目中出现中文乱码的原因以及解决办法,因为出现乱码的方式有好几种,我简单总结一下吧,为以后留着用,也算总结学习一下。
一、文件的乱码
1、项目文本文件默认编码:【右击项目】->【Properties】->【Resource】->【Text file encoding】
2、文件默认编码:默认使用项目的默认编码:【右击文件】->【Properties】->【Resource】->【Text file encoding】
3、 JSP 文件编码:【右击文件】->【Properties】->【Resource】->【Text file encoding】
4、JSP翻译为Servlet时的编码:
<%@ page language="java" pageEncoding="utf-8"%>
二、浏览器和服务器间传输数据的乱码
1、 浏览器端编码,一般由服务器端告诉浏览器如何解码数据:
Servlet
response.setContentType("text/html; charset=UTF-8");
//或者
response.setCharacterEncoding("UTF-8");
JSP
<%@ page language="java" contentType="text/html; charset=utf-8" %>
通用html
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
注:浏览器编码可以由我们手工修改,但最好不要这样,一般让浏览器自动选择即可。
2、浏览器和服务器间传输数据的乱码
服务器端编码,将客户端传过来的数据进行解码:浏览器默认使用ISO-8859-1进行编码数据,然后将数据传输到服务器,因此我们默认只需要将浏览器发送过来的数据转换为我们需要的编码即可。
最简单方式:
String username = request.getParameter("username");
username = new String(username.getBytes("ISO-8859-1"), "UTF-8");
比较好的解决方案:
// 必须在获取参数之前,调用如下方法先解码
request.setCharacterEncoding("UTF-8");
String username = request.getParameter("username");
比较通用方案:在一个Filter中更改所有请求的编码方式:
在src中添加filter来设置编码格式是中文,filter类可以在tomcat的包里面:apache-tomcat-6.0.16.zip\apache-tomcat-6.0.16\webapps\examples\WEB-INF\classes\filters下面,添加SetCharacterEncodingFilter.java文件,SetCharacterEncodingFilter.java文件代码如下:
package filters;
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;
import javax.servlet.UnavailableException;
publicclass SetCharacterEncodingFilter implementsFilter {
/**
* The default character encoding to set for requests that pass through
* this filter.
*/
protectedString encoding = null;
/**
* The filter configuration object we are associated with. If this value
* is null, this filter instance is not currently configured.
*/
protectedFilterConfig filterConfig = null;
/**
* Should a character encoding specified by the client be ignored?
*/
protectedboolean ignore = true;
publicvoid destroy() {
this.encoding = null;
this.filterConfig = null;
}
publicvoid doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throwsIOException, ServletException {
// Conditionally select and set the character encoding to be used
if(ignore || (request.getCharacterEncoding() == null)) {
String encoding = selectEncoding(request);
if(encoding != null)
request.setCharacterEncoding(encoding);
}
chain.doFilter(request, response);
}
/**
* Place this filter into service.
* @param filterConfig The filter configuration object
*/
publicvoid init(FilterConfig filterConfig) throwsServletException {
this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
String value = filterConfig.getInitParameter("ignore");
if(value == null)
this.ignore = true;
elseif (value.equalsIgnoreCase("true"))
this.ignore = true;
elseif (value.equalsIgnoreCase("yes"))
this.ignore = true;
else
this.ignore = false;
}
protectedString selectEncoding(ServletRequest request) {
return(this.encoding);
}
}
在 web.xml 中配置 filter ,具体配置如下页:
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>filters.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
通过修改上面的方法只能解决POST提交方式的请求编码,对GET无效。那么,GET提交方式中文乱码解决方法如下。
TOMCAT默认ISO-8859-1 因此可以设置默认编码为UTF-8解决,在conf\server.xml文件中设置如下
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" URIEncoding="UTF-8"/>
转载自: http://www.csyor.com/622.html