|
由于近來論壇中經(jīng)常碰到中文問題的咨詢,所以在下在百忙之中趕出這篇文章,希望能給大家一點(diǎn)指引.如有紕漏,請(qǐng)及時(shí)指正! Web應(yīng)用中的中文問題 1. 靜態(tài)頁面中文信息不能正確顯示 瀏覽器端看到中文不能正確顯示,首先應(yīng)該檢查瀏覽器是否支持中文,瀏覽器的編碼是否設(shè)置正確.為保證靜態(tài)頁面中文信息正確顯示可以在HTML <HEAD> 部分增加: <meta http-equiv="Content-Type" content="text/html" charset="GBK"> 2. JSP里的中文提示信息不能正確顯示 JSP里的中文提示信息不能正常顯示,最直接的原因是WebLogic的默認(rèn)字符集不是中文字符集(Weblogic8.1里是setlocal,Weblogic7.0sp3,sp4為UTF-8),因此可以在JSP頁面中設(shè)置字符集,加入如下腳本: <%@ page contentType="text/html; charset=GBK" %> 這種做法需要對(duì)每個(gè)JSP頁面進(jìn)行設(shè)置,下面的做法針對(duì)所有的jsp頁面進(jìn)行設(shè)置,比較通用. 3. JSP文件中的提示信息不能正確編譯 JSP文件中的提示信息正確編譯,可以在weblogic.xml里設(shè)置如下腳本,同時(shí)也可以解決上面說的第二個(gè)問題: <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文件之間不能正確傳遞中文數(shù)據(jù) JSP文件之間不能正確傳遞中文數(shù)據(jù),可以有兩種方法解決. 其一:在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> 當(dāng)然這種問題也可以自己用java.net.URLEncoder和java.net.URLDecoder來處理中文. 以上都沒有涉及到數(shù)據(jù)庫(kù)操作,所以當(dāng)出現(xiàn)亂碼時(shí),逐一分析, 必能找到問題所在.另外可能還需要修改WebLogic應(yīng)用服務(wù)器所在操作系統(tǒng)的字符集,確保支持中文. 文件名和目錄中的中文問題 如果你的文件名或者目錄是中文怎么辦呢?上面提到的方法不能解決你的問題了.這時(shí)需要使用java.net.URLEncoder編碼.舉個(gè)例子,在test.jsp里,你需要提供一個(gè)超鏈接到 ./測(cè)試/測(cè)試.jsp,你可以這么寫: <p><a href="<%=java.net.URLEncoder.encode("./測(cè)試/測(cè)試.jsp")%>">go</p> JDBC中的中文問題 如果以上的方法還不能解決你的亂碼問題,那么可能是JDBC操作中的失誤.這里以O(shè)racle9I為例來說明jdbc中的中文問題.首先查詢數(shù)據(jù)庫(kù): select * from v where parameter=‘NLS_CHARACTERSET‘; 得到數(shù)據(jù)庫(kù)的字符集,如果ZHS16GBK,則JDBC的操作不需要轉(zhuǎn)碼;如果是us7ascii,則需要轉(zhuǎn)碼或者作相關(guān)配置.下面以使用不同的數(shù)據(jù)庫(kù)驅(qū)動(dòng)程序?yàn)槔齺斫榻B. 1. 使用Thin Driver 如果使用Thin Driver,那么需要在查詢數(shù)據(jù)庫(kù)的時(shí)候?qū)⒆址蒊SO轉(zhuǎn)換為GBK,寫入數(shù)據(jù)庫(kù)的時(shí)候?qū)⒆址蒅BK轉(zhuǎn)換為ISO. 舉個(gè)例子: 插入一條記錄: 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); } 這里建議你在屬性文件里設(shè)置oracle字符集,根據(jù)字符集判斷 是否轉(zhuǎn)碼,以增加應(yīng)用的移植性. 2.使用OCI Driver 直接使用WebLogic提供的driver,在配置連接池時(shí)設(shè)置Properties屬性: weblogic.codeset=GBK,如下圖所示: 當(dāng)你采用了上面的兩個(gè)方法還不能解決你的亂碼時(shí),你檢查一下是否僅僅是孤僻字不能正常顯示呢?這種情況你需要使用oracle的字符集包: nls_charset12.zip,將該包加入到WebLogic classpath中. 加密中的中文問題 對(duì)于不含中文信息的加密和解碼,我想有很多算法可以實(shí)現(xiàn).但由于中文為兩個(gè)字符,普通的加密算法在一次加密一次解密之后無法復(fù)原.采用BASE64對(duì)中文信息進(jìn)行編碼后再加密可以輕松解決這個(gè)問題.當(dāng)然解密之后同樣需要用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); 下面給出一個(gè)完整的使用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 * 創(chuàng)建日期 (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("")) strRnd = strCode.substring(intStrLen / 2, intStrLen / 2 + 1); intStrLen = strOriginal.length(); strCode = ""; String pw = "中文"; 總結(jié) |
|
|