ITeye博客

转:WebLogic8.1的中文问题解决方法

fragility · 2008-01-29 10:16 · 记录 · Markdown

原文地址:http://dev2dev.bea.com.cn/techdoc/200311546.html

由于近来论坛中经常碰到中文问题的咨询,所以在下在百忙之中赶出这篇文章,希望能给大家一点指引.如有纰漏,请及时指正!

Web应用中的中文问题

  1. 静态页面中文信息不能正确显示 浏览器端看到中文不能正确显示,首先应该检查浏览器是否支持中文,浏览器的编码是否设置正确.为保证静态页面中文信息正确显示可以在HTML 部分增加:

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

JDBC中的中文问题 如果以上的方法还不能解决你的乱码问题,那么可能是JDBC操作中的失误.这里以Oracle9I为例来说明jdbc中的中文问题.首先查询数据库: select * from v$nls_parameters 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(); } /*
  • 解密方法
  • @author zhg
  • 创建日期 (2002-12-15 10:17:08)
  • @param strCode 需解密的字符串
  • @return 解密后的字符串
  • @version 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 = strCode.substring(0, intStrLen / 2)

  • strCode.substring(intStrLen / 2 + 1, intStrLen + 1); strOriginal = new SecurityTest().loopCode( strCode, Constants.INT_RETURN_LOOP - intRnd); strDecodeMe = strOriginal; return strDecodeMe; } /**
  • 加密方法.随机取得加密的循环次数,使得每次加密所得的秘文会有所不同
  • @author zhg
  • 创建日期 (2002-12-15 10:17:08)
  • @param strOriginal 需加密的字符串
  • @return 加密后的字符串
  • @version 1.0 */ public static String encode(String strOriginal) { String strCode; int intRnd; char rnd; int intStrLen; String strCodeMe = ""; if (strOriginal.equals("")) return strCodeMe; //2 到 93 之间的随即数,即同一原文可能获得93种不同的秘文 intRnd = (int) (Math.random() * (Constants.INT_RETURN_LOOP - 2) + 2); strCode = new SecurityTest().loopCode(strOriginal, intRnd); //对随机数作偏移加密 rnd = (char) (intRnd + new SecurityTest().startChar()); intStrLen = strCode.length(); strCodeMe = strCode.substring(0, intStrLen / 2)
  • rnd
  • strCode.substring(intStrLen / 2, intStrLen); if (strCodeMe.indexOf("'") >= 0) return encode(strOriginal); else return strCodeMe; } //基础的凯撒算法,并对于每一位增加了偏移 private String kaiserCode(String strOriginal) { int intChar; String strCode; int i; int intStrLen; int intTmp;

intStrLen = strOriginal.length();

strCode = ""; for (i = 0; i < intStrLen; i++) { intChar = strOriginal.substring(i, i + 1).hashCode(); intTmp = intChar - this.startChar(); intTmp = (intTmp * Constants.INT_PRIM_NUMBER + i + 1) % this.maxChar()

  • this.startChar(); strCode = strCode + (char) (intTmp); } return strCode; } //循环调用凯撒算法一定次数后,可以取得原文 private String loopCode(String strOriginal, int intLoopCount) { String strCode; int i; strCode = strOriginal; for (i = 0; i < intLoopCount; i++) strCode = this.kaiserCode(strCode); return strCode; } public static void main(String args) throws Exception {

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); } private int maxChar() { String str1 = "~"; String str2 = "!"; return str1.hashCode() - str2.hashCode() + 1; } private int startChar() { String str1 = "!"; return str1.hashCode(); } }

总结 本文列举了WebLogic中经常碰到的一些中文问题的解决方法.希望读者能够灵活运用.需要提醒的是GBK字符集比GB2312的字库大,有些不常用字在GB2312里是没有的.所以请尽量使用GBK字符集.

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。