使用 tomcat 时,相信你们都回遇到中文乱码的问题,具体表现为经过表单取得的中文数据为乱码。javascript
1、初级解决方法
经过一番检索后,许多人采用了以下办法,首先对取得字符串按照 iso8859-1 进行解码转换,而后再按照 gb2312 进行编码,最后获得正确的内容。示例代码以下:java
http://xxx.do?ptname='我是中国人'web
String strPtname = request.getParameter("ptname");tomcat
strPtname = new String(strPtname.getBytes("ISO-8859-1"), "UTF-8");
String para = new String( request.getParameter("para").getBytes("iso8859-1"), "gb2312");
具体的缘由是由于美国人在写 tomcat 时默认使用 iso8859-1 进行编码形成的。
然而,在咱们的 servlet 和 jsp 页面中有大量的参数须要进行传递,这样转换的话会带来大量的转换代码,很是不便。
2、入门级解决方法
后来,你们开始写一个过滤器,在取得客户端传过来的参数以前,经过过滤器首先将取得的参数编码设定为 gb2312 ,而后就能够直接使用 getParameter 取得正确的参数了。这个过滤器在 tomcat 的示例代码
jsp-examples 中有详细的使用示例, 其中过滤器在 web.xml 中的设定以下,示例中使用的是日文的编码,咱们只要修改成 gb2312 便可
view plaincopy to clipboardprint?
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>filters.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>EUC_JP</param-value>
</init-param>
</filter>
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>filters.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>EUC_JP</param-value>
</init-param>
</filter>
服务器
过滤器的代码以下:
view plaincopy to clipboardprint?
public class SetCharacterEncodingFilter implements Filter {
// 编码的字符串
protected String encoding = null;
// 过滤器的配置
protected FilterConfig filterConfig = null;
// 是否忽略客户端的编码
protected boolean ignore = true;
// 销毁过滤器
public void destroy() {
this.encoding = null;
this.filterConfig = null;
}
// 过滤方法
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException {
// 若是使用过滤器,忽略客户端的编码,那么使用经过过滤器设定编码
if (ignore || (request.getCharacterEncoding() == null)) {
String encoding = selectEncoding(request);
if (encoding != null)
request.setCharacterEncoding(encoding);
}
// 传送给下一个过滤器
chain.doFilter(request, response);
}
// 初始化过滤器
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"))
this.ignore = true;
else if (value.equalsIgnoreCase("yes"))
this.ignore = true;
else
this.ignore = false;
}
// 返回过滤器设定的编码
protected String selectEncoding(ServletRequest request) {
return (this.encoding);
}
}
public class SetCharacterEncodingFilter implements Filter {
// 编码的字符串
protected String encoding = null;
// 过滤器的配置
protected FilterConfig filterConfig = null;
// 是否忽略客户端的编码
protected boolean ignore = true;
// 销毁过滤器
public void destroy() {
this.encoding = null;
this.filterConfig = null;
}
// 过滤方法
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException {
// 若是使用过滤器,忽略客户端的编码,那么使用经过过滤器设定编码
if (ignore || (request.getCharacterEncoding() == null)) {
String encoding = selectEncoding(request);
if (encoding != null)
request.setCharacterEncoding(encoding);
}
// 传送给下一个过滤器
chain.doFilter(request, response);
}jsp
// 初始化过滤器
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"))
this.ignore = true;
else if (value.equalsIgnoreCase("yes"))
this.ignore = true;
else
this.ignore = false;
}
// 返回过滤器设定的编码
protected String selectEncoding(ServletRequest request) {
return (this.encoding);
}
}
函数
然而在 tomcat5 中,即便使用过滤器,仍然可能取得乱码,缘由何在呢?
3、高级解决方法
这是由于,在 tomcat4 和 tomcat5 中对参数的处理是不同的,在 tomcat4 中 get 与 post 的编码是同样的,因此只要在过滤器中经过 request.setCharacterEncoding 设定一次就能够解决 get 与 post 的问题。然而,在 tomcat5 中,get 与 post 的处理是分开进行的
在 tomcat 5 中,为了解决编码问题,tomcat 的做者做了不少努力,具体表现为在 tomcat 的配置文件 server.xml 中对 Connector 元素增长了以下的配置参数,专门用来对编码进行直接的配置
URIEncoding 用来设定经过 URI 传递的内容使用的编码,tomcat 将使用这里指定的编码对客户端传送的内容进行编码。
什么是 URI 呢?
java doc 的说明中以下说明:URI 是统一资源标识符,而 URL 是统一资源定位符。所以,笼统地说,每一个 URL 都是 URI,但不必定每一个 URI 都是 URL。这是由于 URI 还包括一个子类,即统一资源名称 (URN),它命名资源但不指定如何定位资源。
也就是说,咱们经过 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 处理,为了保持兼容,就有了这个设定。
将 useBodyEncodingForURI 设定为真后,就能够经过 request.setCharacterEncoding 直接解决 get 和 post 中的乱码问题。
这样,咱们能够经过在 server.xml 中设定 URIEncoding 来解决 get 方法中的参数问题,使用过滤器来解决 post 方法中的问题。
或者也能够经过在 server.xml 中设定 useBodyEncodingForURI 为 true ,配合过滤器来解决编码的问题。
在这里,我强烈建议在网站的创做过程当中,全程使用 utf-8 编码来完全解决乱码问题。
具体操做以下:
一、页面内容使用 utf-8 格式保存,在页面中加入 <mete http-equiv="contentType" content="textml;charst=utf-8">
二、服务器端的 server.xml 中设定 useBodyEncodingForURI = true
三、使用过滤器,过滤器设定编码为 utf-8post
四:若是有一些转码也转不过来的话,但是试试打开tomcat的server.xml,找到网站
<Connector acceptCount="100" connectionTimeout="20000" disableUploadTimeout="true" port="80" redirectPort="8443"> ui
并在最后加上useBodyEncodingForURI="true" URIEncoding="UTF-8",以下
<Connector acceptCount="100" connectionTimeout="20000" disableUploadTimeout="true" port="80" redirectPort="8443" useBodyEncodingForURI="true" URIEncoding="UTF-8">
五:
若是用jstl的话,能够本身写一个el的function,调用URLEncoder.encode来编码。
IE缺省对URL后面的参数是不编码发送的,可是tomat缺省是按ISO8859-1来进行URL解码,所以才会出现上述错误。好的作法是:
一、在URL参数中确保用UTF-8编码之,方法能够用js函数encodeURI(),或调用自定义的el function;
二、设置server.xml中的Connector熟悉URIEncoding="UTF-8",确保解码格式与编码格式统一;
方法四:
view plaincopy to clipboardprint?
<mce:script type="text/javascript"><!--
for(var i=0;i<document.links.length;i++){
document.links[i].href=encodeURI(document.links[i].href);
}
// --></mce:script>
<mce:script type="text/javascript"><!--
for(var i=0;i<document.links.length;i++){
document.links[i].href=encodeURI(document.links[i].href);
}
// --></mce:script>
在action中,String s=request.getParameter("s");
s=new String(s.getBytes("iso-8859-1"),"gbk");
六:js的乱码解决
1.客户端:
url=encodeURI(url);
服务器:
String linename = new String(request.getParameter("name").getBytes("ISO-8859-1"),"UTF-8");
2.客户端:
url=encodeURI(encodeURI(url)); //用了2次encodeURI
服务器:
String linename = request.getParameter(name);
//java : 字符解码
linename = java.net.URLDecoder.decode(linename , "UTF-8");
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/jinxinxin1314/archive/2009/08/16/4453390.aspx