您的位置:首页 > 其它

Web开发中编码问题的技术整理

2008-06-06 09:58 344 查看
Java编码转换:
getBytes(String encode):以encode编码方式拆分(编码)字符串,形成字节流
new String(byte[] bytes, String encoding):以encode编码方式组合(解码)字符串,形成本地编码
内部实现最终决定于Converter类

Charset定义:
jsp_charset:jsp页面的contentType里面的charset设置
compile_charset:javac所带的encoding参数,一般默认为本地字符集
servlet_charset:web.xml文件里面的配置的request的encoding,一般和jsp_charset一致

tomcat中的乱码基本上有两种:
第一类是*.jsp文件中的中文无法正确显示。(决定于jsp_charset)
第二类是 request 中的 parameter 取出来是乱码。(决定于servlet_charset)
Reuqest.getParameter实现:
内部实现最终决定于ByteChunk的toStringInternal()
public String toStringInternal() {
String strValue=null;
try {
if( enc==null ) enc=DEFAULT_CHARACTER_ENCODING;
strValue = new String( buff, start, end-start, enc );
} catch (java.io.UnsupportedEncodingException e) {
strValue = new String(buff, start, end-start);
}
return strValue;
}
enc决定于servlet_charset,这里必须要和jsp_charset一致,否则Reuqest.getParameter()为乱码。
DEFAULT_CHARACTER_ENCODING编码决定于JVM的file.encoding参数。

tomcat的启动配置:
如果存在 bin/catalina.bat
在“rem Execute Java with the applicable properties”一句后加上:
set JAVA_OPTS=%JAVA_OPTS% -Dfile.encoding=UTF-8

否则,执行 tomcat5w.exe,在 Java –〉Java Options 选项添加一行:
-Dfile.encoding=UTF-8

在linux环境下,修改/etc/init.d/tomcat文件中LC_ALL=zh_CN.UTF-8即可,可不用再修改tomcat配置
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: