Java多语言编码问题解析(2)

王朝java/jsp·作者佚名  2008-05-31
窄屏简体版  字體: |||超大  

这里是一个具体的例子:

现在因为浏览器对UTF-8的支持,我们可以通过在源文件、请求、响应中都使用unicode编码方式,来轻松达到处理国际化和字符编码问题的目标。

以我们使用的tomcat4.1.2为例,过程如下:

1、编写jsp页面时:在每个JSP页面在页首都要增加一行:

在编辑JSP页面时,一定要确保JSP文件以unicode的方式保存,目前几乎所有的编辑器都有以unicode编码保存或将文件内容转换成unicode的功能。

2、增加一个用来声明request的CharacterEncoding的类SetCharacterEncodingFilter.Java;

SetCharacterEncodingFilter的这个类主要的作用就是:把request在从页面刚提交到server端的时候的encoding声明为我们想要的encoding,通过调用request的方法setCharacterEncoding (String encoding) 来改变,这样可以使request的从客户端传过来的时候,按我们在web.XML (在第二点可以讲到) 中配置的encoding来对提交的数据编码。

3、修改web.xml文件,配置一个filter来过滤全部url请求,通过第二步中的类,声明所有url请求的编码类型未UTF-8。

在web.xml文件中加上以下这段:

Set Character Encoding

org.kyle.web.sample.SetCharacterEncodingFilter

encoding

UTF-8

Set Character Encoding

/*

在上面这段文字中“org.kyle.web.sample.SetCharacterEncodingFilter”指定步骤2中的类的位置,“ UTF-8”指定我们希望声明的request的编码类型,“/*”指定这个filter的适用范围(这里指的是全部url请求)。

同时注重二个问题:

1:servlet的版本必需是支持request.setCharacterEncoding(String encoding)这个方法才行,也就是在serlvert2.3以上。

2:控制面板区域设置的当前代码页属性必需设定为"936 (GBK)",假如是"437(OEM-United States)"它处理文字的时候是8-bit,而中文和日文等是16-bit。所以在显示和处理时它把中文的前8位给截掉,这样就会出现乱码问题。

附:SetCharacterEncodingFilter源文件

package org.kyle.web.sample;

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;

public class SetCharacterEncodingFilter implements Filter

{

/**

* 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;

/**

* 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

*

*encoding

* UTF-8

*

*/

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;

}

/**

* 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

* 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);

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航