Jim’s blog

June 18, 2005

九淺一深Hibernate

【11:53 pm By Jim】

我覺得大爺我,很性福,剛進程、線程、Unix了一天,就要跑去書市。大爺我,要找:
一本 Eclipse 的參考手冊;
一本 Spring 開發指南(指北也可以);
一本 Hibernate 開發指南(指東也沒問題)。
結果,大爺我,擁用水淋淋的大眼睛的大爺我,找了整個書市,都沒有一本像樣的 EclipseSpring《精通 Spring》《精通 Eclipse》千萬不要買,反正老子是悟出來了,凡是國人寫的,名字叫做《精通XX》的,都不能買,基本上看也不用看。除非某天,某個混球,寫出一本《如何精通精通》來,大爺我,或許還有興趣一看。

於是目標就只剩 Hibernate 了,找到三本:
《精通 Hibernate》
《深入淺出 Hibernate》
《精通 Hibernate:Java 對象持久化技術詳解》
基於之前的鑒定原則,我已經對第一本沒什麽好感了,粗粗略過,果不其然,我相信作者技術很棒,嗯,也只是技術很棒,所以就犯了個技術人員寫作很常遇到的毛病,沒有從全局出發,體貼讀者,一開始,就已經陷入了細節當中,瘦,沒有價值,Pass~
第二本,一翻,看到了作者的照片,夏昕(挺帥的哦),OpenDoc 創始人…….怎麽這麽熟….想不起來,好像是 HibernateCN 的人,我想想我想想我想想…….
最後一本,大名鼎鼎的孫衛琴女仕的新作,昨晚找了找,China-pubDearbook 裏,都是同類書籍的銷售冠軍,嗯,Linda 真的很火。

同樣¥59,《深入淺出 Hibernate》 甚至連光盤都沒有,而我們的衛琴姐姐則給了我們大了30%的版面和一張光盤,盡管這和書的質量,沒有什麽關系,但這對讀者來說,很友好。於是,我就開始,對《深入淺出 Hibernate》有好感了,因爲,好東西,是不需要修飾的。

於是,大爺我翻來複去的比較了5分鍾,越比越TMD郁悶,《深入淺出 Hibernate》:循序而進,簡潔友好,而且只針對 Hibernate,其它多餘的東西,你想要也沒有;而《精通 Hibernate:Java 對象持久化技術詳解》則像大雜燴,似乎恨不得把所有相關的東西,都TNND一次過給你說清楚,結果卻是幾邊不到岸,大爺我,想不明白,就這水平的一本書,爲什麽還可以在銷售榜上排第一?

然而很快我就發現自己比較失敗了,因爲,大爺我,發現,銷售榜上排第一,不是因爲這本書寫得有多好,而是因爲我們的衛琴姐姐是女人,是IT女人,是IT寫手中的女人,是IT寫手中的美麗女人,是IT寫手中的首個國內的美麗女人。


本人,並不岐視從事任何工作的女性,但卻鄙視任何炒作,尤其是在傳播知識的行業中。資訊行業的書籍,國內發展得並不好,好的譯本少之有少,原創的好作品更是鳳毛麟角,雖然網上有很多原版的資料看,但不是每個人都喜歡Google,更沒幾個,願冒著掛掉眼睛的危險,把幾百頁的PDF看完,盡管它們大多都是免費的。國內的出版業界以至於作者們,少些浮躁,多些實幹,會比叫叫嚷嚷有意義得多。

只有一深一淺一里一表的,才能真正的看透問題。大爺我,比較喜歡九淺一深,瘦,老闆我要這本《深入淺出 Hibernate》

June 8, 2005

Super Cow Powers【轉】

【3:26 pm By Jim】

Super Cow Powers 是 Debian 系統中一股神秘的力量。

講白了是設計師的娛樂,也就是復活節彩蛋程式(Easter Eggs)。讓某些程式呣呣叫。曾經有人將『超級牛力』視為沒有任何文件說明而回報瑕疵,然而基於娛樂價值,這些指令應該不會納入文件,繼續保持神秘,好讓新手丈二金剛摸不著腦袋直到發現這些復活節彩蛋,捧腹大笑。更好笑得是 aptitude 宣稱沒有超級牛力,大概直到駭客深究程式碼才會發現神秘的蛇吞象。還有一些非套件系統專用的程式含有此類冷笑話,請往程式碼裡翻。

如果你很喜歡見牛說話,請安裝 DebianPackages:cowsay,這是一個可設定的思想牛/多嘴牛程式。

apt-get

【Code】
$ apt-get -h
apt 0.5.24 for linux i386 compiled on Mar 16 2004 22:49:26
Usage: apt-get [options] command

    apt-get [options] install|remove pkg1 [pkg2 …]
    apt-get [options] source pkg1 [pkg2 …]

apt-get is a simple command line interface for downloading and installing packages. The most frequently used commands are update and install.

Commands:
    update - Retrieve new lists of packages
    upgrade - Perform an upgrade
    install - Install new packages (pkg is libc6 not libc6.deb)
    remove - Remove packages
    source - Download source archives
    build-dep - Configure build-dependencies for source packages
    dist-upgrade - Distribution upgrade, see apt-get(8)
    dselect-upgrade - Follow dselect selections
    clean - Erase downloaded archive files
    autoclean - Erase old downloaded archive files
    check - Verify that there are no broken dependencies

Options:
    -h This help text.
    -q Loggable output - no progress indicator
    -qq No output except for errors
    -d Download only - do NOT install or unpack archives
    -s No-act. Perform ordering simulation
    -y Assume Yes to all queries and do not prompt
    -f Attempt to continue if the integrity check fails
    -m Attempt to continue if archives are unlocatable
    -u Show a list of upgraded packages as well
    -b Build the source package after fetching it
    -V Show verbose version numbers
    -c=? Read this configuration file
    -o=? Set an arbitary configuration option, eg -o dir::cache=/tmp

See the apt-get(8), sources.list(5) and apt.conf(5) manual pages for more information and options.

This APT has Super Cow Powers.

$ apt-get moo
         (__)
         (oo)
   /——\/
  / |    ||
 *  /\—/\
    ~~   ~~
….”Have you mooed today?”.

apt-build
【Code】
$ apt-build -v
apt-build version 0.9.2.1
$ apt-build moo
            (__)      ~
            (oo)     /
     _____\/___/
    /   /\ /  /
   ~  /  *  /
      / ___/
*—-/\
    /   \
   /    /
  ~    ~
…”Have you danced today ? Discow !”…

aptitude
這裡顯示已翻譯中文版本。
【Code】
$ aptitude –help
aptitude 0.2.14.1
用法: aptitude [-s 檔案名] [-u|-i]
    aptitude [選項] < 動作> …
動作 (如果未指定,aptitude 將進入交互模式):

    install - 安裝/升級套件
    remove - 移除套件
    purge - 移除套件並刪除其配置檔案
    hold - 將套件置於保持狀態
    unhold - 取消對一個套件的保持命令
    markauto - 將套件標記為自動安裝
    unmarkauto - 將套件標記為手動安裝
    forbid-version - Forbid aptitude from upgrading to a specific package version.
    update - 下載新/可升級套件列表
    upgrade - 執行一次安全的升級
    dist-upgrade - 執行升級,可能會安裝和移除套件
    forget-new - 忽略哪些套件是“新”的
    search - 按名稱 和/或 表達式搜尋套件
    show - 顯示套件細節資訊
    clean - 刪除已下載的套件檔案
    autoclean - 刪除舊的已下載套件檔案
    download - 下載套件的 .deb 檔案

選項:
    -h 此求助純文字
    -s 模擬動作,但是並不真正執行。
    -d 僅僅下載套件,不安裝或者移除任何東西。
    -P 總是提示確認執行動作
    -y 假設對簡單的 是/否 問題回答“是”
    -F 格式 指定顯示搜尋結果的格式;參見手冊
    -O 次序 指定如何排列顯示搜尋結果;參見手冊
    -w 寬度 指定顯示搜尋結果的格式寬度
    -f 積極地嘗試修復損壞的套件。
    -V 顯示就要安裝的套件版本。
    -D 顯示自動改變的套件的依賴關係
    -Z 顯示每個套件的安裝尺寸的變化。
    -v 顯示附加訊息。(可能會提供多次)
    -t [release] 設置將要安裝的套件的發布版本
    –with(out)-recommends, –with(out)-suggests 指定是否將推薦(建議)處理為強依賴關係。
    -S fname: 從檔案名中讀取 aptitude 的延伸狀態資訊。
    -u : 開始執行時下載新的套件列表。
    -i : 開始執行時執行安裝。

這個 aptitude 無超級牛力。
$ aptitude moo
    此軟體沒有復活節彩蛋程式。
$ aptitude -v moo
    此軟體真的沒有復活節彩蛋程式。
$ aptitude -vv moo
    我不是已經告訴你這個軟體真的沒有復活節彩蛋程式了嗎?
$ aptitude -vvv moo
    停啦!
$ aptitude -vvvv moo
    好啦,好啦,如果我給你復活節彩蛋,你是不是就閃人?
$ aptitude -vvvvv moo
    好啦,你贏了。

                            /—-\
                    ——-/      \
                   /               \
                  /                 |
—————–/                  ——–\
———————————————-

    爽了嗎?
$ aptitude -vvvvvv moo
    這是什麼? 這當然是一隻大象被一隻蛇吞掉。你有沒點常識?
$ aptitude -vvvvvvv moo
    走開啦,我正試著專心。

June 6, 2005

I Hacked 127.0.0.1【轉】

【10:50 am By Jim】

我看完下巴都快笑掉了

發生在德國一個名為 stopHipHop 的 IRC channel 上,原文出自一個德語論壇:
http://www.stophiphop.de/modules/news/article.php?storyid=184

不過我看不太懂,而且我們偉大的主角的德語也不太好,於是,有人翻譯成英文了:
http://www.totalillusions.net/forum/index.php?showtopic=328&st=0

我來轉一下:

Never guessed this would have gotten this amount of traffic. This is the story about a hacker who had little problems… Original from the german site http://www.stophiphop.de/, story can be found here: http://www.stophiphop.de/modules/news/article.php?storyid=184, please include this link if you repost this anywhere (site might be down, quite a lot of people are reading this). I, Cochrane, did not write this. I only translated it. Some people seem to get that wrong, it was Elch from www.stophiphop.de who first published this log.

In case you don’t speak german (just as this hacker), I’ve tried a little translation to english. I might have made some spelling errors, but the original spelling wasn’t perfect either. The guy really said “buy buy” in the german version. I’ve posted this on the forum on http://www.desertcombat.com before, so if this looks familiar, might be the same. I’ve corrected some mistakes and put the <> back to the right version (The DC forum does not support them). All censoring was done by this particular forum here. Notice that in germany we get DST earlier than in the US.

The story starts (I’m shortcutting here) with an 【Please control your cussing】 insulting everyone on the IRC channel. Most people there believed it was rather funny. To quote him: “we 【Please control your cussing】 satanists victims winos like you in the ass every day” (this did not make sense in german, either. The translator). But it got even more funny.

For information: The dangerous hacker is called bitchchecker and the one being hacked and original author of the comments, who is talking here, is known as Elch. 127.0.0.1 is always the IP-address of the computer you’re currently using, any request there will return to your computer.

Quote
* bitchchecker (~java@euirc-a97f9137.dip.t-dialin.net) Quit (Ping timeout#)
* bitchchecker (~java@euirc-61a2169c.dip.t-dialin.net) has joined #stopHipHop
【bitchchecker】 why do you kick me
【bitchchecker】 can’t you discus normally
【bitchchecker】 answer!
【Elch】 we didn’t kick you
【Elch】 you had a ping timeout: * bitchchecker (~java@euirc-a97f9137.dip.t-dialin.net) Quit (Ping timeout#)
【bitchchecker】 what ping man
【bitchchecker】 the timing of my pc is right
【bitchchecker】 i even have dst
【bitchchecker】 you banned me
【bitchchecker】 amit it you son of a bitch
【HopperHunter|afk】 LOL
【HopperHunter|afk】 shit you’re stupid, DST^^
【bitchchecker】 shut your mouth WE HAVE DST!
【bitchchecker】 for two weaks already
【bitchchecker】 when you start your pc there is a message from windows that DST is applied.
【Elch】 You’re a real computer expert
【bitchchecker】 shut up i hack you
【Elch】 ok, i’m quiet, hope you don’t show us how good a hacker you are ^^
【bitchchecker】 tell me your network number man then you’re dead
【Elch】 Eh, it’s 129.0.0.1
【Elch】 or maybe 127.0.0.1
【Elch】 yes exactly that’s it: 127.0.0.1 I’m waiting for you great attack
【bitchchecker】 in five minutes your hard drive is deleted
【Elch】 Now I’m frightened
【bitchchecker】 shut up you’ll be gone
【bitchchecker】 i have a program where i enter your ip and you’re dead
【bitchchecker】 say goodbye
【Elch】 to whom?
【bitchchecker】 to you man
buy buy
【Elch】 I’m shivering thinking about such great Hack0rs like you
* bitchchecker (~java@euirc-61a2169c.dip.t-dialin.net) Quit (Ping timeout#)

What happened is clear: That guy entered his own IP-Adress in his mighty Hack-Tool and crashed his own PC. This way, the attack on my PC was a failure. I was already starting to think that I did not have to worry, but a good hacker never calls it a day. Two minutes later he returned.

Quote
* bitchchecker (~java@euirc-b5cd558e.dip.t-dialin.net) has joined #stopHipHop
【bitchchecker】 dude be happy my pc crashed otherwise you’d be gone
【Metanot】 lol
【Elch】 bitchchecker: Then try hacking me again… I still have the same IP: 127.0.0.1
【bitchchecker】 you’re so stupid man
【bitchchecker】 say buy buy
【Metanot】 ah, 【Please control your cussing】 off
【bitchchecker】 buy buy elch
* bitchchecker (~java@euirc-b5cd558e.dip.t-dialin.net) Quit (Ping timeout#)

There was a tension in the room… Would he manage, after these two failures, to crash my PC? I waited. Nothing happened. I felt relieve… Six minutes passed by until he prepared the next wave of attack. Being a Hacker, who usually cracks whole data centers, he knew what his problem was now.

Quote
* bitchchecker (~java@euirc-9ff3c180.dip.t-dialin.net) has joined #stopHipHop
【bitchchecker】 elch you son of a bitch
【Metanot】 bitchchecker how old are you?
【Elch】 What’s up bitchchecker?
【bitchchecker】 you have a frie wal
【bitchchecker】 fire wall
【Elch】 maybe, i don’t know
【bitchchecker】 i’m 26
【Metanot】 such behaviour with 26?
【Elch】 how did you find out that I have a firewall?
【Metanot】 tststs this is not very nice missy
【bitchchecker】 because your gay fire wall directed my turn off signal back to me
【bitchchecker】 be a man turn that shit off
【Elch】 cool, didn’t know this was possible.
【bitchchecker】 thn my virus destroys your pc man
【Metanot】 are you hacking yourselves?
【Elch】 yes bitchchecker is trying to hack me
【Metanot】 he bitchchecker if you’re a hacker you have to get around a firewall even i can do that
【bitchchecker】 yes man i hack the elch but the sucker has a fire wall the
【Metanot】 what firewall do you have?
【bitchchecker】 like a girl
【Metanot】 firewall is normal a normal hacker has to be able to get past it…you girl^^
【He】 Bitch give yourself a jackson and chill you’re letting them provoce you and give those little girls new material all the time
【bitchchecker】 turn the firewall off then i send you a virus 【Please control your cussing】er
【Elch】 Noo
【Metanot】 he bitchchecker why turn it off, you should turn it off
【bitchchecker】 you’re afraid
【bitchchecker】 i don’t wanna hack like this if he hides like a girl behind a fire wall
【bitchchecker】 elch turn off your shit wall!
【Metanot】 i wanted to say something about this, do you know the definition of hacking??? if he turns of the firewall that’s an invitation and that has nothing to do with hacking
【bitchchecker】 shut up
【Metanot】 lol
【bitchchecker】 my grandma surfs with fire wall
【bitchchecker】 and you suckers think you’re cool and don’t dare going into the internet without a fire wall

He calls me girly and says only his grandma would use a firewall. I know that elder people are much more intelligent then younger, but I couldn’t let that rest. To see whether he really is a good hacker I lie and let everything as it is. I don’t have a firewall at all, only my router.

Quote
【Elch】 bitchchecker, a collegue showed me how to turn the firewall off. Now you can try again
【Metanot】 bitchhacker can’t hack
【Black*TdV*】 nice play on words ^^
【bitchchecker】 wort man
【Elch】 bitchchecker: I’m still waiting for your attack!
【Metanot】 how many times again he is no hacker
【bitchchecker】 man do you want a virus
【bitchchecker】 tell me your ip and it deletes your hard drive
【Metanot】 lol ne give it up i’m a hacker myself and i know how hackers behave and i can tell you 100.00% you’re no hacker..^^
【Elch】 127.0.0.1
【Elch】 it’s easy
【bitchchecker】 lolololol you so stupid man you’ll be gone
【bitchchecker】 and are the first files being deleted
【Elch】 mom…
【Elch】 i’ll take a look

In panic I started the Windows Explorer, my heart beating faster. Had I under-estimated him?

Quote
【bitchchecker】 don’t need to rescue you can’t son of a bitch
【Elch】 that’s bad
【bitchchecker】 elch you idiout your hard drive g: is deleted
【Elch】 yes, there’s nothing i can do about it
【bitchchecker】 and in 20 seconds f: is gone

Yes, true, G: and F: were gone. Did I ever have them? Doesn’t matter, I did not have time to think, I was scared. bitchchecker was comforting me with a music tip.

Quote
【bitchchecker】 tupac rules
【bitchchecker】 elch you son of a bitch your f: is gone and e: too

Drive E:? Oh my god… All the games are there! And the vacation pictures! I instantly take a look. Everything still there. But the hacker said it was deleted….

Or isn’t it happening on my computer?

Quote
【bitchchecker】 and d: is at 45% you idiot lolololol
【He】 why doesn’t meta say anything
【Elch】 he’s probably rolling on the floor laughing
【Black*TdV*】 ^^
【bitchchecker】 your d: is gone
【He】 go on BITCH

The guy is good: My CD-drive is allegedly deleted! Bitchchecker turned my ancient disk sucker into a burner! But how did he do this? I’ll have to ask him. Some encourage him. He himself is giving advice how to avoid the disaster on my hard drives.

Quote
【bitchchecker】 elch man you’re so stupid never give your ip on the internet
【bitchchecker】 i’m already at c: 30 percent

Should I tell him he’s not attacking my computer?

Quote
* bitchchecker (~java@euirc-9ff3c180.dip.t-dialin.net) Quit (Ping timeout#)

Too late… It’s 20:22 when we get the last message of our hacker with the alias “bitchchecker”. We see that he has a “Ping timeout”. We haven’t seen him since then… must be the Daylight Saving Time.

Notice: We are well completely aware that maybe bitchchecker was just playing a game with us. He also claimed to have german as his major field of study.

Quote
【bitchchecker】 shut up man i have advanced german

Added by the translator: I completely forgot the last notice - sorry. His german is about as bad as my translation of it (this was intended). Notice that the german expression “Leistungskurs” (the most advanced course you can take in the kind of school that prepares you for university) can not really be translated into english. I tried with “advanced” and “major field of study”, hope it fits.

Someone also made a translation to spanish, you can find it at
http://www.aristasweb.net/noticias.php?idn=3119&clase=100
Nichol4s translated it to italian, you can find it at
http://www.ngi.it/forum/showthread.php?p=6469535#post6469535

Moto學園也把它翻了過來~
http://moto.debian.org.tw/viewtopic.php?t=6572

最讓人憤飯的是,stopHipHop的人居然還爲此做了件T-shirt,不知道這名偉大的Hacker BitchChecker看到後會怎樣?

June 2, 2005

《Thinking In Java》筆記 - Chapter (5)

【11:03 pm By Jim】

* 讓變動的事物與不變的事物彼此隔離,是OOD的首要考慮因素。

  • package:library unit

  • (1)package name 採用反相 internet 域名,以確保其唯一性,並且規定皆爲小寫(Java2 開始規定);

    (2)import 的兩個 library 如含有同名的 class 則仍會發生衝突,可通過在使用時通過指定具體的 package.class 名來解決,如:
    java.util.Random random = new Random();

  • Access Specifiers

  • (1)public:所定義的成員可爲所用人使用;

    (2)private:除其所在的 class,沒有任何人(包括 package 成員)可以訪問;

    (3)protected:允許 class 自身、subclasses 以及 package 成員訪問;

    (4)friendly(隱式):不加任何 Access Specifiers 的默認權限,類似於 protected,唯一的區別是不能被繼承使用;

    (5)default package:假設成員皆爲 friendly(或是更高級別的權限),那麽同一目錄下的所有 class 即使不加任何 package、import 的聲明也可以相互的調用;這是因爲 Java 會將這些 class 視爲隸屬於該目錄的 default package,也就是說,會爲它們加上當前的目錄做爲 default package。

  • Interface 與 Implementation

  • (1)訪問權限
    ① 建立界限,保護(密)代碼;

    ② 使得接口和實現分離、獨立。

    (2)class 的訪問權限
    ① 每個編譯單元(即文件)都僅能有一個 public class;

    ② public class 的名稱(包括大小寫)必須與其編譯單元名稱完全相同;

    ③ 編譯單元內可以不含有任何的 public class;

    ④ 除 inner class,class 不能聲明爲 private 或 protected;

    ⑤ 使用 private class:
    A. 可在 private class 內增加起接口作用的 static 成員,通過 static 成員來使用 private 成員;

    B. 使用 Singleton。

    May 31, 2005

    Spam

    【11:52 am By Jim】

    TMD,打開郵箱,這樣的垃圾一堆

    May 27, 2005

    JSP中文問題的解決方案

    【11:28 pm By Jim】

    JSP很贊,這點地球人都知道。對於小型項目,可以像PHPASP一樣作爲純腳本語言使用,極大的提高開發效率;大中型的項目,有Servlet做爲filter、controller,組件有JavaBeanEJB,再結合XML,成爲了靈活而強大的J2EE,再加上它的平台無關性,Wooooo….這個世界清靜了。

    好了,YY完畢,該批鬥它了,靈活和強大的另一面,也正意味著它的門檻高,很難學(當然如果你只用JSP,就當我沒說過好了,不過我想你不會那麽笨吧…);另一方面就是localize的問題,發明Java的是使用26個字母的人,不是使用雙字節中文的我們,所以本地化問題也就順理成章的成爲了我們使用JSP的其中一個障礙。

    要解決JSP的中文問題,首先就要先了解JSP。我們知道JSP需要Container來裝載運行,Container的其中一項重要功能就是把JSP轉換成Servlet然後編譯成class執行,也就是說JSP本質上就是Servlet,一個純正的B/S(Browser/Server)架構,所以要解決它的中文問題,就得從Browser和Server這兩個方面入手,然後Browser和Server要通信對不?那就是要傳遞了?Yeah,所以這個中間環節也不要忽略,現在先來總結一下我們需要面對的問題:

    • Client端對中文的處理:Browser
    • Server端對中文的處理:Server
    • Browser/Server間交互的處理

    好了,現在可以著手解決問題了。要正確的顯示某種字符集,那就得對各各環節統一編碼。對於中文,我們可以使用國內的GB2312GBK(非標准)、GB18030這幾種編碼,繁體?別擔心,GBKGB18030可以兼容BIG5。但是,GB2312太小,GBKGB2312的擴展而不是標准編碼,GB18030太新,並沒有獲得廣泛的支持,所以現在國內使用最廣泛的還是GB2312。Poor~如果有幸又很不幸,你的系統有法國、德國、俄羅斯、日本、韓國….的用戶使用,那麽情況會更糟糕些,Hey,別擔心,可以解決的,我們有Unicode:)

    目前Unicode在Web端使用的最多的是UTF-8,它可以保證系統對多語言的正確處理,下面就來看看怎樣用它來解決煩人的中文問題。

    (1)Client端對中文的處理:Browser
    把它放在第一位是因爲它最簡單最好解決,不至於把人嚇跑:)
    只需要在你的頁面內指定Encoding,告訴Browser使用哪種Encoding顯示就行了,一行代碼可以搞定。

     <%@page contentType=”text/html; charset=UTF-8″%>

    上面的代碼加在首行,它告訴Browser,該頁面的內容類型是“text/html”,使用“UTF-8”字符集顯示,用於視圖層,這條代碼是JSP頁面專用的,和靜態頁面中meta的content、charset作用是一樣的。

    如果你想保險些,可以連meta也用上,沒有問題。

    (2)Server端對中文的處理:Server
    Server端的處理也很簡單,只需在頁面的起始位置加上:

     <%@page pageEncoding=”UTF-8″%>

    指定Server端使用“UTF-8”編碼集來處理JSP內的Java代碼(其實也就是Servlet)。

    (3)Browser/Server間交互的處理
    做完上面的兩步,你的JSPServlet就可以使用UTF-8編碼集進行正確的中文處理了,但還僅限於獨立、沒有交互的單個JSPServlet。系統需要交互,所以最後我們要解決的就是頁面間的交互問題。

    JSP之間的交互是由Container來完成的,而TomcatResin默認使用的都是ISO8859,所以使用雙字節的字符就會出現煩人亂碼,對了,如果將這個默認的編碼另行指定,就可以解決了。JSP中可以用request.setCharacterEncoding來進行指定。只需要在兩個傳遞的頁面內加入:

     <%request.setCharacterEncoding(”UTF-8″);%>

    很簡單吧,一行就OK了。不過….也很笨。譬如說,你有一個現成的系統需要進行這樣的更改,裏面又有一千幾百個頁面,那麽你….

    Take is easy!有Jim在,我們可以寫一個Filter來解決這個問題:)我寫了一個:

     /*
      * @author Jim
      * @version 1.0
      * RequestEncodingFilter.java
      * ToDo: To filter the parameter sent by jsp pages, encode them into customize
      * encoding instead of system’s “ISO8859-1″ encoding.
      */

     package cn.jwing.cms.common;

     import java.io.*;
     import javax.servlet.*;
     import javax.servlet.http.*;

     public class RequestEncodingFilter implements Filter {
         String charset = null;

         public void init(FilterConfig config) throws ServletException {
             charset = config.getInitParameter(”charset”);
             if (charset == null) charset = “UTF-8″;
         }

         public void doFilter(ServletRequest request, ServletResponse response,
                             FilterChain chain) throws IOException,ServletException {
             request.setCharacterEncoding(charset);
             chain.doFilter(request, response);
         }
         public void destroy() {
         }
     }

    把它扔到classes下,在web.xml中加入相應的servlet、filter項就行了,Simple:)

    到此爲止,你的系統已經可以正確的處理中文了(其實應該說什麽文都沒問題了),結束?No!還有最基本的一點要說一下,相信無論是高手還是低手,都很有可能在這點上疏忽了,就是Editor/IDE的默認文件編碼!這也是最根本的一點,可別把系統想得那麽聰明,會幫你自動進行轉碼的工作,計算機可是最笨的。如果你編輯産生的文件,根本就不是UTF-8而是系統默認的GBK或是CP1252之流,那麽上面所做的一切,都是無用功。所以在做上面的三步之前,先確認你的Editor/IDE支持並使用UTF-8

    最後總結一下,解決方案的四個要點:

    • Browser端Encoding的指定
    • Server端Encoding的指定
    • 頁面間傳輸的Encoding的指定
    • Editor/IDE Encoding的指定

    好了,收工~
    Good luck

    May 24, 2005

    大理想進行中

    【10:55 pm By Jim】

    我是做Java的,也經常在網上混。混多了,多少有些體會,尤其是我這種挨踢人士。我上很多的技術論壇,用多了就發現這些論壇程序不太合意,當然了,這是人家的東西,域名和空間都是別人的,沒有我說話的份,而稍微好用一些的,都賣得很貴,而且,還不一定好用。Discuz精簡型(也就是最低級最便宜的版本)¥899(沒有任何技術支持,2年Upgrade),CJSDN的Jute個人版¥500(1年Upgrade),一個PHP,一個Java,都代表了論壇程序的最高水平。可是,太貴了,尤其對一些只是想搭建一個小小的空間讓朋友聚首的人來說。所以,就有了寫論壇程序的打算。Jwing是網名及用在一些技術論壇的ID,這個名字是很久以前想的,確切來說,應該是J+Wing,J Stand For Java & Jim(Yeah, it’s me.),Wing是希望自己能寫一個很棒的Forum,有Wing的Forum,飛快:)

    一個論壇程序可大可小,不過我知道如果要做好,所要投入的時間和精力是巨大的,尤其是一個人業餘來做,Discuz的大C用了三年(全職)把它做出了名堂,CJSDN的Jute好像更久些,我不曉得我一個人能做出什麽來,我只是相信,堅持和努力是一定會有成果的~

    現在說License問題好像有點遠,但是我想以後我業餘寫的所有東西,都會使用BSD License,大概是因爲開源社區泡多了,受共産主義的思想影響比較大,我希望自己寫的東西能不受什麽Copyright這類東東的約束而幫到有需要的人,BSD License可以做到這一點,Great

    其實呢,forum只是一部分,真正想做的,是一個完全而完整的CMS,J2EE做這類的東東有很多先天的優勢,我希望我能把它做好,盡管我知道開源在國內並不好混,沒關系,堅持自己的理想和信念,就是成功,就是勝利,Right?


    Next Page »

    This work is licensed under a Creative Commons License .
    Get free blog up and running in minutes with Blogsome | Theme designs available here