一.引言
???
ORACLE數(shù)據(jù)庫(kù)字符集,即Oracle全球化支持(Globalization Support),或即國(guó)家語(yǔ)言支持(NLS)其作用是用本國(guó)語(yǔ)言和格式來(lái)存儲(chǔ)、處理和檢索數(shù)據(jù)。利用全球化支持,ORACLE為用戶提供自己熟悉的數(shù)據(jù)庫(kù)母語(yǔ)環(huán)境,諸如日期格式、數(shù)字格式和存儲(chǔ)序列等。Oracle可以支持多種語(yǔ)言及字符集,其中oracle8i支持48種語(yǔ)言、76個(gè)國(guó)家地域、229種字符集,而oracle9i則支持57種語(yǔ)言、88個(gè)國(guó)家地域、235種字符集。由于oracle字符集種類多,且在存儲(chǔ)、檢索、遷移oracle數(shù)據(jù)時(shí)多個(gè)環(huán)節(jié)與字符集的設(shè)置密切相關(guān),因此在實(shí)際的應(yīng)用中,數(shù)據(jù)庫(kù)開(kāi)發(fā)和管理人員經(jīng)常會(huì)遇到有關(guān)oracle字符集方面的問(wèn)題。本文通過(guò)以下幾個(gè)方面闡述,對(duì)oracle字符集做簡(jiǎn)要分析
二.字符集基本知識(shí)
2.1字符集
??? 實(shí)質(zhì)就是按照一定的字符編碼方案,對(duì)一組特定的符號(hào),分別賦予不同數(shù)值編碼的集合。Oracle數(shù)據(jù)庫(kù)最早支持的編碼方案是US7ASCII。
??? Oracle
的字符集命名遵循以下命名規(guī)則
: ??? ??? 即:? <語(yǔ)言><比特位數(shù)><編碼
> ??? 比如: ZHS16GBK表示采用GBK編碼格式、16位(兩個(gè)字節(jié))簡(jiǎn)體中文字符集
2.2字符編碼方案
2.2.1
單字節(jié)編碼
???
(1)單字節(jié)7位字符集,可以定義128個(gè)字符,最常用的字符集為
US7ASCII ??? (2)單字節(jié)8位字符集,可以定義256個(gè)字符,適合于歐洲大部分國(guó)家
???
例如:WE8ISO8859P1(西歐、8位、ISO標(biāo)準(zhǔn)8859P1編碼
) 2.2.2 多字節(jié)編碼
???
(1)變長(zhǎng)多字節(jié)編碼
????
某些字符用一個(gè)字節(jié)表示,其它字符用兩個(gè)或多個(gè)字符表示,變長(zhǎng)多字節(jié)編碼常用于對(duì)亞洲語(yǔ)言的支持,?? 例如日語(yǔ)、漢語(yǔ)、印地語(yǔ)等
???
例如:AL32UTF8(其中AL代表ALL,指適用于所有語(yǔ)言)、
zhs16cgb231280 ??? (2)定長(zhǎng)多字節(jié)編碼
???
每一個(gè)字符都使用固定長(zhǎng)度字節(jié)的編碼方案,目前oracle唯一支持的定長(zhǎng)多字節(jié)編碼是AF16UTF16,也是僅用于國(guó)家字符集
2.2.3 unicode
編碼
??? Unicode
是一個(gè)涵蓋了目前全世界使用的所有已知字符的單一編碼方案,也就是說(shuō)Unicode為每一個(gè)字符提供唯一的編碼。UTF-16是unicode的16位編碼方式,是一種定長(zhǎng)多字節(jié)編碼,用2個(gè)字節(jié)表示一個(gè)unicode字符,AF16UTF16是UTF-16編碼字符集。
??? UTF-8
是unicode的8位編碼方式,是一種變長(zhǎng)多字節(jié)編碼,這種編碼可以用1、2、3個(gè)字節(jié)表示一個(gè)unicode字符,AL32UTF8,UTF8、UTFE是UTF-8編碼字符集
2.3 字符集超級(jí)
??? 當(dāng)一種字符集(字符集A)的編碼數(shù)值包含所有另一種字符集(字符集B)的編碼數(shù)值,并且兩種字符集相同編碼數(shù)值代表相同的字符時(shí),則字符集A是字符集B的超級(jí),或稱字符集B是字符集A的子集。
??? Oracle8i
和oracle9i官方文檔資料中備有子集-超級(jí)對(duì)照表(subset-superset pairs),例如:WE8ISO8859P1是WE8MSWIN1252的子集。由于US7ASCII是最早的Oracle數(shù)據(jù)庫(kù)編碼格式,因此有許多字符集是US7ASCII的超集,例如WE8ISO8859P1、ZHS16CGB231280、ZHS16GBK都是US7ASCII的超集。
2.4 數(shù)據(jù)庫(kù)字符集(oracle服務(wù)器端字符集)
??? 數(shù)據(jù)庫(kù)字符集在創(chuàng)建數(shù)據(jù)庫(kù)時(shí)指定,在創(chuàng)建后通常不能更改。在創(chuàng)建數(shù)據(jù)庫(kù)時(shí),可以指定字符集(CHARACTER SET)和國(guó)家字符集(NATIONAL CHARACTER SET)。
2.4.1
字符集
??? (1)
用來(lái)存儲(chǔ)CHAR, VARCHAR2, CLOB, LONG等類型數(shù)據(jù)
??? (2)
用來(lái)標(biāo)示諸如表名、列名以及PL/SQL變量等
??? (3)
用來(lái)存儲(chǔ)SQL和PL/SQL程序單元等
2.4.2
國(guó)家字符集:
??? (1)
用以存儲(chǔ)NCHAR, NVARCHAR2, NCLOB等類型數(shù)據(jù)
??? (2)
國(guó)家字符集實(shí)質(zhì)上是為oracle選擇的附加字符集,主要作用是為了增強(qiáng)oracle的字符處理能力,因?yàn)?span lang="EN-US">NCHAR數(shù)據(jù)類型可以提供對(duì)亞洲使用定長(zhǎng)多字節(jié)編碼的支持,而數(shù)據(jù)庫(kù)字符集則不能。國(guó)家字符集在oracle9i中進(jìn)行了重新定義,只能在unicode編碼中的AF16UTF16和UTF8中選擇,默認(rèn)值是
AF16UTF16 2.4.3查詢字符集參數(shù)
???
可以查詢以下數(shù)據(jù)字典或視圖查看字符集設(shè)置情況
??? nls_database_parameters
、props$、
v$nls_parameters ??? 查詢結(jié)果中NLS_CHARACTERSET表示字符集,NLS_NCHAR_CHARACTERSET表示國(guó)家字符集
2.4.4
修改數(shù)據(jù)庫(kù)字符集
???
按照上文所說(shuō),數(shù)據(jù)庫(kù)字符集在創(chuàng)建后原則上不能更改。如果需要修改字符集,通常需要導(dǎo)出數(shù)據(jù)庫(kù)數(shù)據(jù),重建數(shù)據(jù)庫(kù),再導(dǎo)入數(shù)據(jù)庫(kù)數(shù)據(jù)的方式來(lái)轉(zhuǎn)換,或通過(guò)ALTER DATABASE CHARACTER SET語(yǔ)句修改字符集,但創(chuàng)建數(shù)據(jù)庫(kù)后修改字符集是有限制的,只有新的字符集是當(dāng)前字符集的超集時(shí)才能修改數(shù)據(jù)庫(kù)字符集,例如UTF8是US7ASCII的超集,修改數(shù)據(jù)庫(kù)字符集可使用ALTER DATABASE CHARACTER SET UTF8。
2.5 客戶端字符集(NLS_LANG參數(shù))
2.5.1
客戶端字符集含義
???
客戶端字符集定義了客戶端字符數(shù)據(jù)的編碼方式,任何發(fā)自或發(fā)往客戶端的字符數(shù)據(jù)均使用客戶端定義的字符集編碼,客戶端可以看作是能與數(shù)據(jù)庫(kù)直接連接的各種應(yīng)用,例如sqlplus,exp/imp等。客戶端字符集是通過(guò)設(shè)置NLS_LANG參數(shù)來(lái)設(shè)定的。
2.5.2 NLS_LANG
參數(shù)格式
??? NLS_LANG=_. ??? Language:
顯示oracle消息,校驗(yàn),日期命名
??? Territory
:指定默認(rèn)日期、數(shù)字、貨幣等格式
??? Client character set
:指定客戶端將使用的字符集
???
例如:
NLS_LANG=AMERICAN_AMERICA.US7ASCII? ??? AMERICAN是語(yǔ)言,AMERICA是地區(qū),US7ASCII是客戶端字符集
2.5.3
客戶端字符集設(shè)置方法
???? 1)UNIX
環(huán)境
???????? $NLS_LANG=“simplified chinese”_china.zhs16gbk ???????? $export NLS_LANG ????????
編輯oracle用戶的profile文件
??? 2)Windows
環(huán)境
????????
編輯注冊(cè)表
???????? Regedit.exe---HKEY_LOCAL_MACHINE---SOFTWARE---ORACLE—HOME0 2.5.4 NLS
參數(shù)查詢
??? Oracle
提供若干NLS參數(shù)定制數(shù)據(jù)庫(kù)和用戶機(jī)以適應(yīng)本地格式,例如有NLS_LANGUAGE,NLS_DATE_FORMAT,NLS_CALENDER等,可以通過(guò)查詢以下數(shù)據(jù)字典或v$視圖查看。
??? NLS_DATABASE_PARAMETERS--
顯示數(shù)據(jù)庫(kù)當(dāng)前NLS參數(shù)取值,包括數(shù)據(jù)庫(kù)字符集取值
??? NLS_SESSION_PARAMETERS--
顯示由NLS_LANG 設(shè)置的參數(shù),或經(jīng)過(guò)alter session 改變后的參數(shù)值(不包括由NLS_LANG 設(shè)置的客戶端字符集)
??? NLS_INSTANCE_PARAMETE--
顯示由參數(shù)文件init.ora 定義的參數(shù)V$NLS_PARAMETERS--顯示數(shù)據(jù)庫(kù)當(dāng)前NLS參數(shù)取值
2.5.5
修改NLS參數(shù)
???
使用下列方法可以修改NLS參數(shù)
???
(1)修改實(shí)例啟動(dòng)時(shí)使用的初始化參數(shù)文件
???
(2)修改環(huán)境變量
NLS_LANG ??? (3)使用ALTER SESSION語(yǔ)句,在oracle會(huì)話中修改
???
(4)使用某些SQL函數(shù)
??? NLS
作用優(yōu)先級(jí)別:Sql function>alter session>環(huán)境變量或注冊(cè)表>參數(shù)文件>數(shù)據(jù)庫(kù)默認(rèn)參數(shù)
三.導(dǎo)入/導(dǎo)出與字符集轉(zhuǎn)換
3.1 EXP/IMP
??? Export 和 Import 是一對(duì)讀寫(xiě)Oracle數(shù)據(jù)的工具。Export 將 Oracle 數(shù)據(jù)庫(kù)中的數(shù)據(jù)輸出到操作系統(tǒng)文件中, Import 把這些文件中的數(shù)據(jù)讀到Oracle 數(shù)據(jù)庫(kù)中,由于使用exp/imp進(jìn)行數(shù)據(jù)遷移時(shí),數(shù)據(jù)從源數(shù)據(jù)庫(kù)到目標(biāo)數(shù)據(jù)庫(kù)的過(guò)程中有四個(gè)環(huán)節(jié)涉及到字符集,如果這四個(gè)環(huán)節(jié)的字符集不一致,將會(huì)發(fā)生字符集轉(zhuǎn)換。
EXP ???? ____________? ?_________________? _____________ ???? |imp導(dǎo)入文件|<-><-> ?????------------?? -----------------??-------------
IMP? ???? ____________? ?_________________? _____________ ???? |imp導(dǎo)入文件|->|環(huán)境變量NLS_LANG|->|數(shù)據(jù)庫(kù)字符集| ?????------------?? -----------------??-------------
??? 四個(gè)字符集是
??
(1)源數(shù)據(jù)庫(kù)字符集
??
(2)Export過(guò)程中用戶會(huì)話字符集(通過(guò)NLS_LANG設(shè)定)
??
(3)Import過(guò)程中用戶會(huì)話字符集(通過(guò)NLS_LANG設(shè)定)
??
(4)目標(biāo)數(shù)據(jù)庫(kù)字符集
3.2導(dǎo)出的轉(zhuǎn)換過(guò)程
??? 在Export過(guò)程中,如果源數(shù)據(jù)庫(kù)字符集與Export用戶會(huì)話字符集不一致,會(huì)發(fā)生字符集轉(zhuǎn)換,并在導(dǎo)出文件的頭部幾個(gè)字節(jié)中存儲(chǔ)Export用戶會(huì)話字符集的ID號(hào)。在這個(gè)轉(zhuǎn)換過(guò)程中可能發(fā)生數(shù)據(jù)的丟失。
例:如果源數(shù)據(jù)庫(kù)使用ZHS16GBK,而Export用戶會(huì)話字符集使用US7ASCII,由于ZHS16GBK是16位字符集,而US7ASCII是7位字符集,這個(gè)轉(zhuǎn)換過(guò)程中,中文字符在US7ASCII中不能夠找到對(duì)等的字符,所以所有中文字符都會(huì)丟失而變成“?? ”形式,這樣轉(zhuǎn)換后生成的Dmp文件已經(jīng)發(fā)生了數(shù)據(jù)丟失。
因此如果想正確導(dǎo)出源數(shù)據(jù)庫(kù)數(shù)據(jù),則Export過(guò)程中用戶會(huì)話字符集應(yīng)等于源數(shù)據(jù)庫(kù)字符集或是源數(shù)據(jù)庫(kù)字符集的超集
3.3導(dǎo)入的轉(zhuǎn)換過(guò)程
???
(1)確定導(dǎo)出數(shù)據(jù)庫(kù)字符集環(huán)境
???
通過(guò)讀取導(dǎo)出文件頭,可以獲得導(dǎo)出文件的字符集設(shè)置
???
(2)確定導(dǎo)入session的字符集,即導(dǎo)入Session使用的NLS_LANG環(huán)境變量
???
(3)IMP讀取導(dǎo)出文件
???
讀取導(dǎo)出文件字符集ID,和導(dǎo)入進(jìn)程的NLS_LANG進(jìn)行比較
???
(4)如果導(dǎo)出文件字符集和導(dǎo)入Session字符集相同,那么在這一步驟內(nèi)就不需要轉(zhuǎn)換,如果不同,就需要把數(shù)據(jù)轉(zhuǎn)換為導(dǎo)入Session使用的字符集。可以看出,導(dǎo)入數(shù)據(jù)到數(shù)據(jù)庫(kù)過(guò)程中發(fā)生兩次字符集轉(zhuǎn)換
???
第一次:導(dǎo)入文件字符集與導(dǎo)入Session使用的字符集之間的轉(zhuǎn)換,如果這個(gè)轉(zhuǎn)換過(guò)程不能正確完成,Import向目標(biāo)數(shù)據(jù)庫(kù)的導(dǎo)入過(guò)程也就不能完成。
???
第二次:導(dǎo)入Session字符集與數(shù)據(jù)庫(kù)字符集之間的轉(zhuǎn)換。
???
然而,oracle8i的這種轉(zhuǎn)換只能在單字節(jié)字符集之間進(jìn)行,oracle8i導(dǎo)入Session不支持多字節(jié)字符集之間的轉(zhuǎn)換,因此為了避免第一次轉(zhuǎn)換,導(dǎo)入Session使用的NLS_LANG與導(dǎo)出文件字符集相同,第二次轉(zhuǎn)換(通過(guò)SQL*Net)支持任何兩種字符集。以上情況在Oracle9i中略有不同
四.亂碼問(wèn)題
??? oracle在數(shù)據(jù)存儲(chǔ)、遷移過(guò)程中經(jīng)常發(fā)生字符亂碼問(wèn)題,歸根到底是由于字符集使用不當(dāng)引起。下面以使用客戶端sqlplus向數(shù)據(jù)庫(kù)插入數(shù)據(jù)和導(dǎo)入/導(dǎo)出(EXP/IMP)過(guò)程為例,說(shuō)明亂碼產(chǎn)生的原因。
4.1使用客戶端sqlplus向數(shù)據(jù)庫(kù)存儲(chǔ)數(shù)據(jù)
???
這個(gè)過(guò)程存在3個(gè)字符集設(shè)置
???
(1)客戶端應(yīng)用字符集
???
(2)客戶端NLS_LANG參數(shù)設(shè)置
???
(3)服務(wù)器端數(shù)據(jù)庫(kù)字符集(Character Set)設(shè)置
???
客戶端應(yīng)用sqlplus中能夠顯示什么樣的字符取決于客戶端操作系統(tǒng)語(yǔ)言環(huán)境(客戶端應(yīng)用字符集),但在應(yīng)用中錄入這些字符后,這些字符能否在數(shù)據(jù)庫(kù)中正常存儲(chǔ),還與另外兩個(gè)字符集設(shè)置緊密相關(guān),其中客戶端NLS_LANG參數(shù)主要用于字符數(shù)據(jù)傳輸過(guò)程中的轉(zhuǎn)換判斷。常見(jiàn)的亂碼大致有兩種情形:
???
(1)漢字變成問(wèn)號(hào)“?”;
當(dāng)從字符集A 轉(zhuǎn)換成字符集B時(shí),如果轉(zhuǎn)換字符之間不存在對(duì)應(yīng)關(guān)系,NLS_LANG使用替代字符“?”替代無(wú)法映射的字符
???
(2)漢字變成未知字符(雖然有些是漢字,但與原字符含義不同)
轉(zhuǎn)換存在對(duì)應(yīng)關(guān)系,但字符集A 中的字符編碼與字符集B 中的字符編碼代表不同含義
4.2發(fā)生亂碼原因
? ?? ?亂碼產(chǎn)生是由于幾個(gè)字符集之間轉(zhuǎn)換不匹配造成,分以下幾種情況:
???
(注:字符集之間如果不存在子集、超集對(duì)應(yīng)關(guān)系時(shí)的情況不予考慮,因?yàn)檫@種情況下字符集之間轉(zhuǎn)換必產(chǎn)生亂碼)
??? ??? 1)服務(wù)器端數(shù)據(jù)庫(kù)字符集與客戶端應(yīng)用字符集相同,與客戶端NLS_LANG參數(shù)設(shè)置不同
???
如果客戶端NLS_LANG字符集是其它兩種字符集的子集,轉(zhuǎn)換過(guò)程將出現(xiàn)亂碼。
???
解決方法:將三種字符集設(shè)置成同一字符集,或NLS_LANG字符集是其它兩種字符集的超集
??? 2
)服務(wù)器端數(shù)據(jù)庫(kù)字符集與客戶端NLS_LANG參數(shù)設(shè)置相同,與客戶端應(yīng)用字符集不同
???
如果客戶端應(yīng)用字符集是其它兩種字符集的超集時(shí),轉(zhuǎn)換過(guò)程將出現(xiàn)亂碼,但對(duì)于單字節(jié)編碼存儲(chǔ)中文問(wèn)題,可參看本文第5章節(jié)的分析
??? 3
)客戶端應(yīng)用字符集、客戶端NLS_LANG參數(shù)設(shè)置、服務(wù)器端數(shù)據(jù)庫(kù)字符集互不相同
?? ?
此種情況較為復(fù)雜,但三種字符集之間只要有不能轉(zhuǎn)換的字符,則必產(chǎn)生亂碼
4.3導(dǎo)入/導(dǎo)出過(guò)程出現(xiàn)亂碼原因
???
這個(gè)過(guò)程存在4個(gè)字符集設(shè)置,在3.1章節(jié)中已分析
??
(1)源數(shù)據(jù)庫(kù)字符集
??
(2)EXP過(guò)程中NLS_LANG參數(shù)
??
(3)IMP過(guò)程中NLS_LANG參數(shù)
??
(4)目標(biāo)數(shù)據(jù)庫(kù)字符集
???
出現(xiàn)亂碼原因
??? 1
)當(dāng)源數(shù)據(jù)庫(kù)字符集不等于EXP過(guò)程中NLS_LANG參數(shù),且源數(shù)據(jù)庫(kù)字符集是EXP過(guò)程中NLS_LANG的子集,才能保證導(dǎo)出文件正確,其他情況則導(dǎo)出文件字符亂碼
??? 2
)EXP過(guò)程中NLS_LANG字符集不等于IMP過(guò)程中NLS_LANG字符集,且EXP過(guò)程中NLS_LANG字符集是IMP過(guò)程中NLS_LANG字符集的子級(jí), 才能保證第一次轉(zhuǎn)換正常,否則第一次轉(zhuǎn)換中出現(xiàn)亂碼。
??? 3
)如果第一次轉(zhuǎn)換正常,IMP過(guò)程中NLS_LANG字符集是目標(biāo)數(shù)據(jù)庫(kù)字符集的子集或相同,才能保證第二次轉(zhuǎn)換正常,否則則第二次轉(zhuǎn)換中出現(xiàn)亂碼
五.單字節(jié)編碼存儲(chǔ)中文問(wèn)題
???
由于歷史的原因,早期的oracle沒(méi)有中文字符集(如oracle6、oracle7、oracle7.1),但有的用戶從那時(shí)起就使用數(shù)據(jù)庫(kù)了,并用US7ASCII字符集存儲(chǔ)了中文,或是有的用戶在創(chuàng)建數(shù)據(jù)庫(kù)時(shí),不考慮清楚,隨意選擇一個(gè)默認(rèn)的字符集,如WE8ISO8859P1或US7ASCII,而這兩個(gè)字符集都沒(méi)有漢字編碼,雖然有些時(shí)候選用這種字符集好象也能正常使用,但用這種字符集存儲(chǔ)漢字信息從原則上說(shuō)就是錯(cuò)誤的,它會(huì)給數(shù)據(jù)庫(kù)的使用與維護(hù)帶來(lái)一系列的麻煩。
???
正常情況下,要將漢字存入數(shù)據(jù)庫(kù),數(shù)據(jù)庫(kù)字符集必須支持中文,而將數(shù)據(jù)庫(kù)字符集設(shè)置為US7ASCII等單字節(jié)字符集是不合適的。US7ASCII字符集只定義了128個(gè)符號(hào),并不支持漢字。另外,如果在SQL*PLUS中能夠輸入中文,操作系統(tǒng)缺省應(yīng)該是支持中文的,但如果在NLS_LANG中的字符集設(shè)置為US7ASCII,顯然也是不正確的,它沒(méi)有反映客戶端的實(shí)際情況。但在實(shí)際應(yīng)用中漢字顯示卻是正確的,這主要是因?yàn)?span lang="EN-US">Oracle檢查數(shù)據(jù)庫(kù)與客戶端的字符集設(shè)置是同樣的,那么數(shù)據(jù)在客戶與數(shù)據(jù)庫(kù)之間的存取過(guò)程中將不發(fā)生任何轉(zhuǎn)換,但是這實(shí)際上導(dǎo)致了數(shù)據(jù)庫(kù)標(biāo)識(shí)的字符集與實(shí)際存入的內(nèi)容是不相符的。而在SELECT的過(guò)程中,Oracle同樣檢查發(fā)現(xiàn)數(shù)據(jù)庫(kù)與客戶端的字符集設(shè)置是相同的,所以它也將存入的內(nèi)容原封不動(dòng)地傳送到客戶端,而客戶端操作系統(tǒng)識(shí)別出這是漢字編碼所以能夠正確顯示。
???
在這個(gè)例子中,數(shù)據(jù)庫(kù)與客戶端都沒(méi)有設(shè)置成中文字符集,但卻能正常顯示中文,從應(yīng)用的角度看好象沒(méi)問(wèn)題。然而這里面卻存在著極大的隱患,比如在應(yīng)用length或substr等字符串函數(shù)時(shí),就可能得到意外的結(jié)果。
???
對(duì)于早期使用US7ASCII字符集數(shù)據(jù)庫(kù)的數(shù)據(jù)遷移到oracle8i/9i中(使用zhs16gbk),由于原始數(shù)據(jù)已經(jīng)按照US7ASCII格式存儲(chǔ),對(duì)于這種情況,可以通過(guò)使用Oracle8i的導(dǎo)出工具,設(shè)置導(dǎo)出字符集為US7ASCII,導(dǎo)出后使用UltraEdit等工具打開(kāi)dmp文件,修改第二、三字符,修改 0001 為0354,這樣就可以將US7ASCII字符集的數(shù)據(jù)正確導(dǎo)入到ZHS16GBK的數(shù)據(jù)庫(kù)中。
六.結(jié)束語(yǔ)
??? 為了避免在數(shù)據(jù)庫(kù)遷移過(guò)程中由于字符集不同導(dǎo)致的數(shù)據(jù)損失,oracle提供了字符集掃描工具(character set scanner),通過(guò)這個(gè)工具我們可以測(cè)試在數(shù)據(jù)遷移過(guò)程中由于字符集轉(zhuǎn)換可能帶來(lái)的問(wèn)題,然后根據(jù)測(cè)試結(jié)果,確定數(shù)據(jù)遷移過(guò)程中最佳字符集解決方案。
參考文獻(xiàn)
[1]Biju Thomas , Bob Bryla
《oracle9i DBA基礎(chǔ)I 學(xué)習(xí)指南》電子工業(yè)出版社
2002
|
表格部分代碼如下:
<table id="testTbl" border=1>
<tr id="tr1">
<td width=6%><input type=checkbox id="box1"></td>
<td id="b">第一行</td>
</tr>
<tr id="tr2">
<td width=6%><input type=checkbox id="box2"></td>
<td id="b">第二行</td>
</tr>
<tr>
<td width=6%><input type=checkbox id="box3"></td>
<td>第三行</td>
</tr>
</table>
動(dòng)態(tài)添加表行的javascript函數(shù)如下:
<script language="javascript">
function addRow(){
//添加一行
var newTr = testTbl.insertRow();
//設(shè)置行背景
newTr.bgColor = '#008040';
//添加兩列
var newTd0 = newTr.insertCell();
var newTd1 = newTr.insertCell();
//設(shè)置列內(nèi)容和屬性
newTd0.innerHTML = '<input type=checkbox id="box4">';
newTd1.innerText= '新增加的行';
}
</script>
<BR>
<a href="#" onclick="addRow();">增加一行</a>
就這么簡(jiǎn)單,做點(diǎn)詳細(xì)的說(shuō)明:
1、inserRow()和insertCell()函數(shù)
insertRow()函數(shù)可以帶參數(shù),形式如下:
insertRow(index)
這個(gè)函數(shù)將新行添加到index的那一行前,比如insertRow(0),是將新行添加到第一行之前。默認(rèn)的insertRow()函數(shù)相當(dāng)于insertRow(-1),將新行添加到表的最后。
insertCell()和insertRow的用法相同。
2、動(dòng)態(tài)設(shè)置屬性和事件
上面行數(shù)中的innerHTML和innerText都是列的屬性。
這個(gè)inner,就是“inner”到<tb></tb>之間,innerText是添加到<tb></tb>之間的文本,innerHTML是添加到<tb></tb>之間的HTML代碼(這個(gè)so簡(jiǎn)單,這個(gè)解釋挺多余的)
設(shè)置其他屬性也是用同樣的方式,比如,設(shè)置行背景色
newTr.bgColor = 'red';
?
設(shè)置事件也一樣,需要簡(jiǎn)單說(shuō)明一點(diǎn)。
比如,我要讓點(diǎn)擊新加行的時(shí)候執(zhí)行一個(gè)自己定義的函數(shù) newClick,newClick行數(shù)如下:
function newClick(){
alert("這是新添加的行");
}
對(duì)onclick事件設(shè)置這個(gè)函數(shù)的代碼如下:
newTr.onclick = newClick;
這里需要主義的是,=后面的部分必須是函數(shù)名,而且不能帶引號(hào),
newTr.onclick = newClick();
newTr.onclick = 'newClick';
newTr.onclick = "newClick";
上面的寫(xiě)法都是錯(cuò)誤的。
為什么,其實(shí)知道為什么沒(méi)有什么意思,知道怎么用就OK了,如果不想知道,可以跳過(guò)下面這一段。
?
實(shí)際上這個(gè)=后面的newClick是指向自己定義的newClick函數(shù)的指針,javascript里面函數(shù)名就是指向函數(shù)的指針,加了引號(hào)括號(hào)什么的瀏覽器就找不到那個(gè)函數(shù)了。
下面的寫(xiě)法,也是正確的
newTr.onclick = function newClick(){
alert("這是新添加的行");
}
這個(gè)使用函數(shù)名實(shí)際上是一樣的
設(shè)置其他的事件用法相同。
J2EE程序中使用oracle數(shù)據(jù)庫(kù)LOB字段的總結(jié)(elathen)
|
???http://www.souzz.net 2005-10-23 文章出處:博客園 |
? |
????
posted on 2005-05-27 09:36 輕松逍遙子 最近在J2EE的項(xiàng)目中需要使用LOB字段保存文本信息以及圖片和文件,到網(wǎng)上搜拉一下,還不少,仔細(xì)看拉一下,但都不是很全有的還有錯(cuò)誤,經(jīng)過(guò)幾天的實(shí)踐,把問(wèn)題都解決拉,順便總結(jié)一下,希望對(duì)需要的朋友有點(diǎn)參考
LOB中我們用的比較多的主要有兩種CLOB和BLOB,我們對(duì)兩種類型分別討論
1.CLOB是字符型LOB,主要存儲(chǔ)文本信息,,最長(zhǎng)為4G.,在J2EE程序中,比如網(wǎng)頁(yè)的textarea中的字符信息比較長(zhǎng),Varchar2字段類型不能滿足時(shí),我們就得用CLOB數(shù)據(jù)類型,我們這次項(xiàng)目中就碰到這種情況.現(xiàn)在我們先說(shuō)說(shuō)如何存取CLOB字段
現(xiàn)在我要把網(wǎng)頁(yè)中的textarea元素的信息保存到數(shù)據(jù)庫(kù)的CLOB字段中, 我們都知道textarea中的信息當(dāng)然不能直接保存成CLOB,我們?cè)诤笈_(tái)得到的是String類型的,不多說(shuō)拉,我們還是以一個(gè)實(shí)例講吧!
先建一個(gè)test表,表有2個(gè)字段:ID,CONTENTS,其中CONTENTS保存CLOB類型的文本數(shù)據(jù)
create table TEST ( ID VARCHAR2(18) not null, CONTENTS CLOB, )
接著我們編寫(xiě)一個(gè)測(cè)試用的jsp文件ClobTest.jsp,代碼如下
<%@ page language="java" contentType="text/html; charset=gb2312" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Clob對(duì)象的存取測(cè)試</title>
</head>
<body>
<form name="test" method="post" action="clobTest.action">
<table width="80%" height="88" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td height="30" align="center">輸入ID號(hào)<input type="text" name="ID">
</tr>
<tr>
<td align="center">
<textarea rows="28" cols="68" name="CONTENTS">
注冊(cè)用戶需遵守:
尊重會(huì)員個(gè)人隱私、保障會(huì)員隱私安全是CSDN的一項(xiàng)基本政策,CSDN不會(huì)公開(kāi)、編輯或透露會(huì)員的注冊(cè)資料,除非符合以下情況: (1) 根據(jù)中華人民共和國(guó)國(guó)家安全機(jī)構(gòu)、公安部門(mén)的要求及根據(jù)相應(yīng)的法律程序要求。 (2) 維護(hù)CSDN的商標(biāo)所有權(quán)及其它權(quán)益。 (3) 在緊急情況下竭力維護(hù)會(huì)員個(gè)人、其它社會(huì)個(gè)體和社會(huì)大眾的安全。 (4) 嚴(yán)重違反CSDN有關(guān)規(guī)定。 CSDN保留結(jié)束會(huì)員使用網(wǎng)絡(luò)服務(wù)資格的權(quán)利,并保證結(jié)束會(huì)員資格后仍為會(huì)員保密所有個(gè)人隱私。
</textarea>
</td>
</tr>
<tr>
<td align="center">
<input type="submit" name="Submit" value="提交">
</td>
</tr>
</table>
</form>
</body>
</html>
點(diǎn)擊”提交”按鈕,我們?cè)诤笈_(tái)的到的是2個(gè)String類型的對(duì)象
String strID = request.getParameter(“ID”);
String strCONTENTS = request.getParameter(“CONTENTS”);
接著我們要做的任務(wù)就是如何把String類型CONTENTS存到數(shù)據(jù)庫(kù)中的CLOB類型字段中!
注意:LOB數(shù)據(jù)不能象其它類型數(shù)據(jù)一樣直接插入(INSERT)。插入前必須先插入一個(gè)空的LOB對(duì)象,CLOB類型的空對(duì)象為EMPTY_CLOB(),BLOB類型的空對(duì)象為EMPTY_BLOB()。之后通過(guò)SELECT命令查詢得到先前插入的記錄并鎖定,繼而將空對(duì)象修改為所要插入的LOB對(duì)象。
//我們先插入一個(gè)空的CLOB對(duì)象
public int insertEmptyClob() throws Exception {
Statement statement = null;
int intResult = -1;
try {
//創(chuàng)建數(shù)據(jù)庫(kù)操作語(yǔ)句
statement = connection.createStatement();
//定義SQL語(yǔ)句
String strSQL = “INSET INTO TEST (ID,CONTENTS) VALUES(strID, EMPTY_CLOB())”;
//執(zhí)行SQL語(yǔ)句
intResult = statement.executeUpdate(strSQL);
System.out.println(" intResult valus is"+intResult);
return intResult;
} catch(Exception e) {
e.printStackTrace();
return -1;
} finally {
if (statement != null) {
statement.close();
}
}
}
//把strCONTENT插入CLOB字段
public void insertClob() throws Exception {
Statement statement = null;
ResultSet resultset = null;
try {
//設(shè)置不自動(dòng)提交
connection.setAutoCommit(false);
//創(chuàng)建數(shù)據(jù)庫(kù)操作語(yǔ)句
statement = connection.createStatement();
//定義SQL語(yǔ)句
String strSQL = “SELECT CONTENTS FROM TEST WHERE ID=strID"”
resultset = statement.executeQuery(strSQL);
oracle.sql.CLOB contents = null;
while(resultset.next()) {
//取出CLOB對(duì)象
contents = (oracle.sql.CLOB)resultset.getClob("CONTENTS");
}
Writer out = contents.getCharacterOutputStream();
out.write(strContents);
out.flush();
out.close();
//數(shù)據(jù)庫(kù)提交
connection.commit();
} catch(Exception e) {
e.printStackTrace();
}finally{
if(resultset != null) {
resultset.close();
}
if(statement != null) {
statement.close();
}
}
}
OK,我們已經(jīng)把這段文本以CLOB字段的形式保存到數(shù)據(jù)庫(kù)中了,在實(shí)際應(yīng)用中,如果要保存或修改一條記錄,我們要分2步做,先保存或修改非LOB字段類型的字段,再保存或修改LOB字段!接下來(lái)我們來(lái)把剛才保存到數(shù)據(jù)庫(kù)中的CLOB字段讀到j(luò)sp頁(yè)面中去。
我們?cè)诒4娴臅r(shí)候,CLOB字段會(huì)把上面textarea中的文本按原來(lái)的格式一行一行(包括空格)都保存到CLOB字段中,讀取的時(shí)候我們只要按照原來(lái)格式讀起出來(lái)就行了(我這里自己用了一個(gè)小處理方法,但如果你有更好的方法請(qǐng)告訴我)。在這里我們把CLOB讀到StringBuffer中,為了保存不同行我在行之間加了個(gè)“&”字符來(lái)區(qū)分。最后轉(zhuǎn)化成String
放到VO中,這樣就保證從前臺(tái)到后臺(tái),從后臺(tái)到前臺(tái)的數(shù)據(jù)傳遞的一致性!代碼如下:
/**
* 獲取CLOB文本對(duì)象
* @param sbSQL
* @return
* @throws java.lang.Exception
*/
public String selectIncludeClob(StringBuffer sbSQL) throws Exception {
Statement stmt = null;
ResultSet rs = null;
StringBuffer sbResult = new StringBuffer();
try {
//設(shè)定數(shù)據(jù)庫(kù)不自動(dòng)提交
//connection.setAutoCommit(false);
//創(chuàng)建數(shù)據(jù)庫(kù)操作語(yǔ)句
stmt = connection.createStatement();
//獲取結(jié)果集
rs = stmt.executeQuery(sbSQL.toString());
while(rs.next()) {
CLOB clob = (CLOB)rs.getClob("CONTENTS");
Reader isClob = clob.getCharacterStream();
BufferedReader bfClob = new BufferedReader(isClob);
String strClob = bfClob.readLine();
while(strClob != null) {
sbResult.append(strClob);
sbResult.append("&");
strClob = bfClob.readLine();
}
}
//提交事務(wù)
// connection.commit();
} catch(Exception e) {
e.printStackTrace();
throw e;
} finally {
if(rs != null) {
rs.close();
}
if(stmt != null) {
stmt.close();
}
}
return sbResult.toString();
}
到j(luò)sp頁(yè)面中,我們從VO中獲取改文本信息。
<textarea rows="42" cols="68" name="CONTENTS" style="border-style: solid; border-color: #FFFFFF; font-family:仿宋_GB2312; font-size:14pt; line-height:200%; margin-top:8; margin-bottom:6" >
<%
String content = vo.getContent();
String[] contentArray = content.split("&");
for(int i=0;i<contentArray.length;i++) {
String s= contentArray[i];
out.println(s);
}
%>
</textarea>
這樣我們就保證什么格式保存就以什么格式顯示。
2.BLOB字段,二進(jìn)制LOB,主要存儲(chǔ)二進(jìn)制數(shù)據(jù),最長(zhǎng)為4G,在J2EE程序中,一般類似于圖片和文件的保存。當(dāng)然也有另一種方法,就把圖片和文件保存在硬盤(pán)上,數(shù)據(jù)庫(kù)中只保存圖片的鏈接地址和文件在服務(wù)器上的路徑。如果遇到文件和圖片比較重要的還是需要保存到數(shù)據(jù)庫(kù)中(例如:我們做國(guó)土資源項(xiàng)目的時(shí)候,好多圖片、文件就很重要,需要保存到數(shù)據(jù)庫(kù)中),下面我寫(xiě)一個(gè)保存文件到數(shù)據(jù)庫(kù)的Blob字段和從數(shù)據(jù)庫(kù)的Blob字段中獲取文件的方法(當(dāng)然完全應(yīng)用還要做其他工作,這里就不多說(shuō)了,如果你不清楚的可以問(wèn)我):
/**
* 把上傳的文件保存到數(shù)據(jù)庫(kù)的Blob字段中
* @param strTableName 對(duì)應(yīng)的表名稱
* @param strColumnName 表中保存文件的Blob字段名稱
* @param inputStream 輸入的文件流
* @param sbSQLWhere where條件
* @throws java.lang.Exception
*/
public static void fileUpload(String strTableName,
String strColumnName,
InputStream inputStream,
StringBuffer sbSQLWhere)
throws Exception {
Connection con = null;
ResultSet resultset = null;
Statement stmt = null;
try {
//得到數(shù)據(jù)庫(kù)連接
con = DBConnector.getConnection();
//構(gòu)建查詢語(yǔ)句
StringBuffer sbSQL = new StringBuffer();
sbSQL.append(" UPDATE ");
sbSQL.append(strTableName);
sbSQL.append(" SET ");
sbSQL.append(strColumnName);
sbSQL.append("=EMPTY_BLOB() ");
sbSQL.append(sbSQLWhere);
System.out.println(" update sql value is*******"+sbSQL.toString());
//獲取數(shù)據(jù)庫(kù)操作語(yǔ)句
stmt=con.createStatement();
//插入空的blob對(duì)象
stmt.executeUpdate(sbSQL.toString());
con.setAutoCommit(false);
StringBuffer sbSQLBlob = new StringBuffer();
sbSQLBlob.append(" SELECT ");
sbSQLBlob.append(strColumnName);
sbSQLBlob.append(" FROM ");
sbSQLBlob.append(strTableName);
sbSQLBlob.append(sbSQLWhere);
sbSQLBlob.append(" FOR UPDATE");
System.out.println(" select sql value is*********"+sbSQL.toString());
resultset =stmt.executeQuery(sbSQLBlob.toString());
while (resultset.next()) {
/* 取出此BLOB對(duì)象 */
oracle.sql.BLOB blob = (oracle.sql.BLOB)resultset.getBlob("BODY");
/* 向BLOB對(duì)象中寫(xiě)入數(shù)據(jù) */
BufferedOutputStream out = new BufferedOutputStream(blob.getBinaryOutputStream());
BufferedInputStream in = new BufferedInputStream(inputStream);
int c;
while ((c=in.read())!=-1) {
out.write(c);
}
in.close();
out.close();
}
con.setAutoCommit(false);
con.commit();
} catch (Exception ex) {
ex.printStackTrace();
throw ex;
} finally {
if (stmt != null) {
stmt.close();
}
if (resultset != null) {
resultset.close();
}
if (con!=null) {
con.close();
}
}
}
下面的方法是從數(shù)據(jù)庫(kù)中得到上傳的文件的輸入流,把輸入流寫(xiě)到servlet流中,再?gòu)捻?yè)面中獲取,servlet就不寫(xiě)了。
/**
* 方法描述:得到數(shù)據(jù)庫(kù)上傳的文件數(shù)據(jù)
*
* 輸入?yún)?shù): 1:表名(String)
* 2:字段名(String)
* 3: Where條件(StringBuffer)
* 5: 輸出流(ServletOutputStream)
*
* 輸出參數(shù):void
* 編寫(xiě)人: */
public static void getdownFile(String strTableName,
String strColumnName,
StringBuffer sbSQLWhere,
ServletOutputStream sos) throws Exception {
Connection con = null;
PreparedStatement ps = null;
ResultSet resultset = null;
try {
//得到數(shù)據(jù)庫(kù)連接
con = DBConnector.getConnection();
StringBuffer sbSQL = new StringBuffer();
//構(gòu)建查詢語(yǔ)句
sbSQL.append(" SELECT " + strColumnName + " FROM " + strTableName);
sbSQL.append(sbSQLWhere);
System.out.println(" sql value is:"+sbSQLWhere.toString());
ps = con.prepareStatement(sbSQL.toString());
//執(zhí)行查詢
resultset = ps.executeQuery();
while (resultset.next()) {
//讀取數(shù)據(jù)流
InputStream is = resultset.getBinaryStream(strColumnName);
byte[] buf = new byte[2048];
while(is.read(buf)!=-1) {
//把數(shù)據(jù)流按塊寫(xiě)到servlet的輸出流中
sos.write(buf);
}
}
} catch (Exception ex) {
ex.printStackTrace();
throw ex;
} finally {
if (ps != null) {
ps.close();
}
if (resultset != null) {
resultset.close();
}
if (con!=null) {
con.close();
}
}
}
圖片的保存和文件的保存一樣,如果不清楚的可以和我聯(lián)系
后記:
平時(shí)總忙著做項(xiàng)目,閑的時(shí)候也很懶,總想把自己實(shí)際中的一些問(wèn)題和解決方法小結(jié)一下,但總沒(méi)完成,這是第一次寫(xiě),寫(xiě)的不好或不清楚的地方請(qǐng)包涵,下次改進(jìn),也希望大家多提意見(jiàn),大家一起進(jìn)步!!!!!!!!!!! |
|
J2EE程序中使用oracle數(shù)據(jù)庫(kù)LOB字段的總結(jié)(elathen)
|
???http://www.souzz.net 2005-10-23 文章出處:博客園 |
? |
????
posted on 2005-05-27 09:36 輕松逍遙子 最近在J2EE的項(xiàng)目中需要使用LOB字段保存文本信息以及圖片和文件,到網(wǎng)上搜拉一下,還不少,仔細(xì)看拉一下,但都不是很全有的還有錯(cuò)誤,經(jīng)過(guò)幾天的實(shí)踐,把問(wèn)題都解決拉,順便總結(jié)一下,希望對(duì)需要的朋友有點(diǎn)參考
LOB中我們用的比較多的主要有兩種CLOB和BLOB,我們對(duì)兩種類型分別討論
1.CLOB是字符型LOB,主要存儲(chǔ)文本信息,,最長(zhǎng)為4G.,在J2EE程序中,比如網(wǎng)頁(yè)的textarea中的字符信息比較長(zhǎng),Varchar2字段類型不能滿足時(shí),我們就得用CLOB數(shù)據(jù)類型,我們這次項(xiàng)目中就碰到這種情況.現(xiàn)在我們先說(shuō)說(shuō)如何存取CLOB字段
現(xiàn)在我要把網(wǎng)頁(yè)中的textarea元素的信息保存到數(shù)據(jù)庫(kù)的CLOB字段中, 我們都知道textarea中的信息當(dāng)然不能直接保存成CLOB,我們?cè)诤笈_(tái)得到的是String類型的,不多說(shuō)拉,我們還是以一個(gè)實(shí)例講吧!
先建一個(gè)test表,表有2個(gè)字段:ID,CONTENTS,其中CONTENTS保存CLOB類型的文本數(shù)據(jù)
create table TEST ( ID VARCHAR2(18) not null, CONTENTS CLOB, )
接著我們編寫(xiě)一個(gè)測(cè)試用的jsp文件ClobTest.jsp,代碼如下
<%@ page language="java" contentType="text/html; charset=gb2312" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Clob對(duì)象的存取測(cè)試</title>
</head>
<body>
<form name="test" method="post" action="clobTest.action">
<table width="80%" height="88" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td height="30" align="center">輸入ID號(hào)<input type="text" name="ID">
</tr>
<tr>
<td align="center">
<textarea rows="28" cols="68" name="CONTENTS">
注冊(cè)用戶需遵守:
尊重會(huì)員個(gè)人隱私、保障會(huì)員隱私安全是CSDN的一項(xiàng)基本政策,CSDN不會(huì)公開(kāi)、編輯或透露會(huì)員的注冊(cè)資料,除非符合以下情況: (1) 根據(jù)中華人民共和國(guó)國(guó)家安全機(jī)構(gòu)、公安部門(mén)的要求及根據(jù)相應(yīng)的法律程序要求。 (2) 維護(hù)CSDN的商標(biāo)所有權(quán)及其它權(quán)益。 (3) 在緊急情況下竭力維護(hù)會(huì)員個(gè)人、其它社會(huì)個(gè)體和社會(huì)大眾的安全。 (4) 嚴(yán)重違反CSDN有關(guān)規(guī)定。 CSDN保留結(jié)束會(huì)員使用網(wǎng)絡(luò)服務(wù)資格的權(quán)利,并保證結(jié)束會(huì)員資格后仍為會(huì)員保密所有個(gè)人隱私。
</textarea>
</td>
</tr>
<tr>
<td align="center">
<input type="submit" name="Submit" value="提交">
</td>
</tr>
</table>
</form>
</body>
</html>
點(diǎn)擊”提交”按鈕,我們?cè)诤笈_(tái)的到的是2個(gè)String類型的對(duì)象
String strID = request.getParameter(“ID”);
String strCONTENTS = request.getParameter(“CONTENTS”);
接著我們要做的任務(wù)就是如何把String類型CONTENTS存到數(shù)據(jù)庫(kù)中的CLOB類型字段中!
注意:LOB數(shù)據(jù)不能象其它類型數(shù)據(jù)一樣直接插入(INSERT)。插入前必須先插入一個(gè)空的LOB對(duì)象,CLOB類型的空對(duì)象為EMPTY_CLOB(),BLOB類型的空對(duì)象為EMPTY_BLOB()。之后通過(guò)SELECT命令查詢得到先前插入的記錄并鎖定,繼而將空對(duì)象修改為所要插入的LOB對(duì)象。
//我們先插入一個(gè)空的CLOB對(duì)象
public int insertEmptyClob() throws Exception {
Statement statement = null;
int intResult = -1;
try {
//創(chuàng)建數(shù)據(jù)庫(kù)操作語(yǔ)句
statement = connection.createStatement();
//定義SQL語(yǔ)句
String strSQL = “INSET INTO TEST (ID,CONTENTS) VALUES(strID, EMPTY_CLOB())”;
//執(zhí)行SQL語(yǔ)句
intResult = statement.executeUpdate(strSQL);
System.out.println(" intResult valus is"+intResult);
return intResult;
} catch(Exception e) {
e.printStackTrace();
return -1;
} finally {
if (statement != null) {
statement.close();
}
}
}
//把strCONTENT插入CLOB字段
public void insertClob() throws Exception {
Statement statement = null;
ResultSet resultset = null;
try {
//設(shè)置不自動(dòng)提交
connection.setAutoCommit(false);
//創(chuàng)建數(shù)據(jù)庫(kù)操作語(yǔ)句
statement = connection.createStatement();
//定義SQL語(yǔ)句
String strSQL = “SELECT CONTENTS FROM TEST WHERE ID=strID"”
resultset = statement.executeQuery(strSQL);
oracle.sql.CLOB contents = null;
while(resultset.next()) {
//取出CLOB對(duì)象
contents = (oracle.sql.CLOB)resultset.getClob("CONTENTS");
}
Writer out = contents.getCharacterOutputStream();
out.write(strContents);
out.flush();
out.close();
//數(shù)據(jù)庫(kù)提交
connection.commit();
} catch(Exception e) {
e.printStackTrace();
}finally{
if(resultset != null) {
resultset.close();
}
if(statement != null) {
statement.close();
}
}
}
OK,我們已經(jīng)把這段文本以CLOB字段的形式保存到數(shù)據(jù)庫(kù)中了,在實(shí)際應(yīng)用中,如果要保存或修改一條記錄,我們要分2步做,先保存或修改非LOB字段類型的字段,再保存或修改LOB字段!接下來(lái)我們來(lái)把剛才保存到數(shù)據(jù)庫(kù)中的CLOB字段讀到j(luò)sp頁(yè)面中去。
我們?cè)诒4娴臅r(shí)候,CLOB字段會(huì)把上面textarea中的文本按原來(lái)的格式一行一行(包括空格)都保存到CLOB字段中,讀取的時(shí)候我們只要按照原來(lái)格式讀起出來(lái)就行了(我這里自己用了一個(gè)小處理方法,但如果你有更好的方法請(qǐng)告訴我)。在這里我們把CLOB讀到StringBuffer中,為了保存不同行我在行之間加了個(gè)“&”字符來(lái)區(qū)分。最后轉(zhuǎn)化成String
放到VO中,這樣就保證從前臺(tái)到后臺(tái),從后臺(tái)到前臺(tái)的數(shù)據(jù)傳遞的一致性!代碼如下:
/**
* 獲取CLOB文本對(duì)象
* @param sbSQL
* @return
* @throws java.lang.Exception
*/
public String selectIncludeClob(StringBuffer sbSQL) throws Exception {
Statement stmt = null;
ResultSet rs = null;
StringBuffer sbResult = new StringBuffer();
try {
//設(shè)定數(shù)據(jù)庫(kù)不自動(dòng)提交
//connection.setAutoCommit(false);
//創(chuàng)建數(shù)據(jù)庫(kù)操作語(yǔ)句
stmt = connection.createStatement();
//獲取結(jié)果集
rs = stmt.executeQuery(sbSQL.toString());
while(rs.next()) {
CLOB clob = (CLOB)rs.getClob("CONTENTS");
Reader isClob = clob.getCharacterStream();
BufferedReader bfClob = new BufferedReader(isClob);
String strClob = bfClob.readLine();
while(strClob != null) {
sbResult.append(strClob);
sbResult.append("&");
strClob = bfClob.readLine();
}
}
//提交事務(wù)
// connection.commit();
} catch(Exception e) {
e.printStackTrace();
throw e;
} finally {
if(rs != null) {
rs.close();
}
if(stmt != null) {
stmt.close();
}
}
return sbResult.toString();
}
到j(luò)sp頁(yè)面中,我們從VO中獲取改文本信息。
<textarea rows="42" cols="68" name="CONTENTS" style="border-style: solid; border-color: #FFFFFF; font-family:仿宋_GB2312; font-size:14pt; line-height:200%; margin-top:8; margin-bottom:6" >
<%
String content = vo.getContent();
String[] contentArray = content.split("&");
for(int i=0;i<contentArray.length;i++) {
String s= contentArray[i];
out.println(s);
}
%>
</textarea>
這樣我們就保證什么格式保存就以什么格式顯示。
2.BLOB字段,二進(jìn)制LOB,主要存儲(chǔ)二進(jìn)制數(shù)據(jù),最長(zhǎng)為4G,在J2EE程序中,一般類似于圖片和文件的保存。當(dāng)然也有另一種方法,就把圖片和文件保存在硬盤(pán)上,數(shù)據(jù)庫(kù)中只保存圖片的鏈接地址和文件在服務(wù)器上的路徑。如果遇到文件和圖片比較重要的還是需要保存到數(shù)據(jù)庫(kù)中(例如:我們做國(guó)土資源項(xiàng)目的時(shí)候,好多圖片、文件就很重要,需要保存到數(shù)據(jù)庫(kù)中),下面我寫(xiě)一個(gè)保存文件到數(shù)據(jù)庫(kù)的Blob字段和從數(shù)據(jù)庫(kù)的Blob字段中獲取文件的方法(當(dāng)然完全應(yīng)用還要做其他工作,這里就不多說(shuō)了,如果你不清楚的可以問(wèn)我):
/**
* 把上傳的文件保存到數(shù)據(jù)庫(kù)的Blob字段中
* @param strTableName 對(duì)應(yīng)的表名稱
* @param strColumnName 表中保存文件的Blob字段名稱
* @param inputStream 輸入的文件流
* @param sbSQLWhere where條件
* @throws java.lang.Exception
*/
public static void fileUpload(String strTableName,
String strColumnName,
InputStream inputStream,
StringBuffer sbSQLWhere)
throws Exception {
Connection con = null;
ResultSet resultset = null;
Statement stmt = null;
try {
//得到數(shù)據(jù)庫(kù)連接
con = DBConnector.getConnection();
//構(gòu)建查詢語(yǔ)句
StringBuffer sbSQL = new StringBuffer();
sbSQL.append(" UPDATE ");
sbSQL.append(strTableName);
sbSQL.append(" SET ");
sbSQL.append(strColumnName);
sbSQL.append("=EMPTY_BLOB() ");
sbSQL.append(sbSQLWhere);
System.out.println(" update sql value is*******"+sbSQL.toString());
//獲取數(shù)據(jù)庫(kù)操作語(yǔ)句
stmt=con.createStatement();
//插入空的blob對(duì)象
stmt.executeUpdate(sbSQL.toString());
con.setAutoCommit(false);
StringBuffer sbSQLBlob = new StringBuffer();
sbSQLBlob.append(" SELECT ");
sbSQLBlob.append(strColumnName);
sbSQLBlob.append(" FROM ");
sbSQLBlob.append(strTableName);
sbSQLBlob.append(sbSQLWhere);
sbSQLBlob.append(" FOR UPDATE");
System.out.println(" select sql value is*********"+sbSQL.toString());
resultset =stmt.executeQuery(sbSQLBlob.toString());
while (resultset.next()) {
/* 取出此BLOB對(duì)象 */
oracle.sql.BLOB blob = (oracle.sql.BLOB)resultset.getBlob("BODY");
/* 向BLOB對(duì)象中寫(xiě)入數(shù)據(jù) */
BufferedOutputStream out = new BufferedOutputStream(blob.getBinaryOutputStream());
BufferedInputStream in = new BufferedInputStream(inputStream);
int c;
while ((c=in.read())!=-1) {
out.write(c);
}
in.close();
out.close();
}
con.setAutoCommit(false);
con.commit();
} catch (Exception ex) {
ex.printStackTrace();
throw ex;
} finally {
if (stmt != null) {
stmt.close();
}
if (resultset != null) {
resultset.close();
}
if (con!=null) {
con.close();
}
}
}
下面的方法是從數(shù)據(jù)庫(kù)中得到上傳的文件的輸入流,把輸入流寫(xiě)到servlet流中,再?gòu)捻?yè)面中獲取,servlet就不寫(xiě)了。
/**
* 方法描述:得到數(shù)據(jù)庫(kù)上傳的文件數(shù)據(jù)
*
* 輸入?yún)?shù): 1:表名(String)
* 2:字段名(String)
* 3: Where條件(StringBuffer)
* 5: 輸出流(ServletOutputStream)
*
* 輸出參數(shù):void
* 編寫(xiě)人: */
public static void getdownFile(String strTableName,
String strColumnName,
StringBuffer sbSQLWhere,
ServletOutputStream sos) throws Exception {
Connection con = null;
PreparedStatement ps = null;
ResultSet resultset = null;
try {
//得到數(shù)據(jù)庫(kù)連接
con = DBConnector.getConnection();
StringBuffer sbSQL = new StringBuffer();
//構(gòu)建查詢語(yǔ)句
sbSQL.append(" SELECT " + strColumnName + " FROM " + strTableName);
sbSQL.append(sbSQLWhere);
System.out.println(" sql value is:"+sbSQLWhere.toString());
ps = con.prepareStatement(sbSQL.toString());
//執(zhí)行查詢
resultset = ps.executeQuery();
while (resultset.next()) {
//讀取數(shù)據(jù)流
InputStream is = resultset.getBinaryStream(strColumnName);
byte[] buf = new byte[2048];
while(is.read(buf)!=-1) {
//把數(shù)據(jù)流按塊寫(xiě)到servlet的輸出流中
sos.write(buf);
}
}
} catch (Exception ex) {
ex.printStackTrace();
throw ex;
} finally {
if (ps != null) {
ps.close();
}
if (resultset != null) {
resultset.close();
}
if (con!=null) {
con.close();
}
}
}
圖片的保存和文件的保存一樣,如果不清楚的可以和我聯(lián)系
后記:
平時(shí)總忙著做項(xiàng)目,閑的時(shí)候也很懶,總想把自己實(shí)際中的一些問(wèn)題和解決方法小結(jié)一下,但總沒(méi)完成,這是第一次寫(xiě),寫(xiě)的不好或不清楚的地方請(qǐng)包涵,下次改進(jìn),也希望大家多提意見(jiàn),大家一起進(jìn)步!!!!!!!!!!! |
|
方法1:
- xmlhttp.setRequestHeader("Cache-Control","no-cache");
- url += "&random="+Math.random();
- url += "×tamp="+new Date().getTime();
方法二:
//處理頁(yè)面緩存
??response.setHeader("Pragma","No-cache");
??response.setHeader("Cache-Control","no-cache");
??response.setDateHeader("Expires", 0);
正則表達(dá)式經(jīng)典 (轉(zhuǎn))
"^\d+$" //非負(fù)整數(shù)(正整數(shù) + 0)
"^[0-9]*[1-9][0-9]*$" //正整數(shù)
"^((-\d+)|(0+))$" //非正整數(shù)(負(fù)整數(shù) + 0)
"^-[0-9]*[1-9][0-9]*$" //負(fù)整數(shù)
"^-?\d+$" //整數(shù)
"^\d+(\.\d+)?$" //非負(fù)浮點(diǎn)數(shù)(正浮點(diǎn)數(shù) + 0)
"^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$" //正浮點(diǎn)數(shù)
"^((-\d+(\.\d+)?)|(0+(\.0+)?))$" //非正浮點(diǎn)數(shù)(負(fù)浮點(diǎn)數(shù) + 0)
"^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$" //負(fù)浮點(diǎn)數(shù)
"^(-?\d+)(\.\d+)?$" //浮點(diǎn)數(shù)
"^[A-Za-z]+$" //由26個(gè)英文字母組成的字符串
"^[A-Z]+$" //由26個(gè)英文字母的大寫(xiě)組成的字符串
"^[a-z]+$" //由26個(gè)英文字母的小寫(xiě)組成的字符串
"^[A-Za-z0-9]+$" //由數(shù)字和26個(gè)英文字母組成的字符串
"^\w+$" //由數(shù)字、26個(gè)英文字母或者下劃線組成的字符串
"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$" //email地址
"^[a-zA-z]+://(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\S*)?$"
----------------------------------------------------------------------------------------------------------------------
字符描述: \:將下一個(gè)字符標(biāo)記為特殊字符或字面值。例如"n"與字符"n"匹配。"\n"與換行符匹配。序列"\\"與"\"匹配,"\("與"("匹配。
^ :匹配輸入的開(kāi)始位置。
$ :匹配輸入的結(jié)尾。
* :匹配前一個(gè)字符零次或幾次。例如,"zo*"可以匹配"z"、"zoo"。
+ :匹配前一個(gè)字符一次或多次。例如,"zo+"可以匹配"zoo",但不匹配"z"。
? :匹配前一個(gè)字符零次或一次。例如,"a?ve?"可以匹配"never"中的"ve"。
.:匹配換行符以外的任何字符。
(pattern) 與模式匹配并記住匹配。匹配的子字符串可以從作為結(jié)果的 Matches 集合中使用 Item [0]...[n]取得。如果要匹配括號(hào)字符(和 ),可使用"\(" 或 "\)"。
x|y:匹配 x 或 y。例如 "z|food" 可匹配 "z" 或 "food"。"(z|f)ood" 匹配 "zoo" 或 "food"。
{n}:n 為非負(fù)的整數(shù)。匹配恰好n次。例如,"o{2}" 不能與 "Bob 中的 "o" 匹配,但是可以與"foooood"中的前兩個(gè)o匹配。
{n,} :n 為非負(fù)的整數(shù)。匹配至少n次。例如,"o{2,}"不匹配"Bob"中的"o",但是匹配"foooood"中所有的o。"o{1,}"等價(jià)于"o+"。"o{0,}"等價(jià)于"o*"。
{n,m} :m 和 n 為非負(fù)的整數(shù)。匹配至少 n 次,至多 m 次。例如,"o{1,3}" 匹配 "fooooood"中前三個(gè)o。"o{0,1}"等價(jià)于"o?"。
[xyz] :一個(gè)字符集。與括號(hào)中字符的其中之一匹配。例如,"[abc]" 匹配"plain"中的"a"。
[^xyz] :一個(gè)否定的字符集。匹配不在此括號(hào)中的任何字符。例如,"[^abc]" 可以匹配"plain"中的"p".
[a-z] :表示某個(gè)范圍內(nèi)的字符。與指定區(qū)間內(nèi)的任何字符匹配。例如,"[a-z]"匹配"a"與"z"之間的任何一個(gè)小寫(xiě)字母字符。
[^m-z] :否定的字符區(qū)間。與不在指定區(qū)間內(nèi)的字符匹配。例如,"[m-z]"與不在"m"到"z"之間的任何字符匹配。
\b :與單詞的邊界匹配,即單詞與空格之間的位置。例如,"er\b" 與"never"中的"er"匹配,但是不匹配"verb"中的"er"。
\B :與非單詞邊界匹配。"ea*r\B"與"never early"中的"ear"匹配。
\d :與一個(gè)數(shù)字字符匹配。等價(jià)于[0-9]。
\D :與非數(shù)字的字符匹配。等價(jià)于[^0-9]。
\f :與分頁(yè)符匹配。
\n :與換行符字符匹配。
\r :與回車(chē)字符匹配。
\s :與任何白字符匹配,包括空格、制表符、分頁(yè)符等。等價(jià)于"[ \f\n\r\t\v]"。
\S :與任何非空白的字符匹配。等價(jià)于"[^ \f\n\r\t\v]"。
\t :與制表符匹配。
\v :與垂直制表符匹配。
\w :與任何單詞字符匹配,包括下劃線。等價(jià)于"[A-Za-z0-9_]"。
\W :與任何非單詞字符匹配。等價(jià)于"[^A-Za-z0-9_]"。
\num :匹配 num個(gè),其中 num 為一個(gè)正整數(shù)。引用回到記住的匹配。例如,"(.)\1"匹配兩個(gè)連續(xù)的相同的字符。
\n:匹配 n,其中n 是一個(gè)八進(jìn)制換碼值。八進(jìn)制換碼值必須是 1, 2 或 3 個(gè)數(shù)字長(zhǎng)。
例如,"\11" 和 "\011" 都與一個(gè)制表符匹配。"\0011"等價(jià)于"\001" 與 "1"。八進(jìn)制換碼值不得超過(guò) 256。否則,只有前兩個(gè)字符被視為表達(dá)式的一部分。允許在正則表達(dá)式中使用ASCII碼。
\xn:匹配n,其中n是一個(gè)十六進(jìn)制的換碼值。十六進(jìn)制換碼值必須恰好為兩個(gè)數(shù)字長(zhǎng)。例如,"\x41"匹配"A"。"\x041"等價(jià)于"\x04" 和 "1"。允許在正則表達(dá)式中使用 ASCII 碼。
好了,常用的方法和屬性就是這些了,上面的語(yǔ)法介紹的已經(jīng)很詳細(xì)了,我們就沒(méi)有必要在羅嗦了,接下來(lái)我們來(lái)看看在具體的例子里面如何使用這些方法和屬性來(lái)校驗(yàn)數(shù)據(jù)的合法性,我們還是舉個(gè)例子吧,比如,我們想要對(duì)用戶輸入的電子郵件進(jìn)行校驗(yàn),那么,什么樣的數(shù)據(jù)才算是一個(gè)合法的電子郵件呢?我可以這樣輸入:uestc95@263.net,當(dāng)然我也會(huì)這樣輸入:xxx@yyy.com.cn,但是這樣的輸入就是非法的:xxx@@com.cn或者@xxx.com.cn,等等,所以我們得出一個(gè)合法的電子郵件地址至少應(yīng)當(dāng)滿足以下幾個(gè)條件:
1. 必須包含一個(gè)并且只有一個(gè)符號(hào)“@”
2. 必須包含至少一個(gè)至多三個(gè)符號(hào)“.”
3. 第一個(gè)字符不得是“@”或者“.”
4. 不允許出現(xiàn)“@.”或者.@
5. 結(jié)尾不得是字符“@”或者“.”
所以根據(jù)以上的原則和上面表中的語(yǔ)法,我們很容易的就可以得到需要的模板如下:"(\w)+[@]{1}(\w)+[.]{1,3}(\w)+"
接下來(lái)我們仔細(xì)分析一下這個(gè)模板,首先“\w”表示郵件的開(kāi)始字符只能是包含下劃線的單詞字符,這樣,滿足了第三個(gè)條件;“[@]{1}”表示在電子郵件中應(yīng)當(dāng)匹配并且只能匹配一次字符“@”,滿足了條件一;同樣的“[.]{1,3}”表示在電子郵件中至少匹配1個(gè)至多匹配3個(gè)字符“.” ,滿足了第二個(gè)條件;模板最后的“(\w)+”表示結(jié)尾的字符只能是包含下劃線在內(nèi)的單詞字符,滿足了條件五;模板中間的“(\w)+”滿足了條件四。
然后,我們就直接調(diào)用剛才的那個(gè)函數(shù)CheckExp("(\w)+[@]{1}(\w)+[.]{1}(\w)+",待校驗(yàn)的字符串)就好了,如果返回True就表示數(shù)據(jù)是合法的,否則就是不正確的,怎么樣,簡(jiǎn)單吧。我們還可以寫(xiě)出來(lái)校驗(yàn)身份證號(hào)碼的模板:"([0-9]){15}";校驗(yàn)URL的模板:"^http://{1}((\w)+[.]){1,3}"等等;我們可以看到,這些模板為我們提供了很好的可重利用的模塊,利用自己或者別人提供的各種模板,我們就可以方便快捷的進(jìn)行數(shù)據(jù)的合法性校驗(yàn)了,相信你一定會(huì)寫(xiě)出非常通用的模板的。
這樣,我們只要定制不同的模板,就可以實(shí)現(xiàn)對(duì)不同數(shù)據(jù)的合法性校驗(yàn)了。所以,正則表達(dá)式對(duì)象中最重要的屬性就是:“Pattern”屬性,只要真正掌握了這個(gè)屬性,才可以自由的運(yùn)用正則表達(dá)式對(duì)象來(lái)為我們的數(shù)據(jù)校驗(yàn)進(jìn)行服務(wù)。
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=560411
TCP協(xié)議
==> TCP首部
??? 源端口號(hào)、目的端口號(hào)、位序號(hào)、位確認(rèn)序號(hào)、首部長(zhǎng)度、標(biāo)志位、窗口大小、檢驗(yàn)和、緊急指針和其它選項(xiàng)。
???
??? 一個(gè)IP地址和一個(gè)端口號(hào)也成為一個(gè)插口(socket)。插口對(duì)可唯一確定互聯(lián)網(wǎng)中每個(gè)TCP連接的雙方。
==> TCP連接的建立與終止
??? TCP是一個(gè)面向連接的協(xié)議,無(wú)論哪方向另一方發(fā)送數(shù)據(jù)之前,都必須先在雙方之間建立一條連接。
??? TCP連接的建立——三次握手。
??? TCP連接的終止——四次握手。這是由TCP的半關(guān)閉造成的。因?yàn)門(mén)CP是全雙工的,因此每個(gè)方向必須單獨(dú)的進(jìn)行關(guān)閉。
==> 最大報(bào)文段長(zhǎng)度MSS
??? MSS越大,允許每個(gè)報(bào)文段傳遞的數(shù)據(jù)越多,相對(duì)TCP和IP的首部有更高的利用率。
??? 有些情況下,MSS是可以在建立TCP連接時(shí)進(jìn)行協(xié)商的選項(xiàng),但是有些情況下不行 。
??? * 如果是本地網(wǎng)絡(luò),TCP可以根據(jù)網(wǎng)絡(luò)外出接口處的MTU值減去固定的IP首部(20)和TCP長(zhǎng)度(20),對(duì)于以太網(wǎng),可以達(dá)到1460。
??? * 如果IP地址為非本地的,則MSS通常定為默認(rèn)值536字節(jié)(允許20字節(jié)的IP首部和20字節(jié)的TCP首部以適合576字節(jié)的IP數(shù)據(jù)報(bào))。
??? MSS讓主機(jī)限制另一端發(fā)送數(shù)據(jù)的長(zhǎng)度,同時(shí)也能控制它自己發(fā)送數(shù)據(jù)報(bào)的長(zhǎng)度,避免較小MTU發(fā)生分片。
==> TCP的半關(guān)閉
??? TCP連接的一端在結(jié)束它的發(fā)送后還能接收來(lái)自另一端數(shù)據(jù)(直到它也發(fā)送FIN)的能力,這就是所謂的半關(guān)閉。應(yīng)用程序很少用到。
==> 復(fù)位報(bào)文段
??? * 不存在的端口(目的端口沒(méi)有進(jìn)程監(jiān)聽(tīng))。目的主機(jī)將對(duì)SYN請(qǐng)求返回一個(gè)RST報(bào)文段。(UDP則將產(chǎn)生一個(gè)端口不可達(dá)的信息)
??? * 異常終止。
??? * 檢測(cè)半打開(kāi)的連接。
==> TCP服務(wù)器的設(shè)計(jì)
??? * 大多數(shù)TCP服務(wù)器的進(jìn)程是并發(fā)的.
??? * 只有處于監(jiān)聽(tīng)的進(jìn)程才能處理客戶端的連接請(qǐng)求.
??? * TCP服務(wù)器可以對(duì)本地IP地址進(jìn)行限制,但是一般不能對(duì)遠(yuǎn)程IP地址進(jìn)行限制.
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=561075
關(guān)于Java棧與堆的思考
1. 棧(stack)與堆(heap)都是Java用來(lái)在Ram中存放數(shù)據(jù)的地方。與C++不同,Java自動(dòng)管理?xiàng):投眩绦騿T不能直接地設(shè)置棧或堆。
2. 棧的優(yōu)勢(shì)是,存取速度比堆要快,僅次于直接位于CPU中的寄存器。但缺點(diǎn)是,存在棧中的數(shù)據(jù)大小與生存期必須是確定的,缺乏靈活性。另外,棧數(shù)據(jù)可以共享,詳見(jiàn)第3點(diǎn)。堆的優(yōu)勢(shì)是可以動(dòng)態(tài)地分配內(nèi)存大小,生存期也不必事先告訴編譯器,Java的垃圾收集器會(huì)自動(dòng)收走這些不再使用的數(shù)據(jù)。但缺點(diǎn)是,由于要在運(yùn)行時(shí)動(dòng)態(tài)分配內(nèi)存,存取速度較慢。
3. Java中的數(shù)據(jù)類型有兩種。
? 一種是基本類型(primitive types), 共有8種,即int, short, long, byte, float, double, boolean, char(注意,并沒(méi)有string的基本類型)。這種類型的定義是通過(guò)諸如int a = 3; long b = 255L;的形式來(lái)定義的,稱為自動(dòng)變量。值得注意的是,自動(dòng)變量存的是字面值,不是類的實(shí)例,即不是類的引用,這里并沒(méi)有類的存在。如int a = 3; 這里的a是一個(gè)指向int類型的引用,指向3這個(gè)字面值。這些字面值的數(shù)據(jù),由于大小可知,生存期可知(這些字面值固定定義在某個(gè)程序塊里面,程序塊退出后,字段值就消失了),出于追求速度的原因,就存在于棧中。
? 另外,棧有一個(gè)很重要的特殊性,就是存在棧中的數(shù)據(jù)可以共享。假設(shè)我們同時(shí)定義
? int a = 3;
? int b = 3;
?? 編譯器先處理int a = 3;首先它會(huì)在棧中創(chuàng)建一個(gè)變量為a的引用,然后查找有沒(méi)有字面值為3的地址,沒(méi)找到,就開(kāi)辟一個(gè)存放3這個(gè)字面值的地址,然后將a指向3的地址。接著處理int b = 3;在創(chuàng)建完b的引用變量后,由于在棧中已經(jīng)有3這個(gè)字面值,便將b直接指向3的地址。這樣,就出現(xiàn)了a與b同時(shí)均指向3的情況。
?? 特別注意的是,這種字面值的引用與類對(duì)象的引用不同。假定兩個(gè)類對(duì)象的引用同時(shí)指向一個(gè)對(duì)象,如果一個(gè)對(duì)象引用變量修改了這個(gè)對(duì)象的內(nèi)部狀態(tài),那么另一個(gè)對(duì)象引用變量也即刻反映出這個(gè)變化。相反,通過(guò)字面值的引用來(lái)修改其值,不會(huì)導(dǎo)致另一個(gè)指向此字面值的引用的值也跟著改變的情況。如上例,我們定義完a與 b的值后,再令a=4;那么,b不會(huì)等于4,還是等于3。在編譯器內(nèi)部,遇到a=4;時(shí),它就會(huì)重新搜索棧中是否有4的字面值,如果沒(méi)有,重新開(kāi)辟地址存放4的值;如果已經(jīng)有了,則直接將a指向這個(gè)地址。因此a值的改變不會(huì)影響到b的值。
? 另一種是包裝類數(shù)據(jù),如Integer, String, Double等將相應(yīng)的基本數(shù)據(jù)類型包裝起來(lái)的類。這些類數(shù)據(jù)全部存在于堆中,Java用new()語(yǔ)句來(lái)顯示地告訴編譯器,在運(yùn)行時(shí)才根據(jù)需要?jiǎng)討B(tài)創(chuàng)建,因此比較靈活,但缺點(diǎn)是要占用更多的時(shí)間。
4. String是一個(gè)特殊的包裝類數(shù)據(jù)。即可以用String str = new String("abc");的形式來(lái)創(chuàng)建,也可以用String str = "abc";的形式來(lái)創(chuàng)建(作為對(duì)比,在JDK 5.0之前,你從未見(jiàn)過(guò)Integer i = 3;的表達(dá)式,因?yàn)轭惻c字面值是不能通用的,除了String。而在JDK 5.0中,這種表達(dá)式是可以的!因?yàn)榫幾g器在后臺(tái)進(jìn)行Integer i = new Integer(3)的轉(zhuǎn)換)。前者是規(guī)范的類的創(chuàng)建過(guò)程,即在Java中,一切都是對(duì)象,而對(duì)象是類的實(shí)例,全部通過(guò)new()的形式來(lái)創(chuàng)建。Java 中的有些類,如DateFormat類,可以通過(guò)該類的getInstance()方法來(lái)返回一個(gè)新創(chuàng)建的類,似乎違反了此原則。其實(shí)不然。該類運(yùn)用了單例模式來(lái)返回類的實(shí)例,只不過(guò)這個(gè)實(shí)例是在該類內(nèi)部通過(guò)new()來(lái)創(chuàng)建的,而getInstance()向外部隱藏了此細(xì)節(jié)。那為什么在String str = "abc";中,并沒(méi)有通過(guò)new()來(lái)創(chuàng)建實(shí)例,是不是違反了上述原則?其實(shí)沒(méi)有。
5. 關(guān)于String str = "abc"的內(nèi)部工作。Java內(nèi)部將此語(yǔ)句轉(zhuǎn)化為以下幾個(gè)步驟:
? (1)先定義一個(gè)名為str的對(duì)String類的對(duì)象引用變量:String str;
?? (2)在棧中查找有沒(méi)有存放值為"abc"的地址,如果沒(méi)有,則開(kāi)辟一個(gè)存放字面值為"abc"的地址,接著創(chuàng)建一個(gè)新的String類的對(duì)象o,并將o 的字符串值指向這個(gè)地址,而且在棧中這個(gè)地址旁邊記下這個(gè)引用的對(duì)象o。如果已經(jīng)有了值為"abc"的地址,則查找對(duì)象o,并返回o的地址。
? (3)將str指向?qū)ο髈的地址。
? 值得注意的是,一般String類中字符串值都是直接存值的。但像String str = "abc";這種場(chǎng)合下,其字符串值卻是保存了一個(gè)指向存在棧中數(shù)據(jù)的引用!
?
? 為了更好地說(shuō)明這個(gè)問(wèn)題,我們可以通過(guò)以下的幾個(gè)代碼進(jìn)行驗(yàn)證。
? String str1 = "abc";
? String str2 = "abc";
? System.out.println(str1==str2);? //true
?
? 注意,我們這里并不用str1.equals(str2);的方式,因?yàn)檫@將比較兩個(gè)字符串的值是否相等。==號(hào),根據(jù)JDK的說(shuō)明,只有在兩個(gè)引用都指向了同一個(gè)對(duì)象時(shí)才返回真值。而我們?cè)谶@里要看的是,str1與str2是否都指向了同一個(gè)對(duì)象。
? 結(jié)果說(shuō)明,JVM創(chuàng)建了兩個(gè)引用str1和str2,但只創(chuàng)建了一個(gè)對(duì)象,而且兩個(gè)引用都指向了這個(gè)對(duì)象。
? 我們?cè)賮?lái)更進(jìn)一步,將以上代碼改成:
? String str1 = "abc";
? String str2 = "abc";
? str1 = "bcd";
? System.out.println(str1 + "," + str2);? //bcd, abc
? System.out.println(str1==str2);? //false
? 這就是說(shuō),賦值的變化導(dǎo)致了類對(duì)象引用的變化,str1指向了另外一個(gè)新對(duì)象!而str2仍舊指向原來(lái)的對(duì)象。上例中,當(dāng)我們將str1的值改為"bcd"時(shí),JVM發(fā)現(xiàn)在棧中沒(méi)有存放該值的地址,便開(kāi)辟了這個(gè)地址,并創(chuàng)建了一個(gè)新的對(duì)象,其字符串的值指向這個(gè)地址。
?? 事實(shí)上,String類被設(shè)計(jì)成為不可改變(immutable)的類。如果你要改變其值,可以,但JVM在運(yùn)行時(shí)根據(jù)新值悄悄創(chuàng)建了一個(gè)新對(duì)象,然后將這個(gè)對(duì)象的地址返回給原來(lái)類的引用。這個(gè)創(chuàng)建過(guò)程雖說(shuō)是完全自動(dòng)進(jìn)行的,但它畢竟占用了更多的時(shí)間。在對(duì)時(shí)間要求比較敏感的環(huán)境中,會(huì)帶有一定的不良影響。
? 再修改原來(lái)代碼:
? String str1 = "abc";
? String str2 = "abc";
?
? str1 = "bcd";
?
? String str3 = str1;
? System.out.println(str3);? //bcd
? String str4 = "bcd";
? System.out.println(str1 == str4);? //true
???
? str3 這個(gè)對(duì)象的引用直接指向str1所指向的對(duì)象(注意,str3并沒(méi)有創(chuàng)建新對(duì)象)。當(dāng)str1改完其值后,再創(chuàng)建一個(gè)String的引用str4,并指向因str1修改值而創(chuàng)建的新的對(duì)象。可以發(fā)現(xiàn),這回str4也沒(méi)有創(chuàng)建新的對(duì)象,從而再次實(shí)現(xiàn)棧中數(shù)據(jù)的共享。
? 我們?cè)俳又匆韵碌拇a。
? String str1 = new String("abc");
? String str2 = "abc";
? System.out.println(str1==str2);? //false
? 創(chuàng)建了兩個(gè)引用。創(chuàng)建了兩個(gè)對(duì)象。兩個(gè)引用分別指向不同的兩個(gè)對(duì)象。
? String str1 = "abc";
? String str2 = new String("abc");
? System.out.println(str1==str2);? //false
? 創(chuàng)建了兩個(gè)引用。創(chuàng)建了兩個(gè)對(duì)象。兩個(gè)引用分別指向不同的兩個(gè)對(duì)象。
? 以上兩段代碼說(shuō)明,只要是用new()來(lái)新建對(duì)象的,都會(huì)在堆中創(chuàng)建,而且其字符串是單獨(dú)存值的,即使與棧中的數(shù)據(jù)相同,也不會(huì)與棧中的數(shù)據(jù)共享。
6. 數(shù)據(jù)類型包裝類的值不可修改。不僅僅是String類的值不可修改,所有的數(shù)據(jù)類型包裝類都不能更改其內(nèi)部的值。
7. 結(jié)論與建議:
?? (1)我們?cè)谑褂弥T如String str = "abc";的格式定義類時(shí),總是想當(dāng)然地認(rèn)為,我們創(chuàng)建了String類的對(duì)象str。擔(dān)心陷阱!對(duì)象可能并沒(méi)有被創(chuàng)建!唯一可以肯定的是,指向 String類的引用被創(chuàng)建了。至于這個(gè)引用到底是否指向了一個(gè)新的對(duì)象,必須根據(jù)上下文來(lái)考慮,除非你通過(guò)new()方法來(lái)顯要地創(chuàng)建一個(gè)新的對(duì)象。因此,更為準(zhǔn)確的說(shuō)法是,我們創(chuàng)建了一個(gè)指向String類的對(duì)象的引用變量str,這個(gè)對(duì)象引用變量指向了某個(gè)值為"abc"的String類。清醒地認(rèn)識(shí)到這一點(diǎn)對(duì)排除程序中難以發(fā)現(xiàn)的bug是很有幫助的。
? (2)使用String str = "abc";的方式,可以在一定程度上提高程序的運(yùn)行速度,因?yàn)镴VM會(huì)自動(dòng)根據(jù)棧中數(shù)據(jù)的實(shí)際情況來(lái)決定是否有必要?jiǎng)?chuàng)建新對(duì)象。而對(duì)于String str = new String("abc");的代碼,則一概在堆中創(chuàng)建新對(duì)象,而不管其字符串值是否相等,是否有必要?jiǎng)?chuàng)建新對(duì)象,從而加重了程序的負(fù)擔(dān)。這個(gè)思想應(yīng)該是享元模式的思想,但JDK的內(nèi)部在這里實(shí)現(xiàn)是否應(yīng)用了這個(gè)模式,不得而知。
? (3)當(dāng)比較包裝類里面的數(shù)值是否相等時(shí),用equals()方法;當(dāng)測(cè)試兩個(gè)包裝類的引用是否指向同一個(gè)對(duì)象時(shí),用==。
? (4)由于String類的immutable性質(zhì),當(dāng)String變量需要經(jīng)常變換其值時(shí),應(yīng)該考慮使用StringBuffer類,以提高程序效率。
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=561129
提高JSP應(yīng)用程序的七大秘籍絕招
你時(shí)常被客戶抱怨JSP頁(yè)面響應(yīng)速度很慢嗎?你想過(guò)當(dāng)客戶訪問(wèn)次數(shù)劇增時(shí),你的WEB應(yīng)用能承受日益增加的訪問(wèn)量嗎?本文講述了調(diào)整JSP和servlet的一些非常實(shí)用的方法,它可使你的servlet和JSP頁(yè)面響應(yīng)更快,擴(kuò)展性更強(qiáng)。而且在用戶數(shù)增加的情況下,系統(tǒng)負(fù)載會(huì)呈現(xiàn)出平滑上長(zhǎng)的趨勢(shì)。在本文中,我將通過(guò)一些實(shí)際例子和配置方法使得你的應(yīng)用程序的性能有出人意料的提升。
???????其中,某些調(diào)優(yōu)技術(shù)是在你的編程工作中實(shí)現(xiàn)的。而另一些技術(shù)是與應(yīng)用服務(wù)器的配置相關(guān)的。在本文中,我們將詳細(xì)地描述怎樣通過(guò)調(diào)整servlet和JSP頁(yè)面,來(lái)提高你的應(yīng)用程序的總體性能。在閱讀本文之前,假設(shè)你有基本的servlet和JSP的知識(shí)。
方法一:在servlet的init()方法中緩存數(shù)據(jù)
當(dāng)應(yīng)用服務(wù)器初始化servlet實(shí)例之后,為客戶端請(qǐng)求提供服務(wù)之前,它會(huì)調(diào)用這個(gè)servlet的init()方法。在一個(gè)servlet的生命周期中,init()方法只會(huì)被調(diào)用一次。通過(guò)在init()方法中緩存一些靜態(tài)的數(shù)據(jù)或完成一些只需要執(zhí)行一次的、耗時(shí)的操作,就可大大地提高系統(tǒng)性能。
例如,通過(guò)在init()方法中建立一個(gè)JDBC連接池是一個(gè)最佳例子,假設(shè)我們是用jdbc2.0的DataSource接口來(lái)取得數(shù)據(jù)庫(kù)連接,在通常的情況下,我們需要通過(guò)JNDI來(lái)取得具體的數(shù)據(jù)源。我們可以想象在一個(gè)具體的應(yīng)用中,如果每次SQL請(qǐng)求都要執(zhí)行一次JNDI查詢的話,那系統(tǒng)性能將會(huì)急劇下降。解決方法是如下代碼,它通過(guò)緩存DataSource,使得下一次SQL調(diào)用時(shí)仍然可以繼續(xù)利用它:
public class ControllerServlet extends HttpServlet
{
private javax.sql.DataSource testDS = null;
public void init(ServletConfig config) throws ServletException
{
super.init(config);
Context ctx = null;
try
{
ctx = new InitialContext();
testDS = (javax.sql.DataSource)ctx.lookup("jdbc/testDS");
}
catch(NamingException ne)
{
ne.printStackTrace();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public javax.sql.DataSource getTestDS()
{
return testDS;
}
...
...
} |
方法 2:禁止servlet和JSP 自動(dòng)重載(auto-reloading)
?????Servlet/JSP提供了一個(gè)實(shí)用的技術(shù),即自動(dòng)重載技術(shù),它為開(kāi)發(fā)人員提供了一個(gè)好的開(kāi)發(fā)環(huán)境,當(dāng)你改變servlet和JSP頁(yè)面后而不必重啟應(yīng)用服務(wù)器。然而,這種技術(shù)在產(chǎn)品運(yùn)行階段對(duì)系統(tǒng)的資源是一個(gè)極大的損耗,因?yàn)樗鼤?huì)給JSP引擎的類裝載器(classloader)帶來(lái)極大的負(fù)擔(dān)。因此關(guān)閉自動(dòng)重載功能對(duì)系統(tǒng)性能的提升是一個(gè)極大的幫助。
方法 3: 不要濫用HttpSession
?????在很多應(yīng)用中,我們的程序需要保持客戶端的狀態(tài),以便頁(yè)面之間可以相互聯(lián)系。但不幸的是由于HTTP具有天生無(wú)狀態(tài)性,從而無(wú)法保存客戶端的狀態(tài)。因此一般的應(yīng)用服務(wù)器都提供了session來(lái)保存客戶的狀態(tài)。在JSP應(yīng)用服務(wù)器中,是通過(guò)HttpSession對(duì)像來(lái)實(shí)現(xiàn)session的功能的,但在方便的同時(shí),它也給系統(tǒng)帶來(lái)了不小的負(fù)擔(dān)。因?yàn)槊慨?dāng)你獲得或更新session時(shí),系統(tǒng)者要對(duì)它進(jìn)行費(fèi)時(shí)的序列化操作。你可以通過(guò)對(duì)HttpSession的以下幾種處理方式來(lái)提升系統(tǒng)的性能。
????如果沒(méi)有必要,就應(yīng)該關(guān)閉JSP頁(yè)面中對(duì)HttpSession的缺省設(shè)置。 如果你沒(méi)有明確指定的話,每個(gè)JSP頁(yè)面都會(huì)缺省地創(chuàng)建一個(gè)HttpSession。如果你的JSP中不需要使用session的話,那可以通過(guò)如下的JSP頁(yè)面指示符來(lái)禁止它:
<%@ page session="false"%> |
不要在HttpSession中存放大的數(shù)據(jù)對(duì)像:如果你在HttpSession中存放大的數(shù)據(jù)對(duì)像的話,每當(dāng)對(duì)它進(jìn)行讀寫(xiě)時(shí),應(yīng)用服務(wù)器都將對(duì)其進(jìn)行序列化,從而增加了系統(tǒng)的額外負(fù)擔(dān)。你在HttpSession中存放的數(shù)據(jù)對(duì)像越大,那系統(tǒng)的性能就下降得越快。
?????當(dāng)你不需要HttpSession時(shí),盡快地釋放它:當(dāng)你不再需要session時(shí),你可以通過(guò)調(diào)用HttpSession.invalidate()方法來(lái)釋放它。盡量將session的超時(shí)時(shí)間設(shè)得短一點(diǎn):在JSP應(yīng)用服務(wù)器中,有一個(gè)缺省的session的超時(shí)時(shí)間。當(dāng)客戶在這個(gè)時(shí)間之后沒(méi)有進(jìn)行任何操作的話,系統(tǒng)會(huì)將相關(guān)的session自動(dòng)從內(nèi)存中釋放。超時(shí)時(shí)間設(shè)得越大,系統(tǒng)的性能就會(huì)越低,因此最好的方法就是盡量使得它的值保持在一個(gè)較低的水平。
方法 4: 將頁(yè)面輸出進(jìn)行壓縮
????壓縮是解決數(shù)據(jù)冗余的一個(gè)好的方法,特別是在網(wǎng)絡(luò)帶寬不夠發(fā)達(dá)的今天。有的瀏覽器支持gzip(GNU zip)進(jìn)行來(lái)對(duì)HTML文件進(jìn)行壓縮,這種方法可以戲劇性地減少HTML文件的下載時(shí)間。因此,如果你將servlet或JSP頁(yè)面生成的HTML頁(yè)面進(jìn)行壓縮的話,那用戶就會(huì)覺(jué)得頁(yè)面瀏覽速度會(huì)非常快。但不幸的是,不是所有的瀏覽器都支持gzip壓縮,但你可以通過(guò)在你的程序中檢查客戶的瀏覽器是否支持它。下面就是關(guān)于這種方法實(shí)現(xiàn)的一個(gè)代碼片段:
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
OutputStream out = null
String encoding = request.getHeader("Accept-Encoding");
if (encoding != null && encoding.indexOf("gzip") != -1)
{
request.setHeader("Content-Encoding" , "gzip");
out = new GZIPOutputStream(request.getOutputStream());
}
else if (encoding != null && encoding.indexOf("compress") != -1)
{
request.setHeader("Content-Encoding" , "compress");
out = new ZIPOutputStream(request.getOutputStream());
}
else
{
out = request.getOutputStream();
}
...
...
} |
方法 5: 使用線程池
????應(yīng)用服務(wù)器缺省地為每個(gè)不同的客戶端請(qǐng)求創(chuàng)建一個(gè)線程進(jìn)行處理,并為它們分派service()方法,當(dāng)service()方法調(diào)用完成后,與之相應(yīng)的線程也隨之撤消。由于創(chuàng)建和撤消線程會(huì)耗費(fèi)一定的系統(tǒng)資源,這種缺省模式降低了系統(tǒng)的性能。但所幸的是我們可以通過(guò)創(chuàng)建一個(gè)線程池來(lái)改變這種狀況。
????另外,我們還要為這個(gè)線程池設(shè)置一個(gè)最小線程數(shù)和一個(gè)最大線程數(shù)。在應(yīng)用服務(wù)器啟動(dòng)時(shí),它會(huì)創(chuàng)建數(shù)量等于最小線程數(shù)的一個(gè)線程池,當(dāng)客戶有請(qǐng)求時(shí),相應(yīng)地從池從取出一個(gè)線程來(lái)進(jìn)行處理,當(dāng)處理完成后,再將線程重新放入到池中。如果池中的線程不夠地話,系統(tǒng)會(huì)自動(dòng)地增加池中線程的數(shù)量,但總量不能超過(guò)最大線程數(shù)。通過(guò)使用線程池,當(dāng)客戶端請(qǐng)求急劇增加時(shí),系統(tǒng)的負(fù)載就會(huì)呈現(xiàn)的平滑的上升曲線,從而提高的系統(tǒng)的可伸縮性。
方法 6: 選擇正確的頁(yè)面包含機(jī)制
在JSP中有兩種方法可以用來(lái)包含另一個(gè)頁(yè)面:
1、使用include指示符
<%@ includee file=”test.jsp” %> |
2、使用jsp指示符
<jsp:includee page=”test.jsp” flush=”true”/> |
在實(shí)際中發(fā)現(xiàn),如果使用第一種方法的話,可以使得系統(tǒng)性能更高。
方法 7:正確地確定javabean的生命周期
??????JSP的一個(gè)強(qiáng)大的地方就是對(duì)javabean的支持。通過(guò)在JSP頁(yè)面中使用<jsp:useBean>標(biāo)簽,可以將javabean直接插入到一個(gè)JSP頁(yè)面中。它的使用方法如下:
<jsp:useBean id="name" scope="page|request|session|application" class="package.className" type="typeName">
</jsp:useBean> |
其中scope屬性指出了這個(gè)bean的生命周期。缺省的生命周期為page。如果你沒(méi)有正確地選擇bean的生命周期的話,它將影響系統(tǒng)的性能。
舉例來(lái)說(shuō),如果你只想在一次請(qǐng)求中使用某個(gè)bean,但你卻將這個(gè)bean的生命周期設(shè)置成了session,那當(dāng)這次請(qǐng)求結(jié)束后,這個(gè)bean將仍然保留在內(nèi)存中,除非session超時(shí)或用戶關(guān)閉瀏覽器。這樣會(huì)耗費(fèi)一定的內(nèi)存,并無(wú)謂的增加了JVM垃圾收集器的工作量。因此為bean設(shè)置正確的生命周期,并在bean的使命結(jié)束后盡快地清理它們,會(huì)使用系統(tǒng)性能有一個(gè)提高。
其它一些有用的方法
1、在字符串連接操作中盡量不使用“+”操作符:在java編程中,我們常常使用“+”操作符來(lái)將幾個(gè)字符串連接起來(lái),但你或許從來(lái)沒(méi)有想到過(guò)它居然會(huì)對(duì)系統(tǒng)性能造成影響吧?由于字符串是常量,因此JVM會(huì)產(chǎn)生一些臨時(shí)的對(duì)像。你使用的“+”越多,生成的臨時(shí)對(duì)像就越多,這樣也會(huì)給系統(tǒng)性能帶來(lái)一些影響。解決的方法是用StringBuffer對(duì)像來(lái)代替“+”操作符。
2、避免使用System.out.println()方法:由于System.out.println()是一種同步調(diào)用,即在調(diào)用它時(shí),磁盤(pán)I/O操作必須等待它的完成,因此我們要盡量避免對(duì)它的調(diào)用。但我們?cè)谡{(diào)試程序時(shí)它又是一個(gè)必不可少的方便工具,為了解決這個(gè)矛盾,我建議你最好使用Log4j工具(http://Jakarta.apache.org ),它既可以方便調(diào)試,而不會(huì)產(chǎn)生System.out.println()這樣的方法。
3、ServletOutputStream 與 PrintWriter的權(quán)衡:使用PrintWriter可能會(huì)帶來(lái)一些小的開(kāi)銷(xiāo),因?yàn)樗鼘⑺械脑驾敵龆嫁D(zhuǎn)換為字符流來(lái)輸出,因此如果使用它來(lái)作為頁(yè)面輸出的話,系統(tǒng)要負(fù)擔(dān)一個(gè)轉(zhuǎn)換過(guò)程。而使用ServletOutputStream作為頁(yè)面輸出的話就不存在一個(gè)問(wèn)題,但它是以二進(jìn)制進(jìn)行輸出的。因此在實(shí)際應(yīng)用中要權(quán)衡兩者的利弊。
總結(jié)
?????本文的目的是通過(guò)對(duì)servlet和JSP的一些調(diào)優(yōu)技術(shù)來(lái)極大地提高你的應(yīng)用程序的性能,并因此提升整個(gè)J2EE應(yīng)用的性能。通過(guò)這些調(diào)優(yōu)技術(shù),你可以發(fā)現(xiàn)其實(shí)并不是某種技術(shù)平臺(tái)(比如J2EE和.NET之爭(zhēng))決定了你的應(yīng)用程序的性能,重要是你要對(duì)這種平臺(tái)有一個(gè)較為深入的了解,這樣你才能從根本上對(duì)自己的應(yīng)用程序做一個(gè)優(yōu)化。
本文來(lái)自:http://www.tkk7.com/sgsoft/articles/2378.html
版權(quán)所有,轉(zhuǎn)載請(qǐng)聲明出處
zhyiwww@163.com
在讀我自己的認(rèn)識(shí)之前
,
我們先來(lái)看一下
servet
的結(jié)構(gòu)圖
:
以下是我自己的一點(diǎn)淺見(jiàn):
①
Servlet
在初始化的時(shí)候
,
是通過(guò)
init(ServletConfig? config)
或
init()
來(lái)執(zhí)行的。
ServletConfig
是一個(gè)接口,它怎樣傳遞給他一格對(duì)象來(lái)進(jìn)行初始化呢?其實(shí),是這個(gè)對(duì)象是由
servlet
容器來(lái)實(shí)例化的,由容器產(chǎn)生一格
ServletConfig
的實(shí)現(xiàn)類的對(duì)象,然后傳遞給
Servlet
結(jié)論:
ServletConfig
由容器實(shí)例化
②
我們有些時(shí)候可能在
Servlet
初始化時(shí)給它一些固定的配置參數(shù),那么這些參數(shù)是怎樣傳遞到
Servlet
呢?
其實(shí),我們?cè)?/span>
web.xml
中給
servlet
配置啟動(dòng)參數(shù),在容器對(duì)
servlet
進(jìn)行初始化的時(shí)候,會(huì)收集你所配置的參數(shù),記錄在
ServletConfig
的實(shí)現(xiàn)類中,所以你才可以通過(guò)
ServletConfig
對(duì)象的
??? public String getInitParameter(String name);
或
??? public Enumeration getInitParameterNames();
方法來(lái)取得你已經(jīng)配置好的參數(shù),也就是說(shuō),你對(duì)
servlet
的配置都已經(jīng)記錄在
ServletConfig
對(duì)象中了。
結(jié)論:你對(duì)
Servlet
的配置,在
Servlet
的初始化時(shí)都由容器來(lái)收集并且記錄到
ServletConfig
的實(shí)現(xiàn)類中。
?
③
我們來(lái)看一個(gè)
Servlet
的配置
? <servlet>
??? <servlet-name>index</servlet-name>
??? <servlet-class>org.zy.pro.sw.servlet.IndexServlet</servlet-class>
??? <init-param>
????? <param-name>dbconfig</param-name>
????? <param-value>/WEB-INF/dbconfig.xml</param-value>
??? </init-param>
? </servlet>
在此,我們實(shí)現(xiàn)對(duì)數(shù)據(jù)庫(kù)的配置文件的加載。
當(dāng)
Servlet
初始化完成后,我們可以通過(guò)
String? dbconf=this.getServletConfig().getInitParameter("dbconfig")
來(lái)取得我們的配置的參數(shù)的值。
但是,我們僅能得到一個(gè)配置的字符串。之后我們可以通過(guò)配置文件取得我們的數(shù)據(jù)庫(kù)的配置參數(shù),然后對(duì)數(shù)據(jù)庫(kù)進(jìn)行初始化。
其實(shí)我們也可以通過(guò)傳遞一個(gè)類的名字串,然后再實(shí)例化。
??? <init-param>
????? <param-name>dbconfig</param-name>
????? <param-value>org.zy.util.db.DBUtil</param-value>
??
?</init-param>
我們先取得配置參數(shù):
String? dbconf=this.getServletConfig().getInitParameter("dbconfig")
;
然后通過(guò)
Class.forName(dbconf).getInstance();
來(lái)實(shí)例化對(duì)象,就可以實(shí)現(xiàn)對(duì)數(shù)據(jù)庫(kù)的調(diào)用了。
結(jié)論:在
web.xml
中對(duì)
Servlet
的初始化,只能傳遞字符串類型的數(shù)據(jù)
④
ServletContext
ServletContext
是負(fù)責(zé)和
Servlet
的上文和下文交互,上面和
Servlet
容器交互,下面和
Servlet
中的請(qǐng)求和相應(yīng)進(jìn)行交互。
在
ServletConfig
中,
???
public ServletContext getServletContext();
方法實(shí)現(xiàn)取得當(dāng)前
ServletContext
的對(duì)象。
你可能要問(wèn),
ServletContext
是一個(gè)接口,那么你如何取得他的對(duì)象呢?
其實(shí)這個(gè)問(wèn)題和
ServletConfig
相同,都是在
Servlet
進(jìn)行初始化的時(shí)候產(chǎn)生的對(duì)象,是由容器來(lái)初始化的。
關(guān)于web打印的總結(jié)
方案一:
???????調(diào)用瀏覽器的打印函數(shù)利用瀏覽器的模版
???源程序代碼:
?????????
<
OBJECT?id
=
"
WebBrowser
"
?classid
=
"
CLSID:8856F961-340A-11D0-A96B-00C04FD705A2
"
?height
=
"
0
"
????????????????width
=
"
0
"
?VIEWASTEXT
>
????????????
</
OBJECT
>
?????說(shuō)明:此代碼放在html?中?打印模版的指定框架為此代碼在的模塊;
??????調(diào)用程序:
??????
<
onclick
=
"
parent.main.focus();parent.main.WebBrowser.ExecWB(7,1);
"
調(diào)用瀏覽器的對(duì)象中的方法
<
2
>
?調(diào)用直接打印
??????
<
onclick
=
"
parent.main.focus();parent.main.WebBrowser..print?(7,1);
"
<
3
>
?當(dāng)打印的時(shí)候不顯示時(shí)的css樣式
??????
<
style?media
=
"
print
"
>
.Noprint
{display:none;}
.PageNext
{page
-
break
-
after:?always;}
</
style
>
<
style
>
.TdCs1?
{?border:solid?windowtext?
1
.0pt;}
.TdCs2?
{?border:solid?windowtext?
1
.0pt;?border
-
left:none;}
.TdCs3?
{border
-
top:none;border
-
left:solid?windowtext?
1
.0pt;?border
-
bottom:solid?windowtext?
1
.0pt;?border
-
right:solid?windowtext?
1
.0pt;}
.TdCs4?
{border
-
top:none;??border
-
left:none;border
-
bottom:solid?windowtext?
1
.0pt;border
-
right:solid?windowtext?
1
.0pt;}
.underline?

