java

位置:IT落伍者 >> java >> 浏览文章

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


发布日期:2021年09月03日
 
Java多语言编码问题解析(2)

这里是一个具体的例子

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

以我们使用的tomcat为例过程如下

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

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

增加一个用来声明request的CharacterEncoding的类SetCharacterEncodingFilterjava

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

修改webxml文件配置一个filter来过滤全部url请求通过第二步中的类声明所有url请求的编码类型未UTF

在webxml文件中加上以下这段

Set Character Encoding

orgkylewebsampleSetCharacterEncodingFilter

encoding

UTF

Set Character Encoding

/*

在上面这段文字中orgkylewebsampleSetCharacterEncodingFilter指定步骤中的类的位置 UTF指定我们希望声明的request的编码类型/*指定这个filter的适用范围(这里指的是全部url请求)

同时注意二个问题

servlet的版本必需是支持requestsetCharacterEncoding(String encoding)这个方法才行也就是在serlvert以上

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

SetCharacterEncodingFilter源文件

package orgkylewebsample;

import javaioIOException;

import javaxservletFilter;

import javaxservletFilterChain;

import javaxservletFilterConfig;

import javaxservletServletException;

import javaxservletServletRequest;

import javaxservletServletResponse;

import javaxservletUnavailableException;

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 withIf 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()

{

thisencoding = null;

thisfilterConfig = 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 || (requestgetCharacterEncoding() == null))

{

String encoding = selectEncoding(request);

if (encoding != null)

requestsetCharacterEncoding(encoding);

}

// Pass control on to the next filter

chaindoFilter(request response);

}

/**

* Place this filter into service

*

* @param filterConfig The filter configuration object

*

*encoding

* UTF

*

*/

public void init(FilterConfig filterConfig) throws ServletException

{

thisfilterConfig = filterConfig;

thisencoding = filterConfiggetInitParameter(encoding);

String value = filterConfiggetInitParameter(ignore);

if (value == null)

thisignore = true;

else if (valueequalsIgnoreCase(true))

thisignore = true;

else if (valueequalsIgnoreCase(yes))

thisignore = true;

else

thisignore = false;

}

/**

* Select an appropriate character encoding to be used based on the

* characteristics of the current request and/or filter initialization

* parametersIf 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 (thisencoding);

}

}

上一篇:Java 国际化和本地化 Toolkit 2.0(下)

下一篇:学习深入Java 1.5枚举类型的内部