WebLogic8.1的中文问题解决方法(转载)[1]

[入库:2005年8月18日] [更新:2007年3月25日]

本文简介:选择自 yukikaze 的 blog


作者:周海根(dev2dev论坛id:zhouhg) 长城软件系统有限公司 j2ee架构设计师, 项目经理
web应用中的中文问题
1. 静态页面中文信息不能正确显示
浏览器端看到中文不能正确显示,首先应该检查浏览器是否支持中文,浏览器的编码是否设置正确.为保证静态页面中文信息正确显示可以在html <head> 部分增加:
<meta http-equiv="content-type" content="text/html" charset="gbk">
2. jsp里的中文提示信息不能正确显示
jsp里的中文提示信息不能正常显示,最直接的原因是weblogic的默认字符集不是中文字符集(weblogic8.1里是setlocal,weblogic7.0sp3,sp4为utf-8),因此可以在jsp页面中设置字符集,加入如下脚本:
<%@ page contenttype="text/html; charset=gbk" %>
这种做法需要对每个jsp页面进行设置,下面的做法针对所有的jsp页面进行设置,比较通用.
3. jsp文件中的提示信息不能正确编译
jsp文件中的提示信息正确编译,可以在weblogic.xml里设置如下脚本,同时也可以解决上面说的第二个问题:
<jsp-descriptor>
<jsp-param>
<param-name>compilecommand</param-name>
<param-value>javac</param-value>
</jsp-param>
<jsp-param>
<param-name>compilersupportsencoding</param-name>
<param-value>true</param-value>
</jsp-param>
<jsp-param>
<param-name>encoding</param-name>
<param-value>gbk</param-value>
</jsp-param>
</jsp-descriptor>
4. jsp文件之间不能正确传递中文数据
jsp文件之间不能正确传递中文数据,可以有两种方法解决.
其一:在web.xml里加上如下脚本:
<context-param>
<param-name>weblogic.httpd.inputcharset./*</param-name>
<param-value>gbk</param-value>
</context-param>
其二:在weblogic.xml里加上如下脚本:
<charset-params>
<input-charset>
<resource-path>/*</resource-path>
<java-charset-name>gbk</java-charset-name>
</input-charset>
</charset-params>
当然这种问题也可以自己用java.net.urlencoder和java.net.urldecoder来处理中文.
以上都没有涉及到数据库操作,所以当出现乱码时,逐一分析,
必能找到问题所在.另外可能还需要修改weblogic应用服务器所在操作系统的字符集,确保支持中文.

文件名和目录中的中文问题
如果你的文件名或者目录是中文怎么办呢?上面提到的方法不能解决你的问题了.这时需要使用java.net.urlencoder编码.举个例子,在test.jsp里,你需要提供一个超链接到 ./测试/测试.jsp,你可以这么写:
<p><a href="<%=java.net.urlencoder.encode("./测试/测试.jsp")%>">go</p>

jdbc中的中文问题
如果以上的方法还不能解决你的乱码问题,那么可能是jdbc操作中的失误.这里以oracle9i为例来说明jdbc中的中文问题.首先查询数据库:
select * from v where parameter='nls_characterset';
得到数据库的字符集,如果zhs16gbk,则jdbc的操作不需要转码;如果是us7ascii,则需要转码或者作相关配置.下面以使用不同的数据库驱动程序为例来介绍.
1. 使用thin driver
如果使用thin driver,那么需要在查询数据库的时候将字符集由iso转换为gbk,写入数据库的时候将字符集由gbk转换为iso.
举个例子:
插入一条记录:
connection conn=null;
preparedstatement pstmt = null;
try {
string strsql="insert into taba(a,b) values('1111','王超')";
conn=ds.getconnection();
strsql = new string(strsql.getbytes("gbk"), "iso-8859-1");
pstmt = conn.preparestatement(strsql);
pstmt.executeupdate();
}
catch (exception e) {
//logger.error(e, e);
}
finally {
disconn(conn, pstmt);
}
查询一条记录:
connection conn=null;
preparedstatement pstmt = null;
resultset rs=null;
try {
string strsql="select b from taba where a='1111'";
conn=ds.getconnection();
strsql = new string(strsql.getbytes("gbk"), "iso-8859-1");
pstmt = conn.preparestatement(strsql);
rs=pstmt.executequery();
string strb;
if (rs.next()){
strb=new string(rs.getstring(1) .getbytes("iso-8859-1"), "gbk");
}
catch (exception e) {
//logger.error(e, e);
}
finally {
disconn(conn, pstmt, rs);
}
这里建议你在属性文件里设置oracle字符集,根据字符集判断
是否转码,以增加应用的移植性.
2.使用oci driver
直接使用weblogic提供的driver,在配置连接池时设置properties属性:
weblogic.codeset=gbk,如下图所示:

当你采用了上面的两个方法还不能解决你的乱码时,你检查一下是否仅仅是孤僻字不能正常显示呢?这种情况你需要使用oracle的字符集包: nls_charset12.zip,将该包加入到weblogic classpath中.

加密中的中文问题
对于不含中文信息的加密和解码,我想有很多算法可以实现.但由于中文为两个字符,普通的加密算法在一次加密一次解密之后无法复原.采用base64对中文信息进行编码后再加密可以轻松解决这个问题.当然解密之后同样需要用base64解码.示例如下:
string pw="中文";
system.out.println(pw);
pw=new sun.misc.base64encoder().encode(pw.getbytes());
system.out.println(pw);
//加密
string pw1=encode(pw);
system.out.println(pw1);
//解密
string pw2=decode(pw1);
system.out.println(pw2);
byte[] bt=new sun.misc.base64decoder().decodebuffer(pw2);
pw2=new string(bt);
system.out.println(pw2);
下面给出一个完整的使用kaiser算法加密的源代码:
package test;
/**
* 加密类
*/
import java.lang.math;
public class securitytest {
interface constants{
public static final int int_prim_number = 95;
public static final int int_return_loop = 94;
}
/**
* sysbean constructor comment.
*/
public securitytest() {
super();
}
/**
* 解密方法
* zhg
* 创建日期 (2002-12-15 10:17:08)
* strcode 需解密的字符串
* 解密后的字符串
* 1.0
*/
public static string decode(string strcode) {
string stroriginal;
int intrnd;
string strrnd;
int intstrlen;

string strdecodeme = "";

if (strcode.equals(""))
return strdecodeme;
intstrlen = strcode.length() - 1;

strrnd = strcode.substring(intstrlen / 2, intstrlen / 2 + 1);
intrnd = strrnd.hashcode() - new securitytest().startchar();
strcode =

本文关键:WebLogic8.1的中文问题解决方法(转载)
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top