快捷搜索:  as  2018  FtCWSyGV  С˵  test  xxx  Ψһ  w3viyKQx

澳门新葡萄app下载_龟发之家论坛



国际化的器械带来的问题还真的很多多少,各国说话不合,所应用的字符集都不一样,JAVA,Mysql,Tomcat,浏览器等等用的字符集也不一样,这几气象得我都说了好几回不用什么Struts,Mysql,Tomcat了,整个都是自己写出来好了,用统一的编码统一的字符集,可惜能力不敷,说说而已,问题照样得办理。在网上查了良久,自己也实践了很多多少天,问题终于算是办理了。

由于要斟酌到很多国的说话,一开始就把项目容身于国际化,在把连接要领从Struts连接池改为Tomcat的连接池之后,原本我碰到的乱码有

1、资本文件里读出来在页面上的乱码;

2、数据库读出来的乱码

3、数据库写进去的乱码

4、在ActionForm验证不经由过程Errors返回的乱码,也便是request,IE参数通报的乱码了。

5、有一个JS写的光阴选择跳出框,假如在charset=UTF-8的时刻就掉足,但假如我改为charset=GBK或GB2312时就统统正常了,很稀罕的征象,我还找不出谜底来。

哇,很多多少的乱码。。。。头都大年夜了好几天。

下面是我的办理措施

1、资本文件里读出来在页面上的乱码:这个最轻易办理了,把写好的ApplicationResources.properties文件,在DOC底下用 native2ascii -encoding gb2312 ApplicationResources.properties

ApplicationResources_zh_CN.properties 敕令来个字符编码转换,将原本的中文转为Unicode编码就搞定了中文简体,繁体也用同样的敕令,只是把 bg2312 改为 bgk 就可以了。

2、数据库读写的乱码,刚开始的时刻由于受曩昔的SQL Server+JDBC 影响(在那时写入数据库是可以不用做什么事情的,只是在读出来的时刻来个 gbk = new String(iso.getBytes("ISO-8859-1"), "GBK") 转换就行了)我也都在把读写都在转换,搞得好繁杂也很麻烦,后来在连接池连接代码jdbc:mysql://localhost:3306/database?autoReconnect=true&useUnicode=true&characterEncoding=GBK那里加上一个&useUnicode=true&characterEncoding=GBK,包管了在数据库操作时刻应用了统一的编码字符集,又办理了两个乱码问题,一石二鸟,嘿嘿。

4、request,response的乱码在网上找了良久,也有两个办理的法子,也是来个转换,还有种法子是写一个过滤器,我选择了后者,由于简单:),这措施用到两个文件,一个是 filter ,一个是 web.xml 文件,代码在后面。

5、至于JS的这个问题,还没法子,只好在JSP页面上改为了,反正这样也没问题。

到此为止,乱码问题总算告一段落了,感到蛮不错的,愁闷了这么久,终于可以痛快了好大年夜一段子了。

-------------------------------------------------------------------------------------------------------------------

过滤器应用代码:EncodingFilter.java

package com.奸淫.奸淫.奸淫;

import javax.servlet.*;

import java.io.IOException;

/**

*

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澳门新葡萄app下载 filter is based on

* the following initialization parameters:

*

*

encoding - The c澳门新葡萄app下载haracter encoding to be configured

*for this request, either conditionally or unconditionally based on

*the ignore initialization parameter.This parameter

*is required, so there is no default.

*

ignore - If set to "true", any character encoding

*specified by the client is ignored, and the value returned by the

*selectEncoding() method is set.If set to "false,

*selectEncoding() is called only if the

*client has not already specified an encoding.By default, this

*parameter is set to "true".

*

*

*

Although this filter can be used unchanged, it is also easy to

* subclass it and make the selectEncoding() method more

* intelligent about what encoding to choose, based on characteristics of

* the incoming request (such as the values of the Accept-Language

* and User-Agent headers, or a value stashed in the current

* user´s session.

*

*/

public class EncodingFilter 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

*/

publ澳门新葡萄app下载ic 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.equalsIgnor澳门新葡萄app下载eCase("yes"))

this.ignore = true;

else

this.ignore = false;

}

// ------------------------------------------------------ Protected Methods

/**

* Select an a澳门新葡萄app下载ppropriate 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

* null.

*

* The default implementation unconditionally returns the value configured

* by the encoding initialization parameter for this

* filter.

*

* @param request The servlet request we are processing

*/

protected String selectEncoding(ServletRequest request) {

return (this.encoding);

}

}//EOC

WEB.XML文件:将下面的代码放在WEB.xml文件的下面就行了

encodingFilter

EncodingFilter

Set the request encoding

com.voip.admin.util.EncodingFilter

encoding

GB18030

ignore

true

encodingFilter

/*

您可能还会对下面的文章感兴趣: