小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

WebLogic8.1的中文問題解決方法

 smoking_boy 2005-09-17
由于近來論壇中經(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(""))
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;
}
/**
* 加密方法.隨機(jī)取得加密的循環(huán)次數(shù),使得每次加密所得的秘文會(huì)有所不同
* zhg
* 創(chuàng)建日期 (2002-12-15 10:17:08)
* strOriginal 需加密的字符串
* 加密后的字符串
* 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 之間的隨即數(shù),即同一原文可能獲得93種不同的秘文
intRnd = (int) (Math.random() * (Constants.INT_RETURN_LOOP - 2) + 2);
strCode = new SecurityTest().loopCode(strOriginal, intRnd);
//對(duì)隨機(jī)數(shù)作偏移加密
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;
}
//基礎(chǔ)的凱撒算法,并對(duì)于每一位增加了偏移
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;
}
//循環(huán)調(diào)用凱撒算法一定次數(shù)后,可以取得原文
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();
}
}

總結(jié)
本文列舉了WebLogic中經(jīng)常碰到的一些中文問題的解決方法.希望讀者能夠靈活運(yùn)用.需要提醒的是GBK字符集比GB2312的字庫(kù)大,有些不常用字在GB2312里是沒有的.所以請(qǐng)盡量使用GBK字符集.

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多