您的位置:首页 > 编程语言 > Java开发

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

2004-06-23 11:38 746 查看
这里是一个具体的例子:
现在因为浏览器对UTF-8的支持,我们可以通过在源文件、请求、响应中都使用unicode编码方式,来轻松达到处理国际化和字符编码问题的目标。
以我们使用的tomcat4.1.2为例,过程如下:
1、编写JSP页面时:在每个JSP页面在页首都要增加一行:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
在编辑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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: