J2EE 中文乱码攻略[zz]
一.GBK与UTF8的比较 GBK的文字编码是双字节来表示的,即不论中、英文字符均使用双字节来表示,只不过为区分中文,将其最高位都定成1。 至于UTF-8编码则是用以解决国际上字符的一种多字节编码,它对英文使用8位(即一个字节),中文使用24位(三个字节)来编码。对于英文字符较多的论坛则用UTF-8节省空间。 GBK包含全部中文字符;
UTF-8则包含全世界所有国家需要用到的字符。 GBK是在国家标准GB2312基础上扩容后兼容GB2312的标准(好像还不是国家标准)
UTF-8编码的文字可以在各国各种支持UTF8字符集的浏览器上显示。
比如,如果是UTF8编码,则在外国人的英文IE上也能显示中文,而无需他们下载IE的中文语言支持包。 所以,对于英文比较多的论坛 ,使用GBK则每个字符占用2个字节,而使用UTF-8英文却只占一个字节。 UTF8是国际编码,它的通用性比较好,外国人也可以浏览论坛
GBK是国家编码,通用性比UTF8差,不过UTF8占用的数据库比GBK大~ 对于DZ论坛来说,很多插件都只支持GBK的,如果需要装较多插件的论坛还是用GBK比较好,而对装较少插件且有特殊用户群的论坛用UTF8比较好。 二.为什么选择UTF8,原因有2个: 1.因为要国际化嘛,选择UTF8可以很好地兼容其他国家语言,对台湾的繁体字也能很好地支持,GBK就比较难 2.数据库的可移植性,大部分数据库对UTF8都有很好地支持 三.需要统一字符集的地方 1.首先开发工具,如MyEclipse,JB,Eclipse等,都可设置编辑的字符集 2.众所周知的页面字符集设置 在JSP头部声明:
<%@ page contentType="text/html;charset= UTF-8" %>
在Jsp的html代码中,声明UTF-8:
<META http-equiv="Content-Type" CONTENT="text/html; charset=utf-8">
3.过滤器
复制内容到剪贴板
代码:
public class TianYaActionServlet extends ActionServlet {
private static final long serialVersionUID = 1L;
protected void process(HttpServletRequest request,
HttpServletResponse response) throws java.io.IOException,
javax.servlet.ServletException {
// TODO Override this org.apache.struts.action.ActionServlet method
request.setCharacterEncoding("UTF-8");
super.process(request, response);
}
}
wml配置
复制内容到剪贴板
代码:
<servlet-class>
com.TianYa.common.util.TianYaActionServlet
</servlet-class>
这样就不用总写request.setCharacterEncoding("UTF-8"); 4.如果个别需要转为其它字符集的用new String(yourString.getBytes("ISO-8859-1"),"UTF-8"); 5.容器设置,如Tomcat 这里要注意form表单的method,Post与get字符集编码可能不同,Tomcat5.0以下的是相同的,以上的是不同的 Post可以通过过滤器设置 get一般是URL传参: Tomcat 5.0.28 - 发布目录 webapps 修改conf/server.xml文件中的 <Connector port="8080" .... /> 增加对GET方法获取数据时的编码设置参数 URIEncoding='GBK' <Connector port="8080" .... URIEncoding='GBK'/> JBOSS 4.0.2 - 发布目录 server/default/deploy (后缀.war) 修改server/default/deploy/jbossweb-tomcat55.sar/server.xml文件中的 <Connector port="8080" .... /> 增加对GET方法获取数据时的编码设置参数 URIEncoding='GBK' <Connector port="8080" .... URIEncoding='GBK'/> 7.数据库字符集设置 不同数据库字符集设置不同,我这就不一一介绍了 mySQL可以连接数据库的URL设定jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8
http://looxiaohu.javaeye.com/blog/203315
唐山迪锐软件IT论坛 http://bbs.tsp2c.cn/?fromuid=136
一.GBK与UTF8的比较 GBK的文字编码是双字节来表示的,即不论中、英文字符均使用双字节来表示,只不过为区分中文,将其最高位都定成1。 至于UTF-8编码则是用以解决国际上字符的一种多字节编码,它对英文使用8位(即一个字节),中文使用24位(三个字节)来编码。对于英文字符较多的论坛则用UTF-8节省空间。 GBK包含全部中文字符;
UTF-8则包含全世界所有国家需要用到的字符。 GBK是在国家标准GB2312基础上扩容后兼容GB2312的标准(好像还不是国家标准)
UTF-8编码的文字可以在各国各种支持UTF8字符集的浏览器上显示。
比如,如果是UTF8编码,则在外国人的英文IE上也能显示中文,而无需他们下载IE的中文语言支持包。 所以,对于英文比较多的论坛 ,使用GBK则每个字符占用2个字节,而使用UTF-8英文却只占一个字节。 UTF8是国际编码,它的通用性比较好,外国人也可以浏览论坛
GBK是国家编码,通用性比UTF8差,不过UTF8占用的数据库比GBK大~ 对于DZ论坛来说,很多插件都只支持GBK的,如果需要装较多插件的论坛还是用GBK比较好,而对装较少插件且有特殊用户群的论坛用UTF8比较好。 二.为什么选择UTF8,原因有2个: 1.因为要国际化嘛,选择UTF8可以很好地兼容其他国家语言,对台湾的繁体字也能很好地支持,GBK就比较难 2.数据库的可移植性,大部分数据库对UTF8都有很好地支持 三.需要统一字符集的地方 1.首先开发工具,如MyEclipse,JB,Eclipse等,都可设置编辑的字符集 2.众所周知的页面字符集设置 在JSP头部声明:
<%@ page contentType="text/html;charset= UTF-8" %>
在Jsp的html代码中,声明UTF-8:
<META http-equiv="Content-Type" CONTENT="text/html; charset=utf-8">
3.过滤器
复制内容到剪贴板
代码:
public class TianYaActionServlet extends ActionServlet {
private static final long serialVersionUID = 1L;
protected void process(HttpServletRequest request,
HttpServletResponse response) throws java.io.IOException,
javax.servlet.ServletException {
// TODO Override this org.apache.struts.action.ActionServlet method
request.setCharacterEncoding("UTF-8");
super.process(request, response);
}
}
wml配置
复制内容到剪贴板
代码:
<servlet-class>
com.TianYa.common.util.TianYaActionServlet
</servlet-class>
这样就不用总写request.setCharacterEncoding("UTF-8"); 4.如果个别需要转为其它字符集的用new String(yourString.getBytes("ISO-8859-1"),"UTF-8"); 5.容器设置,如Tomcat 这里要注意form表单的method,Post与get字符集编码可能不同,Tomcat5.0以下的是相同的,以上的是不同的 Post可以通过过滤器设置 get一般是URL传参: Tomcat 5.0.28 - 发布目录 webapps 修改conf/server.xml文件中的 <Connector port="8080" .... /> 增加对GET方法获取数据时的编码设置参数 URIEncoding='GBK' <Connector port="8080" .... URIEncoding='GBK'/> JBOSS 4.0.2 - 发布目录 server/default/deploy (后缀.war) 修改server/default/deploy/jbossweb-tomcat55.sar/server.xml文件中的 <Connector port="8080" .... /> 增加对GET方法获取数据时的编码设置参数 URIEncoding='GBK' <Connector port="8080" .... URIEncoding='GBK'/> 7.数据库字符集设置 不同数据库字符集设置不同,我这就不一一介绍了 mySQL可以连接数据库的URL设定jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8
http://looxiaohu.javaeye.com/blog/203315
唐山迪锐软件IT论坛 http://bbs.tsp2c.cn/?fromuid=136
解决方案 »
- struts2和jquery实现ajax的一个疑问
- Servlet.service() for servlet dwr-invoker threw exception错误
- 关于hibernate的批量处理操作
- spring + jruby 报错
- JavaScript判断
- web service 响应很慢
- rich:dataTable 中提交
- 请问如何在<html:submit>标签的value属性里显示资源文件里的内容啊?
- JBoss是免费的吗?
- hibernate对象查询工具的问题。在线等答案??
- 如何做到像spring一样获得XML
- spring注入的问题,myeclipse提示没有set方法
1.在src下新建一个filter文件夹,在里面定义一个类名叫SetCharacterEncodingFilter
package filters;import java.io.IOException;
import java.util.Iterator;
import java.util.Map;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;
import javax.servlet.http.HttpServletRequest;/**
* <p>
* Example filter that sets the character encoding to be used in parsing the
* incoming request, either unconditionally or only if the client did not
* specify a character encoding. Configuration of this filter is based on the
* following initialization parameters:
* </p>
* <ul>
* <li><strong>encoding</strong> - The character encoding to be configured for
* this request, either conditionally or unconditionally based on the
* <code>ignore</code> initialization parameter. This parameter is required,
* so there is no default.</li>
* <li><strong>ignore</strong> - If set to "true", any character encoding
* specified by the client is ignored, and the value returned by the
* <code>selectEncoding()</code> method is set. If set to "false,
* <code>selectEncoding()</code> is called <strong>only</strong> if the
* client has not already specified an encoding. By default, this parameter is
* set to "true".</li>
* </ul>
*
* <p>
* Although this filter can be used unchanged, it is also easy to subclass it
* and make the <code>selectEncoding()</code> method more intelligent about
* what encoding to choose, based on characteristics of the incoming request
* (such as the values of the <code>Accept-Language</code> and
* <code>User-Agent</code> headers, or a value stashed in the current user's
* session.
* </p>
*
* @author Craig McClanahan
* @version $Revision: 466607 $ $Date: 2006-10-21 17:09:50 -0600 (Sat, 21 Oct
* 2006) $
*/public class SetCharacterEncodingFilter implements Filter { // ----------------------------------------------------- Instance Variables /**
* The default character encoding to set for requests that pass through this
* filter.
*/
protected String encoding = null; /**
* The filter configuration object we are associated with. If this value is
* null, this filter instance is not currently configured.
*/
protected FilterConfig filterConfig = null; /**
* Should a character encoding specified by the client be ignored?
*/
protected boolean ignore = true; // --------------------------------------------------------- Public Methods /**
* Take this filter out of service.
*/
public void destroy() { this.encoding = null;
this.filterConfig = null; } /**
* Select and set (if specified) the character encoding to be used to
* interpret request parameters for this request.
*
* @param request
* The servlet request we are processing
* @param result
* The servlet response we are creating
* @param chain
* The filter chain we are processing
*
* @exception IOException
* if an input/output error occurs
* @exception ServletException
* if a servlet error occurs
*/
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, 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);
} // Pass control on to the next filter
chain.doFilter(request, response); } /**
* Place this filter into service.
*
* @param filterConfig
* The filter configuration object
*/
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 Methods /**
* Select an appropriate character encoding to be used, based on the
* characteristics of the current request and/or filter initialization
* parameters. If no character encoding should be set, return
* <code>null</code>.
* <p>
* The default implementation unconditionally returns the value configured
* by the <strong>encoding</strong> initialization parameter for this
* filter.
*
* @param request
* The servlet request we are processing
*/
protected String selectEncoding(ServletRequest request) { return (this.encoding); }}2.在web.xml进行配置
<!-- 编码过滤器 -->
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>
filter.SetCharacterEncodingFilter//该类的路径
</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>