有些人原本一向身體健康,但在連續(xù)熬夜數(shù)晚后,突然第二天起床會覺得很疲勞,一閉眼就想睡覺,而且會腰酸背痛,但一到晚上精神又好起來!別以為這是小事!根據(jù)中醫(yī)的看法,是因過勞而造成體內(nèi)器官陰陽失調(diào),就是體內(nèi)器官起內(nèi)訌,互相打架,最后造成器官衰竭而死。
睡眠是身體進行自我調(diào)整的時刻,你侵略它的時間,它便侵略你的健康,希望過勞致死的事件可以不再發(fā)生。
晚上9-11點為免疫系統(tǒng)(淋巴)排毒時間,此段時間應(yīng)安靜或聽音樂。
晚間11-凌晨1點,肝的排毒,需在熟睡中進行。
凌晨1-3點,膽的排毒,亦同。
凌晨3-5點,肺的排毒。此即為何咳嗽的人在這段時間咳得最劇烈,因排毒動作已走到肺;不應(yīng)用止咳藥,以免抑制廢積物的排除。
凌晨5-7點,大腸的排毒,應(yīng)上廁所排便。
早上7-9點,小腸大量吸收營養(yǎng)的時段,應(yīng)吃早餐。療病者最好早吃,在6點半前,養(yǎng)生者在7點半前,不吃早餐者應(yīng)改變習(xí)慣,即使拖到9、10點吃都比不吃好。
半夜至凌晨4點為脊椎造血時段,必須熟睡,不宜熬夜。
posted @
2007-08-18 17:47 jadmin 閱讀(90) |
評論 (0) |
編輯 收藏
1. 扎實的基礎(chǔ)。數(shù)據(jù)結(jié)構(gòu)、離散數(shù)學(xué)、編譯原理,這些是所有計算機科學(xué)的基礎(chǔ),如果不掌握他們,很難寫出高水平的程序。據(jù)我的觀察,學(xué)計算機專業(yè)的人比學(xué)其他專業(yè)的人更能寫出高質(zhì)量的軟件。程序人人都會寫,但當(dāng)你發(fā)現(xiàn)寫到一定程度很難再提高的時候,就應(yīng)該想想是不是要回過頭來學(xué)學(xué)這些最基本的理論。不要一開始就去學(xué)OOP,即使你再精通OOP,遇到一些基本算法的時候可能也會束手無策。
2. 豐富的想象力。不要拘泥于固定的思維方式,遇到問題的時候要多想幾種解決問題的方案,試試別人從沒想過的方法。豐富的想象力是建立在豐富的知識的基礎(chǔ)上,除計算機以外,多涉獵其他的學(xué)科,比如天文、物理、數(shù)學(xué)等等。另外,多看科幻電影也是一個很好的途徑。
3. 最簡單的是最好的。這也許是所有科學(xué)都遵循的一條準(zhǔn)則,如此復(fù)雜的質(zhì)能互換原理在愛因斯坦眼里不過是一個簡單得不能再簡單的公式:E=mc2。簡單的方法更容易被人理解,更容易實現(xiàn),也更容易維護。遇到問題時要優(yōu)先考慮最簡單的方案,只有簡單方案不能滿足要求時再考慮復(fù)雜的方案。
4. 不鉆牛角尖。當(dāng)你遇到障礙的時候,不妨?xí)簳r遠離電腦,看看窗外的風(fēng)景,聽聽輕音樂,和朋友聊聊天。當(dāng)我遇到難題的時候會去玩游戲,而且是那種極暴力的打斗類游戲,當(dāng)負責(zé)游戲的那部分大腦細胞極度亢奮的時候,負責(zé)編程的那部分大腦細胞就得到了充分的休息。當(dāng)重新開始工作的時候,我會發(fā)現(xiàn)那些難題現(xiàn)在竟然可以迎刃而解。
5. 對答案的渴求。人類自然科學(xué)的發(fā)展史就是一個渴求得到答案的過程,即使只能知道答案的一小部分也值得我們?nèi)ジ冻觥V灰銏远ㄐ拍睿欢ㄒ业絾栴}的答案,你才會付出精力去探索,即使最后沒有得到答案,在過程中你也會學(xué)到很多東西。
6. 多與別人交流。三人行必有我?guī)煟苍S在一次和別人不經(jīng)意的談話中,就可以迸出靈感的火花。多上上網(wǎng),看看別人對同一問題的看法,會給你很大的啟發(fā)。
7. 良好的編程風(fēng)格。注意養(yǎng)成良好的習(xí)慣,代碼的縮進編排,變量的命名規(guī)則要始終保持一致。大家都知道如何排除代碼中錯誤,卻往往忽視了對注釋的排錯。注釋是程序的一個重要組成部分,它可以使你的代碼更容易理解,而如果代碼已經(jīng)清楚地表達了你的思想,就不必再加注釋了,如果注釋和代碼不一致,那就更加糟糕。
8. 韌性和毅力。這也許是"高手"和一般程序員最大的區(qū)別。A good programming is 99% sweat and 1% coffee。高手們并不是天才,他們是在無數(shù)個日日夜夜中磨練出來的。成功能給我們帶來無比的喜悅,但過程卻是無比的枯燥乏味。你不妨做個測試,找個10000以內(nèi)的素數(shù)表,把它們?nèi)汲聛恚缓笤贆z查三遍,如果能夠不間斷地完成這一工作,你就可以滿足這一條。
posted @
2007-08-18 17:40 jadmin 閱讀(65) |
評論 (0) |
編輯 收藏
To laugh often and much;
To win the respect of intelligent people and the affection of children;
To earn the appreciation of honest critics and endure the betrayal of false friends;
To appreciate beauty;
To find the best in others;
To leave the world a bit better, whether by a healthy child, a garden patch or a redeemed social condition;
To know even one life has breathed easier because you have lived;
This is to have succeeded.
posted @
2007-08-18 17:36 jadmin 閱讀(77) |
評論 (0) |
編輯 收藏
知識在于積累和實踐,-正所謂"厚積薄發(fā)",我想應(yīng)該是這個道理吧!
舊的知識需要時刻溫習(xí),就像英語,你不看,不接觸,遲早有一天會把它忘得干干凈凈
從今天開始,每天進步一點點,畢竟"學(xué)習(xí)"是一輩子的事情,馬虎不得!呵呵~~~
posted @
2007-08-18 16:56 jadmin 閱讀(71) |
評論 (0) |
編輯 收藏
The furthest distance in the world
Is not between life and death
But when I stand in front of you
Yet you don’t know that
I love you
The furthest distance in the world
Is not when i stand in font of you
Yet you can’t see my love
But when undoubtedly knowing the love from both
Yet cannot
Be togehter
The furthest distance in the world
Is not being apart while being in love
But when plainly can not resist the yearning
Yet pretending
You have never been in my heart
The furthest distance in the world
Is not
But using one’s indifferent heart
To dig an uncrossable river
For the one who loves you
世界上最遙遠的距離,不是生與死
而是我就站在你的面前,你卻不知道我愛你
世界上最遙遠的距離,不是我站在你面前,你卻不知道我愛你
而是明明知道彼此相愛,卻不能在一起
世界上最遙遠的距離,不是明明知道彼此相愛,卻不能在一起
而是明明無法抵擋這股想念,卻還得故意裝作絲毫沒有把你放在心里
世界上最遙遠的距離,不是明明無法抵擋這股想念,卻還得故意裝作絲毫沒有把你放在心里,而是用自己冷漠的心對愛你的人掘了一條無法跨越的溝渠
posted @
2007-08-18 16:44 jadmin 閱讀(60) |
評論 (0) |
編輯 收藏
1.Compliment three people every day.每天贊美三個人。
2.Watch a sunrise.看日出。
3.Be the first to say "hello".先向別人打招呼。
4.Live beneath your means.靠自己的力量生活。
5.Treat everyone as your want to be treated.像自己希望得到的善待一樣善待他人。
6.Never give up on anybody;Miracles happen.不要放棄任何人;奇跡總會發(fā)生。
7.Remember someone's name.記住別人的名字。
8 .Pray not for things,but for wisdom and courage.不要乞求得到什么東西,但可以乞求智慧和勇氣。
9.Be tough-minded,but tender hearted.意志要堅強,但心地要善良。
10.Be kinder than you have to be.仁慈,再仁慈一點。
11.Don't forget that a person's greatest emotional need is to feel appreciated.不
要忘記一個人最偉大的情感需要是得到別人的欣賞。
12.Keep your promises.履行你的諾言。
13.Show cheerfulness even when you don’t feel it.即使沒有快樂的感覺,也要努力表現(xiàn)出來。
14.Remember that overnight success usually takes 15 years.切記所謂的“一夜成名”通常需要付出“15年的努力”。
15.Leave everything better than you found it.讓一切變的更加美好。
16.Remember that winners do what losers don't want to do.切記勝利者通常會做失敗者不想做的事情。
17.When you arrive at your job in the morning,let the first thing you say brighten everyone's day.早上到辦公室時,先說一件能讓每個人在一天中感到愉快的事。
18.Don't rain on other people's parades.看到別人的輝煌時,不要抱怨。
19.Don't waste an opportunity to tell someone you love them.抓住一切機會對別人說“我愛你”。
20.Keep some things to yourself and don't promote have by hurting people you love.自己承受一些事情,不要讓所愛的人受傷害。
posted @
2007-08-18 16:21 jadmin 閱讀(51) |
評論 (0) |
編輯 收藏
程序、進程和線程
???? 程序式計算機指令的集合,它以文件的形式存儲在磁盤上
???? 進程:是一個程序在其自身的地址空間中的一次執(zhí)行活動
???? 進程是資源申請、調(diào)度和獨立運行的單位,因此,它使用系統(tǒng)中的運行資源;而程序不能申請系統(tǒng)資源,不能被系統(tǒng)調(diào)度,也不能作為獨立運行的單位,因此,它不占有系統(tǒng)地運行資源
???? 線程:是進程中的一個單一的連續(xù)控制流程。一個進程可以擁有多個線程,但至少有一個線程
???? 線程又稱為輕量級進程,它和進程一樣擁有獨立的執(zhí)行控制,由操作系統(tǒng)負責(zé)調(diào)度,區(qū)別在于線程沒有獨立的存儲空間,而是和所屬進程中的其他線程共享一個存儲空間,這使得線程間的通信遠較進程簡單
???? 單CPU下某一個時刻只能有一個線程在運行
?? Java對多線程的支持
???? Java在語言級提供了對多線程程序設(shè)計的支持
???? 實現(xiàn)多線程程序的兩種方式:
?????? (1)從Thread類(java.lang包)繼承:A thread is a thread of execution in a program.
?????? (2)實現(xiàn)Runnable接口
???? Java運行時系統(tǒng)實現(xiàn)了一個用于調(diào)度線程執(zhí)行的線程調(diào)度器(其他的語言一般是由OS調(diào)度的),用于確定某一時刻有哪一個線程在CPU上運行
???? 在Java技術(shù)中,線程通常是搶占式的而不需要時間片分配進程(分配給多個線程相等的CPU時間的進程)。搶占式調(diào)度模型就是許多線程處于可以運行狀態(tài)(等待狀態(tài)),但實際上只有一個線程在運行。該線程一只運行到它終止,進入可運行狀態(tài)(等待狀態(tài)),或者另一個具有更高優(yōu)先級的線程變成可運行狀態(tài)。在后一種情況下,低優(yōu)先級的線程被高優(yōu)先級的線程搶占,高優(yōu)先級的線程獲得運行的機會
???? Java線程調(diào)度器支持不同優(yōu)先級線程的搶占方式,但其本身不支持相同優(yōu)先級線程的時間片輪換
???? Java運行時系統(tǒng)所在的操作系統(tǒng)(例如:Windows2000)支持時間片的輪換,則線程調(diào)度器就支持相同優(yōu)先級線程的時間片輪換
----------------------------------------------------------------------------------------------------
實現(xiàn)多線程程序的一種方式:從Thread類繼承
class MultiThread
{
??????? public static void main(String[] args)//main()方法也是在一個線程當(dāng)中被執(zhí)行的
??????? {
????????????? MyThread mt=new MyThread();
????????????? //mt.setDaemon(true);//將mt聲明為后臺線程。public final void setDaemon boolean on):Marks this thread as either a daemon thread or a user thread. The Java Virtual Machine exits when the only threads running are all daemon threads.This method must be called before the thread is started.on - if true, marks this thread as a daemon thread.
????????????? mt.setPriority(Thread.MAX_PRIORITY);//設(shè)置線程優(yōu)先級void setPriority(int newPriority):Changes the priority of this thread;static int MAX_PRIORITY:The maximum priority that a thread can have.??
????????????? mt.start();//void start():Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.
????????????? int index=0;
????????????? while(true)
????????????? {
????????????????? if(index++==1000)
????????????????????? break;
????????????????? System.out.println("main:"+Thread.currentThread().getName());//static Thread currentThread():Returns a reference to the currently executing thread object; String getName():Returns this thread's name.??
????????????? }
??????? }
}
class MyThread extends Thread//There are two ways to create a new thread of execution. One is to declare a class to be a subclass of Thread. This subclass should override the run method of class Thread.
{
????????????? public void run()
????????????? {
?????????????????? while(true)??????
?????????????????? {
??????????????????????? System.out.println(getName());
???????????????????????? //yield();//中止自己static void yield():Causes the currently executing thread object to temporarily pause and allow other threads to execute.
?????????????????? }
????????????? }
}
/*
D:\java\L5>javac MultiThread.java
D:\java\L5>java MultiThread
main:main
Thread-0
在main()里,原本寫的是先調(diào)用mt.start(),即啟動mt線程,再打印main線程,但結(jié)果里是先打印出來了main線程。這是因為OS分配給main線程的時間片剛開始還沒有用完,所以繼續(xù)執(zhí)行打印了main線程,等main線程的時間片執(zhí)行完了,才執(zhí)行的MyThread線程
*/
-----------------------------------------------------------------------------------------------------------------
實現(xiàn)多線程程序的另一種方式:實現(xiàn)Runnable接口The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread. The class must define a method of no arguments called run.
class MultiThread
{
??????? public static void main(String[] args)
??????? {
????????????? MyThread mt=new MyThread();
????????????? new Thread(mt).start();//Thread的一個構(gòu)造方法:Thread(Runnable target):Allocates a new Thread object.
????????????? int index=0;
????????????? while(true)
????????????? {
????????????????? System.out.println("main:"+Thread.currentThread().getName());
????????????? }
??????? }
}
class MyThread implements Runnable//這個MyThread已經(jīng)不是從Thread類派生來的了
{
????????????? public void run()
????????????? {
?????????????????? while(true)??????
?????????????????? {
??????????????????????? System.out.println(Thread.currentThread().getName());
?????????????????? }
????????????? }
}
------------------------------------------------------------------------------------------
?? 線程的同步
???? The code segments within a program that access the same object from separate,conxurrent threads are called "critical sections"
???? 同步的兩種方式:同步快和同步方法。不管是那種方式,都是用synchronized來實現(xiàn)的
???? 每一個對象都有一個監(jiān)視器,或者叫做鎖。同步方法利用的是this所代表的對象的鎖。每個class也有一個鎖,是這個class所對應(yīng)的Class對象的鎖
?? wait、notify、notifyAll
???? 每一個對象出了一個鎖之外,還有一個等待隊列(wait set),當(dāng)一個對象剛創(chuàng)建的時候,它的等待隊列時空的
???? 我們應(yīng)該在當(dāng)前線程鎖住對象的鎖后,去掉用該對象的wait方法
???? 當(dāng)調(diào)用對象的notify方法時,將從該對象的等待隊列中刪除一個任意選擇的線程,這個線程將再次成為可運行的線程
???? 當(dāng)調(diào)用對象的notifyAll方法時,將從該對象的等待隊列中刪除所有等待的線程,這些線程將成為可運行的線程
???? wait和notify主要用于生產(chǎn)者—消費者這種關(guān)系中
-------------------------------------------------------------------------------------------
火車站售票系統(tǒng)
class TicketsSystem
{
??????????? public static void main(String[] args)
??????????? {
??????????????? SellThread st=new SellThread();//同時賣這100張票,不是應(yīng)該創(chuàng)建4個SellThread對象(SellThread st1=new SellThread()),因為如果是創(chuàng)建4個SellThread對象,那每個對象里都有100張票
??????????????? new Thread(st).start();//創(chuàng)建4個線程同時賣這100張票
??????????????? new Thread(st).start();
??????????????? new Thread(st).start();
??????????????? new Thread(st).start();
??????????? }
}
class SellThread implements Runnable
{
???? int tickets=100;
???? Object obj=new Object();
???? public void run()
???? {
???????? while(true)
????? {
???????????? synchronized(obj)//可以用synchronized(this)
???????????? {????????
???????????????? if(tickets>0)
????????? {
???????????????????? try
???????????????????? {
????????????????? Thread.sleep(10);
???????????????????? }
????????????? catch(Exception e)
?????? {
???????????????????? e.printStackTrace();
????????????? }
???????????????????? System.out.println(Thread.currentThread().getName()+
????????????????????????????????????? " sell ticket:"+tickets);
???????????????????? tickets--;
??????????????? }
??????? }
??????????? //sell();
????? }
}
????? public synchronized void sell()
????? {
????????? if(tickets>0)
{
?? System.out.println(Thread.currentThread().getName()+
??????????????????????? " sell ticket:"+tickets);
????????? tickets--;
???????? }
?????? }
}
posted @
2007-08-18 14:02 jadmin 閱讀(76) |
評論 (0) |
編輯 收藏
為什么說亂碼是中國程序員無法避免的話題呢?這個首先要從編碼機制上說起,大家都是中文和英文的編碼格式不是一樣,解碼也是不一樣的!如果中國的程序員不會遇到亂碼,那么只有使用漢語編程。漢語編程是怎么回事我也不大清楚,應(yīng)該是前年吧,我一朋友給我介紹漢語編程,怎么不錯不錯?當(dāng)時因為學(xué)習(xí)忙沒去關(guān)注這個,等我閑了,那個朋友不弄這個,問他他也不說不大清楚,最后自己對這個學(xué)習(xí)也不了了之了。
今天我寫這個不是講解中英文之間的差距,解碼等,我是將我在這幾年工作遇到各種各樣的亂碼的解決方法,總結(jié)一樣,也希望大家能把自己暈倒解決亂碼的方法都說出來,咱們弄一個解決亂碼的“葵花寶典”。
對于Java由于默認的編碼方式是 UNICODE,所以用中文也易出問題,常見的解決是
String s2 = new String(s1.getBytes(“ISO-8859-1”),”GBK”);
1、utf8解決JSP中文亂碼問題
一般說來在每個頁面的開始處,加入:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
request.setCharacterEncoding("UTF-8");
%>
charset=UTF-8 的作用是指定JSP向客戶端輸出的編碼方式為“UTF-8”
pageEncoding="UTF-8" 為了讓JSP引擎能正確地解碼含有中文字符的JSP頁面,這在LINUX中很有效
request.setCharacterEncoding("UTF-8"); 是對請求進行了中文編碼
有時,這樣仍不能解決問題,還需要這樣處理一下:
String msg = request.getParameter("message");
String str=new String(msg.getBytes("ISO-8859-1"),"UTF-8");
out.println(st);
2、Tomcat 5.5 中文亂碼
只要把%TOMCAT安裝目錄%/ webapps\servlets-examples\WEB-INF\classes\filters\SetCharacterEncodingFilter.class文件拷到你的webapp目錄/filters下,如果沒有filters目錄,就創(chuàng)建一個。
2)在你的web.xml里加入如下幾行:
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>filters.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
3)完成.
2 get方式的解決辦法
1) 打開tomcat的server.xml文件,找到區(qū)塊,加入如下一行:
URIEncoding=”GBK”
完整的應(yīng)如下:
<Connector
port="80" maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
debug="0" connectionTimeout="20000"
disableUploadTimeout="true"
URIEncoding="GBK"
/>
2)重啟tomcat,一切OK。
3、xmlHttpRequest中文問題
頁面jsp用的GBK編碼
<%@ page contentType="text/html; charset=GBK"%>
javascript部分
function addFracasReport() {
var url="controler?actionId=0_06_03_01&actionFlag=0010";
var urlmsg="&reportId="+fracasReport1.textReportId.value; //故障報告表編號
var xmlHttp=Common.createXMLHttpRequest();
xmlHttp.onreadystatechange = Common.getReadyStateHandler(xmlHttp, eval("turnAnalyPage"));
xmlHttp.open("POST",url,true);
xmlHttp.setRequestHeader( " Content-Type " , " application/x-www-form-urlencoded);
xmlHttp.send(urlmsg);
}
后臺java中獲得的reportId是亂碼,不知道該怎么轉(zhuǎn),主要是不知道xmlHttp.send(urlmsg); 以后是什么編碼?在后面用java來轉(zhuǎn),試了幾種,都沒有成功,其中有:
public static String UTF_8ToGBK(String str) {
try {
return new String(str.getBytes("UTF-8"), "GBK");
} catch (Exception ex) {
return null;
}
}
public static String UTF8ToGBK(String str) {
try {
return new String(str.getBytes("UTF-16BE"), "GBK");
} catch (Exception ex) {
return null;
}
}
public static String GBK(String str) {
try {
return new String(str.getBytes("GBK"),"GBK");
} catch (Exception ex) {
return null;
}
}
public static String getStr(String str) {
try {
String temp_p = str;
String temp = new String(temp_p.getBytes("ISO8859_1"), "GBK");
temp = sqlStrchop(temp);
return temp;
} catch (Exception e) {
return null;
}
}
4、JDBC ODBC Bridge的Bug及其解決方法
在編寫一數(shù)據(jù)庫管理程序時,發(fā)現(xiàn)JDBC-ODBC Bridge存在不易發(fā)現(xiàn)的Bug。在向數(shù)據(jù)表插入數(shù)據(jù)時,如果為英文字符,存儲內(nèi)容完全正確,如果存入中文字符,部分?jǐn)?shù)據(jù)庫只能存儲前七八個中文字符,其他內(nèi)容被截去,導(dǎo)致存儲內(nèi)容的不完整(有些數(shù)據(jù)庫不存在這個問題,如Sybase SQL Anywhere 5.0。JDBC-ODBC Bridge還存在無法建表的Bug)。
對于廣大需要存儲中文信息的Java程序員來說,這可是一個不好的消息。要么改用其他語言編程,要么選擇其他價格昂貴的數(shù)據(jù)庫產(chǎn)品。“一次編寫,到處運行”的目標(biāo),也大打折扣。能不能采用變通的方法,將中文信息進行處理后再存儲來解決這個問題呢?答案是肯定的。
解決問題的具體思路、方法
Java采用Unicode碼編碼方式,中英文字符均采用16bit存儲。既然存儲英文信息是正確的,根據(jù)一定規(guī)則,將中文信息轉(zhuǎn)換成英文信息后存儲,自然不會出現(xiàn)截尾現(xiàn)象。讀取信息時再進行逆向操作,將英文信息還原成中文信息即可。由GB2312編碼規(guī)則可知,漢字一般為二個高位為1的ASCII碼,在轉(zhuǎn)換時將一個漢字的二個高位1去掉,還原時再將二個高位1加上。為了處理含有英文字符的中文字串,對英文字符則需要加上一個Byte 0標(biāo)記。以下提供的兩個公用靜態(tài)方法,可加入任何一個類中使用。
將中英文字串轉(zhuǎn)換成純英文字串
public static String toTureAsciiStr(String str){
StringBuffer sb = new StringBuffer();
byte[] bt = str.getBytes();
for(int i =0 ; i〈bt.length; i++){
if(bt[i]〈0){
//是漢字去高位1
sb.append((char)(bt[i]&&0x7f));
}else{//是英文字符 補0作記錄
sb.append((char)0);
sb.append((char)bt[i]);
}
}
return sb.toString();
}
將經(jīng)轉(zhuǎn)換的字串還原
public static String unToTrueAsciiStr(String str){
byte[] bt = str.getBytes();
int i,l=0,length = bt.length,j=0;
for(i = 0; i〈length; i++){
if(bt[i] == 0){
l++;
}
}
byte []bt2 = new byte[length-l];
for(i =0 ; i〈length; i++){
if(bt[i] == 0){
i++;
bt2[j] = bt[i];
}else{
bt2[j] = (byte)(bt[i]|0x80);
}
j++;
}
String tt = new String(bt2);
return tt;
}
上例在實際編程中效果很好,只是存儲的中文信息需要經(jīng)過同樣處理,才能被其他系統(tǒng)使用。而且如果中文字串出現(xiàn)英文字符,實際上增加了額外的存儲空間。
5、Solaris下Servlet編程的中文問題及解決辦法
在使用Java開發(fā)Internet上的一個應(yīng)用系統(tǒng)時,發(fā)現(xiàn)在Windows下調(diào)試完全正常的Servlet,上傳到Solaris 服務(wù)器上,運行卻出現(xiàn)故障——返回的網(wǎng)頁不能顯示中文,應(yīng)為中文的信息全為亂碼;用中文信息做關(guān)鍵字,不能正確檢索數(shù)據(jù)庫。后來采用加入檢查代碼等方法探知故障原因如下:
顯示亂碼主要是因為通過類 HttpServletResponse提供的方法setContentType 無法改變返回給客戶的數(shù)據(jù)的編碼方式,正確的編碼方式應(yīng)為GB2312或者GBK,而事實上為缺省的ISO8859-1。無法檢索中文信息則是因為,客戶提交的中文信息經(jīng)瀏覽器編碼到達服務(wù)器后,Servlet無法將其正確解碼。
舉例說明顯示亂碼解決方法
Servlet 一般通常做法如下:
public class ZldTestServlet extends HttpServlet {
public void doGet (HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException{
//在使用 Writer向瀏覽器返回數(shù)據(jù)前,設(shè)置 content-type header ,在這里設(shè)置相應(yīng)的字符集gb2312
response.setContentType("text/html; charset=gb2312");
PrintWriter out = response.getWriter(); //*
// 正式返回數(shù)據(jù)
out.println("〈html〉〈head〉〈title〉Servlet test〈/title〉〈/head〉" );
out.println("這是一個測試頁!");
out.println("〈/body〉〈/html〉");
out.close();
}
...
}
解決頁面顯示亂碼問題,需將*處代碼換成如下內(nèi)容:
PrintWriter out = new PrintWriter(new OutputStreamWriter(response.getOutputStream(),"gb2312"));
Solaris中文信息檢索問題的解決
瀏覽器利用表單向服務(wù)器提交信息時,一般采用x-www-form-urlencoded 的MIME格式對數(shù)據(jù)進行編碼。如果使用get方法,參數(shù)名稱和參數(shù)值經(jīng)編碼后附加在URL后,在Java中稱作查詢串(query string)。
在Servlet程序中,如果采用ServletRequest的方法getParameter取得參數(shù)值,在Solaris環(huán)境下,對漢字卻不能正確解碼。因而無法正確檢索數(shù)據(jù)庫。
在Java 1.2的包——java.net中提供了URLEncode和URLDecode類。類URLEncode提供了按x-www-form-urlencoded格式對給定串進行轉(zhuǎn)換的方法。類URLEncode則提供了逆方法。
6、Common Mail亂碼問題
common mail是一個小而方便的mail包,他實現(xiàn)了對Java Mail的封裝,使用起來十分的方便,但是我在使用他的時候發(fā)現(xiàn),使用純文本的內(nèi)容發(fā)送,結(jié)果是亂碼,代碼如下:
public class TestCommonMail {
public static void main(String[] args) throws EmailException, MessagingException {
SimpleEmail email = new SimpleEmail();
email.setCharset("GB2312");
email.setHostName("smtp.163.com");
email.setSubject("test");
email.addTo("test@163.com");
email.setFrom("test@163.com");
email.setMsg("我的測試");
email.setAuthentication("test", "test");
email.send();
}
}
分析了一下commons mail的源碼找到了原因。源碼如下:
public class SimpleEmail extends Email
{
public Email setMsg(String msg) throws EmailException, MessagingException
{
if (EmailUtils.isEmpty(msg))
{
throw new EmailException("Invalid message supplied");
}
setContent(msg, TEXT_PLAIN);
return this;
}
}
Email代碼片段
public void setContent(Object aObject, String aContentType)
{
this.content = aObject;
if (EmailUtils.isEmpty(aContentType))
{
this.contentType = null;
}
else
{
// set the content type
this.contentType = aContentType;
// set the charset if the input was properly formed
String strMarker = "; charset=";
int charsetPos = aContentType.toLowerCase().indexOf(strMarker);
if (charsetPos != -1)
{
// find the next space (after the marker)
charsetPos += strMarker.length();
int intCharsetEnd =
aContentType.toLowerCase().indexOf(" ", charsetPos);
if (intCharsetEnd != -1)
{
this.charset =
aContentType.substring(charsetPos, intCharsetEnd);
}
else
{
this.charset = aContentType.substring(charsetPos);
}
}
}
}
email.send(); 的send方法將調(diào)用
public void buildMimeMessage() throws EmailException
{
try
{
this.getMailSession();
this.message = new MimeMessage(this.session);
if (EmailUtils.isNotEmpty(this.subject))
{
if (EmailUtils.isNotEmpty(this.charset))
{
this.message.setSubject(this.subject, this.charset);
}
else
{
this.message.setSubject(this.subject);
}
}
// ========================================================
// Start of replacement code
if (this.content != null)
{
this.message.setContent(this.content, this.contentType);
}
// end of replacement code
// ========================================================
else if (this.emailBody != null)
{
this.message.setContent(this.emailBody);
}
else
{
this.message.setContent("", Email.TEXT_PLAIN);
}
if (this.fromAddress != null)
{
this.message.setFrom(this.fromAddress);
}
else
{
throw new EmailException("Sender address required");
}
if (this.toList.size() + this.ccList.size() + this.bccList.size() == 0)
{
throw new EmailException(
"At least one receiver address required");
}
if (this.toList.size() > 0)
{
this.message.setRecipients(
Message.RecipientType.TO,
this.toInternetAddressArray(this.toList));
}
if (this.ccList.size() > 0)
{
this.message.setRecipients(
Message.RecipientType.CC,
this.toInternetAddressArray(this.ccList));
}
if (this.bccList.size() > 0)
{
this.message.setRecipients(
Message.RecipientType.BCC,
this.toInternetAddressArray(this.bccList));
}
if (this.replyList.size() > 0)
{
this.message.setReplyTo(
this.toInternetAddressArray(this.replyList));
}
if (this.headers.size() > 0)
{
Iterator iterHeaderKeys = this.headers.keySet().iterator();
while (iterHeaderKeys.hasNext())
{
String name = (String) iterHeaderKeys.next();
String value = (String) headers.get(name);
this.message.addHeader(name, value);
}
}
if (this.message.getSentDate() == null)
{
this.message.setSentDate(getSentDate());
}
if (this.popBeforeSmtp)
{
Store store = session.getStore("pop3");
store.connect(this.popHost, this.popUsername, this.popPassword);
}
}
catch (MessagingException me)
{
throw new EmailException(me);
}
}
由代碼可以知道純文本方式最終調(diào)用了Java Mail的
message.setContent(this.content, this.contentType);
content是內(nèi)容
contentType是類型,如text/plain,
(我們可以試試直接用Java mail發(fā)郵件,設(shè)置文本內(nèi)容不使用setText方法,也使用setContent("測試", "text/plain")方式,你可以看到內(nèi)容也是亂碼)
關(guān)鍵就在于text/plain,我們改成text/plain; charset=gb2312,ok亂碼解決了。在commons mail我們看SimpleEmail 類中setMsg方法調(diào)用的就是 setContent(msg, TEXT_PLAIN); 我們只需要將Email類中的常量TEXT_PLAIN修改一下加入 charset=你的字符集 ,重新打包jar,這樣就可以了
7、toad的字符集的設(shè)置與oracle的安裝
oracle數(shù)據(jù)庫服務(wù)器的安裝一般是中文字符集,有時安裝在不同的平臺下,設(shè)置為ISO編碼,toad是oracle開發(fā)的最好工具,不是我說的,可是中文環(huán)境下安裝的toad,打開英文字符的oracle時,中文全是亂碼。必須進行設(shè)置
環(huán)境變量---〉系統(tǒng)變量
加
NLS_lANG=SIMPLIFIED CHINESE_CHINA.ZHS16GBK
或
NLS_lANG=AMERICAN_AMERICA.WE8ISO8859P1
AMERICAN_AMERICA.WE8MSWIN1252
或者
打開注冊表,點擊HKEY_LOCAL_MATHINE
再點擊Software,再點擊ORACLE
在點擊HOME(ORACLE所在目錄)
在注冊表的右半面有NLS_LANG,
雙擊它,將你想要的覆蓋掉原來的就可以了
最好記下舊的,以便可以改回來。
connect sys/chang_on_install
update props$
set value$='ZHS16CGB231280'
where name='NLS_CHARACTERSET';
commit;
這樣就OK了
8、如何解決GWT(google web toolkit)中文的問題
GWT 中文亂碼解決方法
1.把你要顯示的中文“測試字符串”輸入到一個文件,如:1.txt
2.進入命令行,進入1.txt所在的目錄,敲入以下命令:native2ascii.exe 1.txt 2.txt 回車。這樣就生成了另外一個文件2.txt。
3.2.txt的內(nèi)容如下:\u6d4b\u8bd5\u5b57\u7b26\u4e32
4.然后用上面的編碼,在gwt中使用,就可以了.
9、xmlHttp得到的網(wǎng)頁怎么是亂碼?
(1)在服務(wù)器端使用WebRequest而不是xmlHttp
(2) 將
StreamReader sr = new StreamReader(stream);
對于簡體中文改成:
StreamReader sr = new StreamReader(stream , Encoding.Default );
對于utf-8改成:
StreamReader sr = new StreamReader(stream , Encoding.UTF8 );
當(dāng)然,Encoding枚舉還有很多其他的成員,對于不同的編碼content-type可以有選擇的應(yīng)用
(3)后來我發(fā)現(xiàn)無論是content-type是gb2312還是utf-8,用
StreamReader sr = new StreamReader(stream , Encoding.Default );
都可以返回正常的漢字,所以統(tǒng)一的改成Encoding.Default
--------------------------------------------------------------------------------
最后,在服務(wù)器端從一個url獲得網(wǎng)頁的源代碼的代碼如下:
/// <summary>
/// post一個指定的url,獲得網(wǎng)頁的源代碼(用WebRequest實現(xiàn))
/// </summary>
/// <param name="url"></param>
/// <returns>
/// 如果請求失敗,返回null
/// 如果請求成功,返回網(wǎng)頁的源代碼
/// </returns>
public static string GetContentFromUrl2( string url )
{
//變量定義
string respstr;
WebRequest myWebRequest=WebRequest.Create(url);
// myWebRequest.PreAuthenticate=true;
// NetworkCredential networkCredential=new NetworkCredential( username , password , domain );
// myWebRequest.Credentials=networkCredential;
// Assign the response object of 'WebRequest' to a 'WebResponse' variable.
WebResponse myWebResponse=myWebRequest.GetResponse();
System.IO.Stream stream = myWebResponse.GetResponseStream();
StreamReader sr = new StreamReader(stream , Encoding.Default );
//以字符串形式讀取數(shù)據(jù)流
respstr = sr.ReadToEnd();
sr.Close();
return respstr;
}
來源:soddabao-BlogJava
posted @
2007-08-17 02:00 jadmin 閱讀(49) |
評論 (0) |
編輯 收藏
前言
ant是java開發(fā)者工具箱的重要一環(huán),junit,xdoclet等都與它緊密關(guān)聯(lián),程序員可能習(xí)慣了IDE提供的自動構(gòu)建,甚至部署的功能,從而忽略了ant本身,其實,主流的IDE通常是內(nèi)置ant任務(wù)來完成這些工作的,熟悉ant內(nèi)在的機理,可以閱讀或簡單修改build.xml無疑可以幫助你更靈活地集成、管理應(yīng)用項目,如果需要學(xué)習(xí)maven這種開源項目管理解決方案,也是要以理解ant為基礎(chǔ)的喲。另外,使用ant的過程實際上對構(gòu)建進行了文檔化,它是無關(guān)于IDE的,想象一下,你的同事中可能三分之一在用JbuilderX,三分之一用eclipse,還有一些是別的。
本人使用eclipse3.0.1,以前的構(gòu)建和發(fā)布工作都由myeclipse插件作了,趁周末實踐了一下手動構(gòu)建,記此備忘。
實踐
準(zhǔn)備工作:這是我的個人習(xí)慣,把所有公用的類庫jar置于一個固定目錄,分好類,不要丟在一個文件夾下,如jakarta-commons、hibernate、spring、struts等,這些是源碼構(gòu)建時需要用到的,在部署時可能有一些不用再打進去了,比如servlet.jar。如果你們有自己的framework,也一并放在這里。然后,打開eclipse,進入Windows->Preferences->Java->User Libraries,增加一個自己的庫,比如說mylib,把剛才那些公共的jar全部添入,這樣有個好處,在eclipse項目中,不用再看到煩人的長長的jar列表了,比較整潔。
下來正式進行:
1.新建一個Java Project,此時就不要再選你的j2ee插件內(nèi)置的一些選項了,至簡即可。
2.在root下建幾個文件夾,我們在網(wǎng)上下載的開源項目中經(jīng)常可以看到這些,比如:
src - 源碼
classes - 編譯
web - jsp等
lib - 庫,這里可以簡單地把mylib下的東東copy過來,便于將來發(fā)布源碼。
dlist - 輸出的jar或war
當(dāng)然,我們要建一個build.xml,eclipse中會出現(xiàn)一個螞蟻的小圖標(biāo),一般這個文件建立后,下一個項目簡單的copy過去,稍加改動就可以了。
3.打開項目的屬性頁,在Java Build Path的庫選項中,加入我們自定義的公共庫mylib.至于Builders方式就不用改了,使用默認的Java Builer即可,我只是項目部署時使用ant,平常的排錯工作就交給IDE吧。
4.重中之重,寫你的build.xml,網(wǎng)上文章很海,我這里就不再?嗦了,基本上就分那幾個任務(wù):
4.1 先要聲明一些路徑變量,如
<property name="war.dir" value="dlist" />
也可以將其寫至properties文件中,在這里引用;
4.2 聲明編譯的類路徑,如下:
<path id="master-classpath">
<fileset dir="${lib.root}/struts">
<include name="struts-menu-2.3.jar" />
<include name="struts.jar" />
</fileset>
<fileset dir="${lib.root}/jakarta-commons">
<include name="commons-*.jar" />
</fileset>
<fileset dir="${lib.root}/ibatis2.0.9">
<include name="ibatis-*.jar" />
</fileset>
<fileset dir="${lib.root}/jdbcdriver">
<include name="jtds-0.9-rc2.jar" />
</fileset>s
......
</path>
4.3 清空輸出目錄,如web,dlist等。
4.4 編譯構(gòu)建:
<target name="build" description="Compile main source tree java files into class files, generate jar files">
<mkdir dir="${build.dir}" />
<javac destdir="${build.dir}" source="1.3" target="1.3" debug="true" deprecation="false" optimize="false" failonerror="true">
<src path="${src.dir}" />
<classpath refid="master-classpath" />
</javac>
<copy todir="${build.dir}" preservelastmodified="true">
<fileset dir="${src.dir}">
<include name="**/*.xml" />
<include name="**/*.properties" />
</fileset>
</copy>
<!-- ============================================= -->
<!-- 據(jù)測試,資源文件不能被打到j(luò)ar文件中,其余均可 -->
<!-- ============================================= -->
<copy todir="${webclasses.dir}/conf" preservelastmodified="true">
<fileset dir="${src.dir}/conf">
<include name="springResources*.properties" />
</fileset>
</copy>
<mkdir dir="${weblib.dir}" />
<jar jarfile="${weblib.dir}/${name}.jar" compress="true">
<fileset dir="${build.dir}">
<include name="**" />
</fileset>
</jar>
<copy todir="${weblib.dir}" preservelastmodified="true">
<fileset dir="${lib.root}">
<include name="log4j-1.2.8.jar" />
</fileset>
<fileset dir="${lib.root}/struts">
<include name="struts-menu-2.3.jar" />
<include name="struts.jar" />
</fileset>
<fileset dir="${lib.root}/jakarta-commons">
<include name="commons-*.jar" />
</fileset>
<fileset dir="${lib.root}/spring-1.1.3">
<include name="spring.jar" />
<include name="aopalliance.jar" />
</fileset>
......
</copy>
</target>
<!-- ============================================= -->
<!-- Compile main Java sources and copy libraries -->
<!-- ============================================= -->
<target name="warfile" description="Build the web application archive">
<mkdir dir="${dist.dir}" />
<war warfile="${dist.dir}/${name}.war" basedir="${war.dir}" webxml="${war.dir}/WEB-INF/web.xml">
<include name="*" />
<include name="WEB-INF/*.*" />
<exclude name="WEB-INF/web.xml" />
<include name="WEB-INF/classes/*.*" />
<include name="WEB-INF/lib/**" />
<exclude name="**/.*" />
</war>
</target>
4.5 打成war
<target name="warfile" description="Build the web application archive">
<mkdir dir="${dist.dir}" />
<war warfile="${dist.dir}/${name}.war" basedir="${war.dir}" webxml="${war.dir}/WEB-INF/web.xml">
<include name="*" />
<include name="WEB-INF/*.*" />
<exclude name="WEB-INF/web.xml" />
<include name="WEB-INF/classes/*.*" />
<include name="WEB-INF/lib/**" />
<exclude name="**/.*" />
</war>
</target>
4.6 把幾個任務(wù)串起來,弄一個default target
<target name="all">
<antcall target="clean" />
<antcall target="build" />
<antcall target="warfile" />
</target>
打完收功。在實踐中發(fā)現(xiàn),一些配置文件,如struts-config.xml ibatis和spring的xml都可以打進jar文件,spring資源文件好象不行,得單獨copy至WEB-INFclasses下,另外,你的web文件夾下,事先得放好web.xml,以及一些tld文件喲!
posted @
2007-08-15 01:33 jadmin 閱讀(69) |
評論 (0) |
編輯 收藏
1。安裝tomcat5.5的admin package tomcat5.5版本中的admin沒有默認安裝,先下載admin package jakarta-tomcat-5.5.9-admin.zip或者jakarta-tomcat-5.5.9-admin.tar.gz(這兩個package應(yīng)該是一樣的,下載地址http://mirrors.uol.com.br/pub/apache/jakarta/tomcat-5/v5.5.9/bin/)。
將其中的/conf/Catalina/localhost/admin.xml復(fù)制到%JAVA_HOME%\conf\Catalina\localhost\目錄下,將server/webapps/admin文件夾復(fù)制到%JAVA_HOME%\server\webapps目錄下。另外的RELEASE-NOTES,NOTICE,LICENSE直接復(fù)制到%JAVA_HOME%目錄下即可,不知道是不是一定得要的,自己可以試試,呵呵。這樣就完成了admin package的安裝。
2。配置admin 上面完成了admin package的安裝,現(xiàn)在可以進入http://localhost:8080/admin。首先進入一個登陸窗口,用戶名默認是admin,密碼為空,當(dāng)然你可以在安裝tomcat的時候修改。進入以后,找到Data Sources,單擊,在右邊的data Source Actions中選擇Create New Data Source.下面是要填寫的信息。
(示例) 如果你安裝的是ms的jdbc;
JNDI Name:jdbc/sqlserver Data Source URL:jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=sqlserver JDBC Driver Class: com.microsoft.jdbc.sqlserver.SQLServerDriver 驅(qū)動名稱 User Name:sa//數(shù)據(jù)庫登陸用戶名和密碼 Password:sa Max.Active Connections:4 Max.Idle Connections:2 Max.Wait for Connection:5000 Validation Query: 如果你安裝的是jtds; JNDI Name:jdbc/sqlserver Data Source URL:jdbc:jtds:sqlserver://localhost:1433/sqlserver 其中sqlserver是你的數(shù)據(jù)庫名稱 JDBC Driver Class: net.sourceforge.jtds.jdbc.Driver 驅(qū)動名稱 User Name:sa//數(shù)據(jù)庫登陸用戶名和密碼 Password:sa Max.Active Connections:4 Max.Idle Connections:2 Max.Wait for Connection:5000 Validation Query:
字串5
3。文件配置通過文件夾導(dǎo)航到%TOMCAT_HOME%\conf,打開web.xml,在的前面添加以下內(nèi)容: DB Connection jdbc/sqlserver javax.sql.DataSource Container
注意res-ref-name填寫的內(nèi)容要與在上文提到的JNDI Name名稱一致。 配置完成后重新啟動tomcat,在%JAVA_HOME%\\webapps\ROOT\META-INF下找到對應(yīng)的配置文件Context.xml,檢查其內(nèi)容。
下面是參考信息。
WEB-INF/web.xml WEB-INF/web.xml 如果是舊版本,那么通過文件夾導(dǎo)航到%TOMCAT_HOME%\conf\Catalina\localhost下,找到你的web應(yīng)用對應(yīng)的.xml文件,如xmdc.xml,并在此文件之前添入一行代碼:
重新啟動tomcat,應(yīng)該沒什么問題了。
posted @
2007-08-15 01:24 jadmin 閱讀(44) |
評論 (0) |
編輯 收藏