科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航



ZDNet>软件频道>中间件-zhiding>Java多语言编码问题解析

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

本文介绍我们在编译时,如何注意把源文件用的是什么编码方式正确告诉Java编译器的问题,供大家参考!

来源:赛迪网 2007年11月18日

关键字:解析 编码 多语言 java

1、Java编译器在对源文件编译前,会先把源文件转换为unicode编码,因为这个原因,我们在编译时一定要把源文件用的是什么编码方式正确无误的“告诉”编译器。

例如:我们的源文件是以UTF-8的方式保存的,而在编译时编译器却把它当作是用GBK方式保存的,这样编译器就会按照GBK->Unicode的编码转换方法对源文件进行转换,然后再编译,这样当然会出错,实际上编译器应当按照UTF-8->Unicode的编码转换方法来对源文件进行转换。

a、对于控制台程序,编译器会把源文件看作是由系统默认的编码类型来编码的(系统默认的编码类型取决于在控制面板区域设置里的配置,中文win2k下通常是GBK),也可以使用-encoding参数来设置,如:javac -encoding UTF-8,这样编译器就会把源文件看作是用UTF-8编码的(这只是告诉编译器源文件的编码类型,而不是对源文件转码)。在各种语言的平台上只要在编译用时-encoding指定与源文件的编码相同的编码方式,就不会存在国际化的问题了。

b、对于JSP,编译器则会根据设定的字符集来判断JSP文件使用的是什么编码方式,进而将其转换成unicode后进行编译;若JSP中未指定,编译器则会把JSP文件看作是按照系统默认的编码来保存的。在JSP2.0里新增了一个指令来通知编译器这个源文件所使用的编码方式。

2、在处理输入输出时,注意设置输入流和输出流的编码类型与用户输入时和输出设备显示时采用的编码方式一致。

由于JRE在处理输入输出时会将输入或输出的内容进行编码转换,对于输入会转换为unicode后再送入,因此要正确的匹配实际输入内容的编码方式和告知JRE的编码方式,对于输出,会由unicode转换为其他的编码再送出程序,因此要正确匹配输出设备显示时用的编码方式和告知JRE的编码方式。

例如:程序中设置输入流的编码是new InputStreamReader(System.in,"GB2312");而程序运行后用户输入时用了繁体中文的输入法,输入了BIG5编码的内容,这样JRE把BIG5编码的内容当作GB2312的进行了GB2312->unicode的编码转换,这样转换后的结果显然不是用户想要输入的内容了。

默认情况下,JRE会把输入输出的内容当作是按照系统默认编码方式编码的。

3、在Servlet中,除了一定要把源文件用的是什么编码方式正确无误的“告诉”编译器外,还要注意实际提交的URL数据、表单数据的编码格式和request中声明的编码格式一致。

客户端浏览器在通过表单和URL提交数据时,容器和JVM会将request中的数据看作是按照request所声明的编码方式来编码的,将数据由这种编码方式转换为unicode后再送入servlet(实际上容器会先将request中的数据转为一种中间编码方式,具体根据容器的配置而定,再由JVM由这种中间方式转换为unicode,通常这种中间格式是ISO)。servlet输出的unicode数据会由容器根据response中声明的编码方式进行转换,再送到客户端浏览器上。

在接收客户端输入时,用request.setCharacterEncoding()声明请求中数据的编码方式。

在向客户端输出时用response.setContentType("text/html;charset=");声明响应的数据的编码方式,告知浏览器以哪种编码方式显示。

4、在JSP中,由于JSP本就会被JSP编译器编译为servlet来运行,因此情况与servlet相同。

这两个JSP指令声明了请求和响应的编码方式。

只要确保URL参数或表单中数据的编码方式和所声明的编码方式一致,再通过告知JSP编译器本JSP文件采用的编码方式及含有哪种字符,即可解决JSP的字符编码问题。

这里是一个具体的例子:

现在因为浏览器对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);
}
}

查看本文来源

推广二维码
邮件订阅

如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

重磅专题