{border
-
top
-
style:?none;border
-
right
-
style:?none;?border
-
bottom
-
style:?solid;?border
-
left
-
style:?none;border
-
bottom
-
color:?#
000000
;}
????????
</
style
>
????
<
4
>
?其余的設(shè)置
???
<
input?type
=
"
button
"
?value
=
"
打印
"
?onclick
=
"
document.all.WebBrowser.ExecWB(6,1)
"
class
=
"
NOPRINT
"
>
<
input?type
=
"
button
"
?value
=
"
直接打onclick=
"
document.all.WebBrowser.ExecWB(
6
,
6
)
"
?class=
"
NOPRINT
"
>
<
input?type
=
"
button
"
?value
=
"
頁(yè)面設(shè)置
"
?onclick
=
"
document.all.WebBrowser.ExecWB(8,1)
"
?
class
=
"
NOPRINT
"
>
<
input?type
=
"
button
"
?value
=
"
打印預(yù)覽
"
?onclick
=
"
document.all.WebBrowser.ExecWB(7,1)
"
?
class
=
"
NOPRINT
"
>
<html>
<head>
<title>看看</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<!--media=print 這個(gè)屬性可以在打印時(shí)有效-->
<style media=print>
.Noprint{display:none;}
.PageNext{page-break-after: always;}
</style>
<style>
.tdp
{
border-bottom: 1 solid #000000;
border-left: 1 solid #000000;
border-right: 0 solid #ffffff;
border-top: 0 solid #ffffff;
}
.tabp
{
border-color: #000000 #000000 #000000 #000000;
border-style: solid;
border-top-width: 2px;
border-right-width: 2px;
border-bottom-width: 1px;
border-left-width: 1px;
}
.NOPRINT {
font-family: "宋體";
font-size: 9pt;
}
</style>
</head>
<body >
<center class="Noprint" >
<p>
<OBJECT id=WebBrowser classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 height=0 width=0>
</OBJECT>
<input type=button value=打印 onclick=document.all.WebBrowser.ExecWB(6,1)>
<input type=button value=直接打印 onclick=document.all.WebBrowser.ExecWB(6,6)>
<input type=button value=頁(yè)面設(shè)置 onclick=document.all.WebBrowser.ExecWB(8,1)>
</p>
<p> <input type=button value=打印預(yù)覽 onclick=document.all.WebBrowser.ExecWB(7,1)>
<br/>
</p>
<hr align="center" width="90%" size="1" noshade>
</center>
<table width="90%" border="0" align="center" cellpadding="2" cellspacing="0" class="tabp">
<tr>
<td colspan="3" class="tdp">第1頁(yè)</td>
</tr>
<tr>
<td width="29%" class="tdp"> </td>
<td width="28%" class="tdp"> </td>
<td width="43%" class="tdp"> </td>
</tr>
<tr>
<td colspan="3" class="tdp"> </td>
</tr>
<tr>
<td colspan="3" class="tdp"><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="50%" class="tdp"><p>這樣的報(bào)表</p>
<p>對(duì)一般的要求就夠了。</p></td>
<td> </td>
</tr>
</table></td>
</tr>
</table>
<hr align="center" width="90%" size="1" noshade class="NOPRINT" >
<!--分頁(yè)-->
<div class="PageNext"></div>
<table width="90%" border="0" align="center" cellpadding="2" cellspacing="0" class="tabp">
<tr>
<td class="tdp">第2頁(yè)</td>
</tr>
<tr>
<td class="tdp">看到分頁(yè)了吧</td>
</tr>
<tr>
<td class="tdp"> </td>
</tr>
<tr>
<td class="tdp"> </td>
</tr>
<tr>
<td class="tdp"><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="50%" class="tdp"><p>這樣的報(bào)表</p>
<p>對(duì)一般的要求就夠了。</p></td>
<td> </td>
</tr>
</table></td>
</tr>
</table>
</body>
</html>
?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "<html xmlns="<title>Pure CSS Scrollable Table with Fixed Header</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<meta http-equiv="language" content="en-us" />
<script type="text/javascript">
<!--
/* http://www.alistapart.com/articles/zebratables/ */
function removeClassName (elem, className) {
?elem.className = elem.className.replace(className, "").trim();
}
function addCSSClass (elem, className) {
?removeClassName (elem, className);
?elem.className = (elem.className + " " + className).trim();
}
String.prototype.trim = function() {
?return this.replace( /^\s+|\s+$/, "" );
}
function stripedTable() {
?if (document.getElementById && document.getElementsByTagName) {?
??var allTables = document.getElementsByTagName('table');
??if (!allTables) { return; }
??for (var i = 0; i < allTables.length; i++) {
???if (allTables[i].className.match(/[\w\s ]*scrollTable[\w\s ]*/)) {
????var trs = allTables[i].getElementsByTagName("tr");
????for (var j = 0; j < trs.length; j++) {
?????removeClassName(trs[j], 'alternateRow');
?????addCSSClass(trs[j], 'normalRow');
????}
????for (var k = 0; k < trs.length; k += 2) {
?????removeClassName(trs[k], 'normalRow');
?????addCSSClass(trs[k], 'alternateRow');
????}
???}
??}
?}
}
/* onload state is fired, append onclick action to the table's DIV */
/* container. This allows the HTML document to validate correctly. */
/* addIEonScroll added on 2005-01-28?????????????????????????????? */
/* Terence Ordona, portal[AT]imaputz[DOT]com?????????????????????? */
function addIEonScroll() {
?var thisContainer = document.getElementById('tableContainer');
?if (!thisContainer) { return; }
?var onClickAction = 'toggleSelectBoxes();';
?thisContainer.onscroll = new Function(onClickAction);
}
/* Only WinIE will fire this function. All other browsers scroll the TBODY element and not the DIV */
/* This is to hide the SELECT elements from scrolling over the fixed Header. WinIE only.?????????? */
/* toggleSelectBoxes added on 2005-01-28 */
/* Terence Ordona, portal[AT]imaputz[DOT]com???????? */
function toggleSelectBoxes() {
?var thisContainer = document.getElementById('tableContainer');
?var thisHeader = document.getElementById('fixedHeader');
?if (!thisContainer || !thisHeader) { return; }
?var selectBoxes = thisContainer.getElementsByTagName('select');
?if (!selectBoxes) { return; }
?for (var i = 0; i < selectBoxes.length; i++) {
??if (thisContainer.scrollTop >= eval(selectBoxes[i].parentNode.offsetTop - thisHeader.offsetHeight)) {
???selectBoxes[i].style.visibility = 'hidden';
??} else {
???selectBoxes[i].style.visibility = 'visible';
??}
?}
}
window.onload = function() { stripedTable(); addIEonScroll(); }
-->
</script>
<style type="text/css">
<!--
/* Terence Ordona, portal[AT]imaputz[DOT]com???????? */
/* http://creativecommons.org/licenses/by-sa/2.0/??? */
/* begin some basic styling here???????????????????? */
body {
?background: #FFF;
?color: #000;
?font: normal normal 12px Verdana, Geneva, Arial, Helvetica, sans-serif;
?margin: 10px;
?padding: 0
}
table, td, a {
?color: #000;
?font: normal normal 12px Verdana, Geneva, Arial, Helvetica, sans-serif
}
h1 {
?font: normal normal 18px Verdana, Geneva, Arial, Helvetica, sans-serif;
?margin: 0 0 5px 0
}
h2 {
?font: normal normal 16px Verdana, Geneva, Arial, Helvetica, sans-serif;
?margin: 0 0 5px 0
}
h3 {
?font: normal normal 13px Verdana, Geneva, Arial, Helvetica, sans-serif;
?color: #008000;
?margin: 0 0 15px 0
}
/* end basic styling???????????????????????????????? */
/* define height and width of scrollable area. Add 16px to width for scrollbar????????? */
/* allow WinIE to scale 100% width of browser by not defining a width?????????????????? */
/* WARNING: applying a background here may cause problems with scrolling in WinIE 5.x?? */
div.tableContainer {
?clear: both;
?border: 1px solid #963;
?height: 285px;
?overflow: auto;
?width: 756px;
}
/* WinIE 6.x needs to re-account for it's scrollbar. Give it some padding */
\html div.tableContainer/* */ {
?padding: 0 16px 0 0;
?width: 740px;
}
/* clean up for allowing display Opera 5.x/6.x and MacIE 5.x */
html>body div.tableContainer {
?height: auto;
?padding: 0;
}
/* Reset overflow value to hidden for all non-IE browsers. */
/* Filter out Opera 5.x/6.x and MacIE 5.x????????????????? */
head:first-child+body div[class].tableContainer {
?height: 285px;
?overflow: hidden;
?width: 756px
}
/* define width of table. IE browsers only???????????????? */
/* if width is set to 100%, you can remove the width?????? */
/* property from div.tableContainer and have the div scale */
div.tableContainer table {
?float: left;
?width: 100%
}
/* WinIE 6.x needs to re-account for padding. Give it a negative margin */
\html div.tableContainer table/* */ {
?margin: 0 -16px 0 0
}
/* define width of table. Opera 5.x/6.x and MacIE 5.x */
html>body div.tableContainer table {
?float: none;
?margin: 0;
?width: 740px
}
/* define width of table. Add 16px to width for scrollbar.?????????? */
/* All other non-IE browsers. Filter out Opera 5.x/6.x and MacIE 5.x */
head:first-child+body div[class].tableContainer table {
?width: 756px
}
/* set table header to a fixed position. WinIE 6.x only?????????????????????????????????????? */
/* In WinIE 6.x, any element with a position property set to relative and is a child of?????? */
/* an element that has an overflow property set, the relative value translates into fixed.??? */
/* Ex: parent element DIV with a class of tableContainer has an overflow property set to auto */
thead.fixedHeader tr {
?position: relative;
?/* expression is for WinIE 5.x only. Remove to validate and for pure CSS solution????? */
?top: expression(document.getElementById("tableContainer").scrollTop);
}
/* set THEAD element to have block level attributes. All other non-IE browsers??????????? */
/* this enables overflow to work on TBODY element. All other non-IE, non-Mozilla browsers */
/* Filter out Opera 5.x/6.x and MacIE 5.x???????????????????????????????????????????????? */
head:first-child+body thead[class].fixedHeader tr {
?display: block;
}
/* make the TH elements pretty */
thead.fixedHeader th {
?background: #C96;
?border-left: 1px solid #EB8;
?border-right: 1px solid #B74;
?border-top: 1px solid #EB8;
?font-weight: normal;
?padding: 4px 3px;
?text-align: left
}
/* make the A elements pretty. makes for nice clickable headers??????????????? */
thead.fixedHeader a, thead.fixedHeader a:link, thead.fixedHeader a:visited {
?color: #FFF;
?display: block;
?text-decoration: none;
?width: 100%
}
/* make the A elements pretty. makes for nice clickable headers??????????????? */
/* WARNING: swapping the background on hover may cause problems in WinIE 6.x?? */
thead.fixedHeader a:hover {
?color: #FFF;
?display: block;
?text-decoration: underline;
?width: 100%
}
/* define the table content to be scrollable????????????????????????????????????????????? */
/* set TBODY element to have block level attributes. All other non-IE browsers??????????? */
/* this enables overflow to work on TBODY element. All other non-IE, non-Mozilla browsers */
/* induced side effect is that child TDs no longer accept width: auto???????????????????? */
/* Filter out Opera 5.x/6.x and MacIE 5.x???????????????????????????????????????????????? */
head:first-child+body tbody[class].scrollContent {
?display: block;
?height: 262px;
?overflow: auto;
?width: 100%
}
/* make TD elements pretty. Provide alternating classes for striping the table */
/* http://www.alistapart.com/articles/zebratables/???????????????????????????? */
tbody.scrollContent td, tbody.scrollContent tr.normalRow td {
?background: #FFF;
?border-bottom: none;
?border-left: none;
?border-right: 1px solid #CCC;
?border-top: 1px solid #DDD;
?padding: 2px 3px 3px 4px
}
tbody.scrollContent tr.alternateRow td {
?background: #EEE;
?border-bottom: none;
?border-left: none;
?border-right: 1px solid #CCC;
?border-top: 1px solid #DDD;
?padding: 2px 3px 3px 4px
}
/* define width of TH elements: 1st, 2nd, and 3rd respectively.????? */
/* All other non-IE browsers. Filter out Opera 5.x/6.x and MacIE 5.x */
/* Add 16px to last TH for scrollbar padding???????????????????????? */
/* http://www.w3.org/TR/REC-CSS2/selector.html#adjacent-selectors??? */
head:first-child+body thead[class].fixedHeader th {
?width: 200px
}
head:first-child+body thead[class].fixedHeader th + th {
?width: 240px
}
head:first-child+body thead[class].fixedHeader th + th + th {
?border-right: none;
?padding: 4px 4px 4px 3px;
?width: 316px
}
/* define width of TH elements: 1st, 2nd, and 3rd respectively.????? */
/* All other non-IE browsers. Filter out Opera 5.x/6.x and MacIE 5.x */
/* Add 16px to last TH for scrollbar padding???????????????????????? */
/* http://www.w3.org/TR/REC-CSS2/selector.html#adjacent-selectors??? */
head:first-child+body tbody[class].scrollContent td {
?width: 200px
}
head:first-child+body tbody[class].scrollContent td + td {
?width: 240px
}
head:first-child+body tbody[class].scrollContent td + td + td {
?border-right: none;
?padding: 2px 4px 2px 3px;
?width: 300px
}
-->
</style>
</head><body>
<h1>(Almost) Pure CSS Scrollable Table with Fixed Header</h1>
<h2>Using CSS to allow scrolling within a single HTML table</h2>
<div><br/></div>
<h2>The Bullet Resistant Version</h2>
<h3>Lots of CSS Browser Filtering</h3>
<form id="sampleForm" action="bulletVersion.html" method="post">
<div id="tableContainer" class="tableContainer">
<table border="0" cellpadding="0" cellspacing="0" width="100%" class="scrollTable">
<thead class="fixedHeader" id="fixedHeader">
?<tr>
??<th><a href="#">Header 1</a></th>
??<th><a href="#">Header 2</a></th>
??<th><a href="#">Header 3</a></th>
?</tr>
</thead>
<tbody class="scrollContent">
?<tr>
??<td>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Nulla vitae wisi. Nulla euismod aliquet tellus.</td>
??<td>In sit amet enim. Praesent vulputate tortor nec ante. Morbi sollicitudin est non neque.</td>
??<td>Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos hymenaeos.</td>
?</tr>
?<tr>
??<td>Cell Content 1</td>
??<td>Cell Content 2</td>
??<td><select name="sampleSelect1" id="sampleSelect1"><option>Option 1</option><option>Option 2</option><option>Option 3</option><option>Option 4</option><option>Option 5</option></select></td>
?</tr>
?<tr>
??<td>Cell Content 1</td>
??<td>Cell Content 2</td>
??<td><select name="sampleSelect2" id="sampleSelect2" size="5" multiple="multiple"><option>Option 1</option><option>Option 2</option><option>Option 3</option><option>Option 4</option><option>Option 5</option></select></td>
?</tr>
?<tr>
??<td>More Cell Content 1</td>
??<td>More Cell Content 2</td>
??<td><input type="text" name="sampleText" id="sampleText" value="This is a sample Text form element" /></td>
?</tr>
?<tr>
??<td>Even More Cell Content 1</td>
??<td>Even More Cell Content 2</td>
??<td><input type="password" name="samplePassword" id="samplePassword" value="password" /></td>
?</tr>
?<tr>
??<td>And Repeat 1</td>
??<td>And Repeat 2</td>
??<td><input type="submit" name="sampleSubmit" id="sampleSubmit" value="Sample Submit Button" /></td>
?</tr>
?<tr>
??<td>Cell Content 1</td>
??<td>Cell Content 2</td>
??<td><input type="reset" name="sampleReset" id="sampleReset" value="Sample Reset Button" /></td>
?</tr>
?<tr>
??<td>More Cell Content 1</td>
??<td>More Cell Content 2</td>
??<td><input type="button" name="sampleButton" id="sampleButton" value="Sample Button Element" /></td>
?</tr>
?<tr>
??<td>Even More Cell Content 1</td>
??<td>Even More Cell Content 2</td>
??<td><input type="checkbox" name="sampleCheckbox" id="sampleCheckboxA" value="sampleCheckboxA" /> <label for="sampleCheckboxA">Sample Checkbox A</label></td>
?</tr>
?<tr>
??<td>And Repeat 1</td>
??<td>And Repeat 2</td>
??<td><input type="checkbox" name="sampleCheckbox" id="sampleCheckboxB" value="sampleCheckboxB" /> <label for="sampleCheckboxB">Sample Checkbox B</label></td>
?</tr>
?<tr>
??<td>Cell Content 1</td>
??<td>Cell Content 2</td>
??<td><input type="radio" name="sampleRadio" id="sampleRadioA" value="sampleRadioA" /> <label for="sampleRadioA">Sample Radio A</label></td>
?</tr>
?<tr>
??<td>More Cell Content 1</td>
??<td>More Cell Content 2</td>
??<td><input type="radio" name="sampleRadio" id="sampleRadioB" value="sampleRadioB" /> <label for="sampleRadioB">Sample Radio B</label></td>
?</tr>
?<tr>
??<td>Even More Cell Content 1</td>
??<td>Even More Cell Content 2</td>
??<td><select name="sampleSelect3" id="sampleSelect3"><option>Option 1</option><option>Option 2</option><option>Option 3</option><option>Option 4</option><option>Option 5</option></select></td>
?</tr>
?<tr>
??<td>And Repeat 1</td>
??<td>And Repeat 2</td>
??<td><select name="sampleSelect4" id="sampleSelect4" size="5" multiple="multiple"><option>Option 1</option><option>Option 2</option><option>Option 3</option><option>Option 4</option><option>Option 5</option></select></td>
?</tr>
?<tr>
??<td>Cell Content 1</td>
??<td>Cell Content 2</td>
??<td><textarea cols="20" rows="5" name="sampleTextarea" id="sampleTextarea">Cell Content 3</textarea></td>
?</tr>
?<tr>
??<td>More Cell Content 1</td>
??<td>More Cell Content 2</td>
??<td>More Cell Content 3</td>
?</tr>
?<tr>
??<td>Even More Cell Content 1</td>
??<td>Even More Cell Content 2</td>
??<td>Even More Cell Content 3</td>
?</tr>
?<tr>
??<td>And Repeat 1</td>
??<td>And Repeat 2</td>
??<td>And Repeat 3</td>
?</tr>
?<tr>
??<td>Cell Content 1</td>
??<td>Cell Content 2</td>
??<td><select name="sampleSelect5" id="sampleSelect5"><option>Option 1</option><option>Option 2</option><option>Option 3</option><option>Option 4</option><option>Option 5</option></select></td>
?</tr>
?<tr>
??<td>More Cell Content 1</td>
??<td>More Cell Content 2</td>
??<td><select name="sampleSelect6" id="sampleSelect6"><option>Option 1</option><option>Option 2</option><option>Option 3</option><option>Option 4</option><option>Option 5</option></select></td>
?</tr>
?<tr>
??<td>Even More Cell Content 1</td>
??<td>Even More Cell Content 2</td>
??<td>Even More Cell Content 3</td>
?</tr>
?<tr>
??<td>And Repeat 1</td>
??<td>And Repeat 2</td>
??<td>And Repeat 3</td>
?</tr>
?<tr>
??<td>Cell Content 1</td>
??<td>Cell Content 2</td>
??<td>Cell Content 3</td>
?</tr>
?<tr>
??<td>More Cell Content 1</td>
??<td>More Cell Content 2</td>
??<td>More Cell Content 3</td>
?</tr>
?<tr>
??<td>Even More Cell Content 1</td>
??<td>Even More Cell Content 2</td>
??<td>Even More Cell Content 3</td>
?</tr>
?<tr>
??<td>And Repeat 1</td>
??<td>And Repeat 2</td>
??<td>And Repeat 3</td>
?</tr>
?<tr>
??<td>Cell Content 1</td>
??<td>Cell Content 2</td>
??<td>Cell Content 3</td>
?</tr>
?<tr>
??<td>More Cell Content 1</td>
??<td>More Cell Content 2</td>
??<td>More Cell Content 3</td>
?</tr>
?<tr>
??<td>Even More Cell Content 1</td>
??<td>Even More Cell Content 2</td>
??<td>Even More Cell Content 3</td>
?</tr>
?<tr>
??<td>And Repeat 1</td>
??<td>And Repeat 2</td>
??<td>And Repeat 3</td>
?</tr>
?<tr>
??<td>Cell Content 1</td>
??<td>Cell Content 2</td>
??<td>Cell Content 3</td>
?</tr>
?<tr>
??<td>More Cell Content 1</td>
??<td>More Cell Content 2</td>
??<td>More Cell Content 3</td>
?</tr>
?<tr>
??<td>Even More Cell Content 1</td>
??<td>Even More Cell Content 2</td>
??<td>Even More Cell Content 3</td>
?</tr>
?<tr>
??<td>And Repeat 1</td>
??<td>And Repeat 2</td>
??<td>And Repeat 3</td>
?</tr>
?<tr>
??<td>Cell Content 1</td>
??<td>Cell Content 2</td>
??<td>Cell Content 3</td>
?</tr>
?<tr>
??<td>More Cell Content 1</td>
??<td>More Cell Content 2</td>
??<td>More Cell Content 3</td>
?</tr>
?<tr>
??<td>Even More Cell Content 1</td>
??<td>Even More Cell Content 2</td>
??<td>Even More Cell Content 3</td>
?</tr>
?<tr>
??<td>And Repeat 1</td>
??<td>And Repeat 2</td>
??<td>And Repeat 3</td>
?</tr>
?<tr>
??<td>Cell Content 1</td>
??<td>Cell Content 2</td>
??<td>Cell Content 3</td>
?</tr>
?<tr>
??<td>More Cell Content 1</td>
??<td>More Cell Content 2</td>
??<td>More Cell Content 3</td>
?</tr>
?<tr>
??<td>Even More Cell Content 1</td>
??<td>Even More Cell Content 2</td>
??<td>Even More Cell Content 3</td>
?</tr>
?<tr>
??<td>And Repeat 1</td>
??<td>And Repeat 2</td>
??<td>And Repeat 3</td>
?</tr>
?<tr>
??<td>Cell Content 1</td>
??<td>Cell Content 2</td>
??<td>Cell Content 3</td>
?</tr>
?<tr>
??<td>More Cell Content 1</td>
??<td>More Cell Content 2</td>
??<td>More Cell Content 3</td>
?</tr>
?<tr>
??<td>Even More Cell Content 1</td>
??<td>Even More Cell Content 2</td>
??<td>Even More Cell Content 3</td>
?</tr>
?<tr>
??<td>End of Cell Content 1</td>
??<td>End of Cell Content 2</td>
??<td>End of Cell Content 3</td>
?</tr>
</tbody>
</table>
</div>
</form>
<div>
?<p>Also see the <a href=" Big Four Version</a> :: Support for current generation of the four major Browsers</p>
?<h3>Browser Support (table is scrollable with fixed headers)</h3>
?<ul>
??<li>Opera 7.x + (All Platforms) :: Tested with 7.2x and 7.5x</li>
??<li>Mozilla 1.x + (All Platforms) :: Tested with 1.0x and 1.6x</li>
??<li>IE 6.x + (Windows) :: Tested with 6.0x</li>
??<li>IE 5.x + (Windows) :: Tested with 5.0x and 5.5x</li>
??<li>Safari 1.x + (MacOS) :: Tested with 1.2x</li>
??<li>Konqueror 3.x + (Linux / BSD) :: Tested with 3.2x</li>
?</ul>
?<h3>Almost works (table is viewable)</h3>
?<ul>
??<li>IE 5.x + (MacOS) :: Tested with 5.2x</li>
??<li>Opera 5.x and 6.x :: Tested with 5.1x and 6.x</li>
?</ul>
?<h3>Degrades gracefully</h3>
?<ul>
??<li>All other non-supporting browsers</li>
?</ul>
?<h3>Notes:</h3>
?<ul>
??<li>Opera v5 to v7 adds margins to the THEAD and TBODY and their children</li>
??<li>On Konqueror 3.x the scrollbar may be slightly off.</li>
??<li>On Konqueror 3.x form elements may not hide correctly.</li>
??<li>On MacIE 5.x the last table header cell may obscure the up arrow of the scrollbar.</li>
??<li>Gecko/20041217 may have table cell alignment issues (bug?), Prior versions (eg: Gecko/20040113) do not have this</li>
?</ul>
?<h3>Updates:</h3>
?<ul>
??<li>2004.10.15 11am: Added link to Big Four Version</li>
??<li>2004.11.02 01pm: Fixed incorrect width on 2nd Cell. Was 250px, should be 240px.</li>
??<li>2005.01.28 07pm: Added form elements to aid in testing scrolling abilities</li>
??<li>2005.01.28 08pm: Added JS IE Select element workaround.</li>
?</ul>
</div>
<div>
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
put a bunch of breaks to test scrolling within the HTML document itself.
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
put a bunch of breaks to test scrolling within the HTML document itself.
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
put a bunch of breaks to test scrolling within the HTML document itself.
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
done.
</div>
</body></html>
http://www.imaputz.com/cssStuff/bulletVersion.html