锘??xml version="1.0" encoding="utf-8" standalone="yes"?>亚洲中文无码永久免,久久亚洲AV无码西西人体,亚洲?V乱码久久精品蜜桃http://www.tkk7.com/honzeland/category/17080.htmlzh-cnFri, 12 Oct 2007 09:04:40 GMTFri, 12 Oct 2007 09:04:40 GMT60濡備綍浣跨敤Log4j錛?/title><link>http://www.tkk7.com/honzeland/articles/133434.html</link><dc:creator>honzeland</dc:creator><author>honzeland</author><pubDate>Mon, 30 Jul 2007 13:13:00 GMT</pubDate><guid>http://www.tkk7.com/honzeland/articles/133434.html</guid><wfw:comment>http://www.tkk7.com/honzeland/comments/133434.html</wfw:comment><comments>http://www.tkk7.com/honzeland/articles/133434.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.tkk7.com/honzeland/comments/commentRss/133434.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/honzeland/services/trackbacks/133434.html</trackback:ping><description><![CDATA[From錛歨ttp://www.tkk7.com/rickhunter/articles/28133.html<br /> <br /> <font size="2"> <strong>1銆?Log4j鏄粈涔堬紵<br /> </strong>  Log4j鍙互甯姪璋冭瘯錛堟湁鏃跺檇ebug鏄彂鎸ヤ笉浜嗕綔 鐢ㄧ殑錛夊拰鍒嗘瀽錛岃涓嬭澆鍜屼簡瑙f洿璇︾粏鐨勫唴瀹癸紝榪樻槸璁塊棶鍏跺畼鏂圭綉绔欏惂錛?/font> <a > <font size="2">http://jakarta.apache.org/log4j</font> </a> <font size="2">銆?br /> <br /> <strong>2銆丩og4j鐨勬蹇?/strong><br />   <!--startfragment --> Log4j涓湁涓変釜涓昏鐨勭粍浠訛紝瀹冧滑鍒嗗埆鏄?/font> <font size="2">Logger銆丄ppender鍜孡ayout錛孡<!--startfragment -->og4j 鍏佽寮鍙戜漢鍛樺畾涔夊涓狶ogger錛屾瘡涓狶ogger鎷ユ湁鑷繁鐨勫悕瀛楋紝Logger涔嬮棿閫氳繃鍚嶅瓧鏉ヨ〃鏄庨毝灞炲叧緋匯傛湁涓涓狶ogger縐頒負Root錛屽畠姘歌繙 瀛樺湪錛屼笖涓嶈兘閫氳繃鍚嶅瓧媯绱㈡垨寮曠敤錛屽彲浠ラ氳繃Logger.getRootLogger()鏂規硶鑾峰緱錛屽叾瀹僉ogger閫氳繃 Logger.getLogger(String name)鏂規硶銆?br />    Appender鍒欐槸鐢ㄦ潵鎸囨槑灝嗘墍鏈夌殑log淇℃伅瀛樻斁鍒頒粈涔堝湴鏂癸紝Log4j涓敮鎸佸縐峚ppender錛屽<!--startfragment --></font> <font size="3"> </font> <font size="2">console銆乫iles銆丟UI components銆丯T Event Loggers絳夛紝涓涓狶ogger鍙互鎷ユ湁澶氫釜Appender錛屼篃灝辨槸浣犳棦鍙互灝哃og淇℃伅杈撳嚭鍒板睆騫曪紝鍚屾椂瀛樺偍鍒頒竴涓枃浠朵腑銆?br />    Layout鐨勪綔鐢ㄦ槸鎺у埗Log淇℃伅鐨勮緭鍑烘柟寮忥紝涔熷氨鏄牸寮忓寲杈撳嚭鐨勪俊鎭?br />    Log4j涓皢瑕佽緭鍑虹殑Log淇℃伅瀹氫箟浜?縐嶇駭鍒紝渚濇涓篋EBUG銆両NFO銆乄ARN銆丒RROR鍜孎ATAL錛屽綋杈撳嚭鏃訛紝鍙湁綰у埆楂樿繃閰嶇疆涓瀹氱殑 綰у埆鐨勪俊鎭墠鑳界湡姝g殑杈撳嚭錛岃繖鏍峰氨寰堟柟渚跨殑鏉ラ厤緗笉鍚屾儏鍐典笅瑕佽緭鍑虹殑鍐呭錛岃屼笉闇瑕佹洿鏀逛唬鐮侊紝榪欑偣瀹炲湪鏄柟渚垮晩銆?br /> <br /> <strong>3銆丩og4j鐨勯厤緗枃浠?/strong><br />   铏界劧鍙互涓嶇敤閰嶇疆鏂囦歡錛岃屽湪紼嬪簭涓疄鐜伴厤緗紝浣嗚繖縐嶆柟娉曞湪濡備粖鐨勭郴緇熷紑鍙戜腑鏄劇劧鏄笉鍙彇鐨勶紝鑳介噰鐢ㄩ厤緗枃浠剁殑鍦版柟涓瀹氫竴瀹氳鐢ㄩ厤緗枃浠躲侺og4j鏀寔涓? 縐嶆牸寮忕殑閰嶇疆鏂囦歡錛歑ML鏍煎紡鍜孞ava鐨刾roperty鏍煎紡錛屾湰浜烘洿鍠滄鍚庤咃紝棣栧厛鐪嬩竴涓畝鍗曠殑渚嬪瓙鍚э紝濡備笅錛?br /> <br /> </font> <font color="#614db3"> <font size="2">  log4j.rootLogger=debug, <strong>stdout, R</strong><br />   log4j.appender.<strong>stdout</strong>=org.apache.log4j.ConsoleAppender<br />   log4j.appender.stdout.layout=org.apache.log4j.PatternLayout<br /> <br />   # Pattern to output the caller's file name and line number.<br />   log4j.appender.stdout.layout.ConversionPattern=%5p [%t] <strong>(%F:%L)</strong> - %m%n<br /> <br />   log4j.appender.<strong>R</strong>=org.apache.log4j.RollingFileAppender<br />   log4j.appender.R.File=example.log<br />   log4j.appender.R.MaxFileSize=</font> <font size="2"> <strong>100KB<br /> </strong> <br />   # Keep one backup file<br />   log4j.appender.R.MaxBackupIndex=1<br /> <br />   log4j.appender.R.layout=org.apache.log4j.PatternLayout<br />   log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n         <br /> <br /> </font> <font color="#000000"> <font size="2">  棣栧厛錛屾槸璁劇疆root錛屾牸寮忎負<!--startfragment --> log4j.rootLogger=[level],appenderName, ...錛屽叾涓璴evel灝辨槸璁劇疆闇瑕佽緭鍑轟俊鎭殑綰у埆錛屽悗闈㈡槸appender鐨勮緭鍑虹殑鐩殑鍦幫紝<!--startfragment -->appenderName灝辨槸鎸囧畾鏃ュ織淇℃伅杈撳嚭鍒板摢涓湴鏂廣傛偍鍙互鍚屾椂鎸囧畾澶氫釜杈撳嚭鐩殑鍦般?/font> <font size="2">閰嶇疆鏃ュ織淇℃伅杈撳嚭鐩殑鍦癆ppender錛屽叾璇硶涓?br /> </font> <font size="2">  log4j.appender.appenderName = fully.qualified.name.of.appender.class<br />   log4j.appender.appenderName.option1 = value1<br />   ...<br />   log4j.appender.appenderName.option = valueN</font> <br /> <font size="2">Log4j鎻愪緵鐨刟ppender鏈変互涓嬪嚑縐嶏細<br />   org.apache.log4j.ConsoleAppender錛堟帶鍒跺彴錛?br />   org.apache.log4j.FileAppender錛堟枃浠訛級<br />   org.apache.log4j.DailyRollingFileAppender錛堟瘡澶╀駭鐢熶竴涓棩蹇楁枃浠訛級<br />   org.apache.log4j.RollingFileAppender錛堟枃浠跺ぇ灝忓埌杈炬寚瀹氬昂瀵哥殑鏃跺欎駭鐢熸柊鏂囦歡錛?br />   org.apache.log4j.WriterAppender錛堝皢鏃ュ織淇℃伅浠ユ祦鏍煎紡鍙戦佸埌浠繪剰鎸囧畾鐨勫湴鏂癸級<br /> </font> </font> </font> <font color="#614db3"> <font color="#000000"> <font size="2">閰嶇疆鏃ュ織淇℃伅鐨勬牸寮忥紙甯冨眬錛夛紝鍏惰娉曚負錛?br /> </font> <font size="2">  log4j.appender.appenderName.layout = fully.qualified.name.of.layout.class<br />   log4j.appender.appenderName.layout.option1 = value1<br />   ....<br />   log4j.appender.appenderName.layout.option = valueN</font> <br /> <font size="2">Log4j鎻愪緵鐨刲ayout鏈変互涓嬪嚑縐嶏細<br />   org.apache.log4j.HTMLLayout錛堜互HTML琛ㄦ牸褰㈠紡甯冨眬錛夛紝<br />   org.apache.log4j.PatternLayout錛堝彲浠ョ伒媧誨湴鎸囧畾甯冨眬妯″紡錛夛紝<br />   org.apache.log4j.SimpleLayout錛堝寘鍚棩蹇椾俊鎭殑綰у埆鍜屼俊鎭瓧絎︿覆錛夛紝<br />   org.apache.log4j.TTCCLayout錛堝寘鍚棩蹇椾駭鐢熺殑鏃墮棿銆佺嚎紼嬨佺被鍒瓑絳変俊鎭級 <br /> <br /> </font> </font> </font> <font color="#000000"> <span style="font-size: 10.5pt;"> <font size="2"> <span lang="EN-US">Log4J閲囩敤綾諱技C璇█涓殑printf鍑芥暟鐨勬墦鍗版牸寮忔牸寮忓寲鏃ュ織淇℃伅錛屾墦鍗板弬鏁板涓嬶細 %m 杈撳嚭浠g爜涓寚瀹氱殑娑堟伅<o:p></o:p></span> </font> </span> </font> <p> <font color="#000000"> <span style="font-size: 10.5pt;"> <font size="2">銆銆</font> <span lang="EN-US"> <font size="2">%p 杈撳嚭浼樺厛綰э紝鍗矰EBUG錛孖NFO錛學ARN錛孍RROR錛孎ATAL <br /> %r 杈撳嚭鑷簲鐢ㄥ惎鍔ㄥ埌杈撳嚭璇og淇℃伅鑰楄垂鐨勬縐掓暟 <br /> %c 杈撳嚭鎵灞炵殑綾葷洰錛岄氬父灝辨槸鎵鍦ㄧ被鐨勫叏鍚?<br /> %t 杈撳嚭浜х敓璇ユ棩蹇椾簨浠剁殑綰跨▼鍚?<br /> %n 杈撳嚭涓涓洖杞︽崲琛岀錛學indows騫沖彴涓?#8220;\r\n”錛孶nix騫沖彴涓?#8220;\n” <br /> %d 杈撳嚭鏃ュ織鏃墮棿鐐圭殑鏃ユ湡鎴栨椂闂達紝榛樿鏍煎紡涓篒SO8601錛屼篃鍙互鍦ㄥ叾鍚庢寚瀹氭牸寮忥紝姣斿錛?d{yyy MMM dd HH:mm:ss,SSS}錛岃緭鍑虹被浼鹼細</font> </span> </span> <st1:chsdate isrocdate="False" islunardate="False" day="18" month="10" year="2002"> <span style="font-size: 10.5pt;" lang="EN-US"> <font size="2">2002騫?0鏈?8鏃?/font> </span> </st1:chsdate> <span style="font-size: 10.5pt;" lang="EN-US"> <font size="2"> 22錛?0錛?8錛?21 <br /> %l 杈撳嚭鏃ュ織浜嬩歡鐨勫彂鐢熶綅緗紝鍖呮嫭綾葷洰鍚嶃佸彂鐢熺殑綰跨▼錛屼互鍙婂湪浠g爜涓殑琛屾暟銆備婦渚嬶細Testlog4.main(TestLog4.java:10)</font> </span> </font> </p> <br /> <font color="#614db3"> <font color="#000000"> <font size="2"> <br /> <strong>4銆丩og4j鍦ㄧ▼搴忎腑鐨勪嬌鐢?/strong> </font> </font> </font> <font color="#614db3"> <font color="#000000"> <br /> </font> <font color="#a0a0a0"> <font color="#090909" size="2">  瑕佸湪鑷繁鐨勭▼搴忎腑浣跨敤Log4j錛岄鍏堥渶瑕佸皢commons-logging.jar鍜宭ogging-log4j-1.2.9.jar瀵煎叆鍒版瀯寤鴻礬寰? 涓傜劧鍚庡啀灝唋og4j.properties鏀懼埌src鏍圭洰褰曚笅銆傝繖鏍峰氨鍙互鍦ㄧ▼搴忎腑浣跨敤log4j浜嗐傚湪綾諱腑浣跨敤log4j錛?/font> </font> </font> <font color="#614db3"> <font color="#a0a0a0"> <font color="#090909" size="2">棣栧厛澹版槑涓涓潤鎬佸彉閲?/font> </font> </font> <font color="#614db3"> <font color="#a0a0a0"> <font color="#090909" size="2">Logger logger=Logger.getLog("classname")錛涚幇鍦ㄥ氨鍙互浣跨敤浜嗭紝鐢ㄦ硶濡備笅錛歭ogger.debug("debug message")鎴栬卨ogger.info("info message")錛岀湅涓嬮潰涓涓皬渚嬪瓙錛?/font> </font> </font> <font color="#614db3"> <font color="#a0a0a0"> <br /> </font> <br /> <font size="2">  import com.foo.Bar;<br />   import org.apache.log4j.Logger;<br />   import org.apache.log4j.PropertyConfigurator;<br />   public class MyApp {<br />     static Logger logger = Logger.getLogger(MyApp.class.getName());<br />     public static void main(String[] args) {<br />       // BasicConfigurator replaced with PropertyConfigurator.<br />       PropertyConfigurator.configure(args[0]);<br />       logger.info("Entering application.");<br />       Bar bar = new Bar();<br />       bar.doIt();<br />       logger.info("Exiting application.");<br />     }<br />   }<br /> <br /> <br /> <br /> </font></font> <div id="m24m4m4" class="postTitle"> zz: http://www.tkk7.com/apple0668/archive/2007/10/11/152150.html<br /> <a id="viewpost1_TitleUrl" class="postTitle2" href="../../apple0668/archive/2007/10/11/152150.html">Log4閰嶇疆</a><br /> </div> <p>涓銆佸父鐢ㄨ緭鍑烘牸寮?/p> <p>%c   鍒楀嚭logger鍚嶅瓧絀洪棿鐨勫叏縐幫紝濡傚姞涓妠<灞傛暟>}琛ㄧず鍑轟粠鏈鍐呭眰綆楄搗鐨勬寚瀹氬眰鏁扮殑鍚嶅瓧絀洪棿<br /> %X  鎸塎DC錛圡apped Diagnostic Context,綰跨▼鏄犲皠琛級杈撳嚭鏃ュ織銆傞氬父鐢ㄤ簬澶氫釜瀹㈡埛绔繛鎺ュ悓涓鍙版湇鍔″櫒錛屾柟渚挎湇鍔″櫒鍖哄垎鏄偅涓鎴風璁塊棶鐣欎笅鏉ョ殑鏃ュ織銆?br /> %p  鏃ュ織淇℃伅綰у埆<br /> %d   %d{<鏃ユ湡鏍煎紡>}:鏃ュ織淇℃伅浜х敓鏃墮棿,浣跨敤ISO8601瀹氫箟鐨勬棩鏈熸牸寮?br /> %C   鏃ュ織淇℃伅鎵鍦ㄥ湴錛堝叏闄愮被鍚嶏級<br /> %m   浜х敓鐨勬棩蹇楀叿浣撲俊鎭?br /> %n    杈撳嚭鏃ュ織淇℃伅鎹㈣<br /> %F銆鏄劇ず璋冪敤logger鐨勬簮鏂囦歡鍚?br /> %l     杈撳嚭鏃ュ織浜嬩歡鐨勫彂鐢熶綅緗紝鍖呮嫭綾葷洰鍚嶃佸彂鐢熺殑綰跨▼錛屼互鍙婂湪浠g爜涓殑琛屾暟<br /> %L    鏄劇ず璋冪敤logger鐨勪唬鐮佽<br /> %M   鏄劇ず璋冪敤logger鐨勬柟娉曞悕<br /> %r     鏄劇ず浠庣▼搴忓惎鍔ㄦ椂鍒拌褰曡鏉℃棩蹇楁椂宸茬粡緇忚繃鐨勬縐掓暟<br /> %t     杈撳嚭浜х敓璇ユ棩蹇椾簨浠剁殑綰跨▼鍚?br /> %%銆鏄劇ず涓涓?br /> 浜屻乴og4j.properties<br /> </p> <p>#鎺у埗鍖呬腑鏃ュ織杈撳嚭綰у埆<br /> log4j.logger.org.apache.struts = debug</p> <p># 搴旂敤浜庢帶鍒跺彴<br /> log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender<br /> log4j.appender.Threshold=DEBUG<br /> log4j.appender.CONSOLE.Target=System.out<br /> log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout<br /> log4j.appender.CONSOLE.layout.ConversionPattern=[framework] %d - %-4r [%t] %-5p %c %x - %m%n<br /> #log4j.appender.CONSOLE.layout.ConversionPattern=[start]%d{DATE}[DATE]%n%p[PRIORITY]%n%x[NDC]%n%t[THREAD] n%c[CATEGORY]%n%m[MESSAGE]%n%n</p> <p>#搴旂敤浜庢枃浠?br /> log4j.appender.FILE=org.apache.log4j.FileAppender<br /> log4j.appender.FILE.File=file.log<br /> log4j.appender.FILE.Append=false<br /> log4j.appender.FILE.layout=org.apache.log4j.PatternLayout<br /> log4j.appender.FILE.layout.ConversionPattern=[framework] %d - %-4r [%t] %-5p %c %x - %m%n<br /> # Use this layout for LogFactor 5 analysis</p> <p># 搴旂敤浜庢枃浠跺洖婊?br /> log4j.appender.ROLLING_FILE=org.apache.log4j.RollingFileAppender<br /> log4j.appender.ROLLING_FILE.Threshold=ERROR<br /> log4j.appender.ROLLING_FILE.File=rolling.log<br /> log4j.appender.ROLLING_FILE.Append=true<br /> log4j.appender.ROLLING_FILE.MaxFileSize=100KB<br /> log4j.appender.ROLLING_FILE.MaxBackupIndex=10<br /> log4j.appender.ROLLING_FILE.layout=org.apache.log4j.PatternLayout<br /> log4j.appender.ROLLING_FILE.layout.ConversionPattern=[framework] %d - %-4r [%t] %-5p %c %x - %m%n</p> <p><br /> #搴旂敤浜巗ocket<br /> log4j.appender.SOCKET=org.apache.log4j.net.SocketAppender<br /> log4j.appender.SOCKET.RemoteHost=localhost<br /> log4j.appender.SOCKET.Port=5001<br /> log4j.appender.SOCKET.LocationInfo=true<br /> # Set up for Log Facter 5<br /> log4j.appender.SOCKET.layout=org.apache.log4j.PatternLayout<br /> log4j.appender.SOCET.layout.ConversionPattern=[start]%d{DATE}[DATE]%n%p[PRIORITY]%n%x[NDC]%n%t[THREAD]%n%c[CATEGORY]%n%m[MESSAGE]%n%n</p> <p><br /> # Log Factor 5 Appender<br /> log4j.appender.LF5_APPENDER=org.apache.log4j.lf5.LF5Appender<br /> log4j.appender.LF5_APPENDER.MaxNumberOfRecords=2000</p> <p># 鍙戦佹棩蹇楃粰閭歡<br /> log4j.appender.MAIL=org.apache.log4j.net.SMTPAppender<br /> log4j.appender.MAIL.Threshold=FATAL<br /> log4j.appender.MAIL.BufferSize=10<br /> log4j.appender.MAIL.From=web@www.wuset.com<br /> log4j.appender.MAIL.SMTPHost=www.wusetu.com<br /> log4j.appender.MAIL.Subject=Log4J Message<br /> log4j.appender.MAIL.To=web@www.wusetu.com<br /> log4j.appender.MAIL.layout=org.apache.log4j.PatternLayout<br /> log4j.appender.MAIL.layout.ConversionPattern=[framework] %d - %-4r [%t] %-5p %c %x - %m%n</p> <p># 鐢ㄤ簬鏁版嵁搴?br /> log4j.appender.DATABASE=org.apache.log4j.jdbc.JDBCAppender<br /> log4j.appender.DATABASE.URL=jdbc:mysql://localhost:3306/test<br /> log4j.appender.DATABASE.driver=com.mysql.jdbc.Driver<br /> log4j.appender.DATABASE.user=root<br /> log4j.appender.DATABASE.password=<br /> log4j.appender.DATABASE.sql=INSERT INTO LOG4J (Message) VALUES ('[framework] %d - %-4r [%t] %-5p %c %x - %m%n')<br /> log4j.appender.DATABASE.layout=org.apache.log4j.PatternLayout<br /> log4j.appender.DATABASE.layout.ConversionPattern=[framework] %d - %-4r [%t] %-5p %c %x - %m%n</p> <p>#姣忔棩鍥炴粴鏃ュ織鏂囦歡<br /> log4j.appender.A1=org.apache.log4j.DailyRollingFileAppender<br /> log4j.appender.A1.File=SampleMessages.log4j<br /> log4j.appender.A1.DatePattern=yyyyMMdd-HH'.log4j'<br /> log4j.appender.A1.layout=org.apache.log4j.xml.XMLLayout</p> <p>#鑷畾涔堿ppender<br /> log4j.appender.im = net.cybercorlin.util.logger.appender.IMAppender<br /> log4j.appender.im.host = mail.cybercorlin.net<br /> log4j.appender.im.username = username<br /> log4j.appender.im.password = password<br /> log4j.appender.im.recipient = corlin@cybercorlin.net<br /> log4j.appender.im.layout=org.apache.log4j.PatternLayout<br /> log4j.appender.im.layout.ConversionPattern =[framework] %d - %-4r [%t] %-5p %c %x - %m%n</p> <br /> <br /> <img src ="http://www.tkk7.com/honzeland/aggbug/133434.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/honzeland/" target="_blank">honzeland</a> 2007-07-30 21:13 <a href="http://www.tkk7.com/honzeland/articles/133434.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>Mastering the Java CLASSPATH http://www.tkk7.com/honzeland/articles/96954.htmlhonzelandhonzelandWed, 31 Jan 2007 06:32:00 GMThttp://www.tkk7.com/honzeland/articles/96954.htmlhttp://www.tkk7.com/honzeland/comments/96954.htmlhttp://www.tkk7.com/honzeland/articles/96954.html#Feedback0http://www.tkk7.com/honzeland/comments/commentRss/96954.htmlhttp://www.tkk7.com/honzeland/services/trackbacks/96954.html from http://www.kevinboone.com/classpath.html

The significance of the class search path

An understanding of the class search path is important for all Java developers. However, the widespread use of integrated development tools has concealed the technicalities for so long that there is a widespread lack of comprehension, even among experienced developers. The problem is particularly acute with development of distributed applications, as the system which will run the application is likely to be configured rather differently from the one on which development takes place.

This article describes in detail how the Java compiler and the JVM use the class search path to locate classes when they are referenced by other Java code. It does this with reference to a very simple example, which uses two classes in the same package. We will see how various operations to compile these two classes succeed and fail, depending on the class path setting.

To make things absolutely clear, we will use only simple command-line tools to carry out the compile operations. Interactive development tools have their own ways of manipulating the class path, which vary from product to product.

There is no fundamental difference between the way that the Java compiler searches for classes, and the way that the JVM does it at run time. However, the compiler has the ability to compile classes from source code, where the JVM does not. In the examples below we will use the compiler, but similar issues apply at run time.

The example

This example has two trivial classes: com.web_tomorrow.CPTest1 and com.web_tomorrow.CPTest2, which are listed below.
package com.web_tomorrow;
public class CPTest1
{
public static void main(String[] args)
  {
  System.out.println ("Run CPTest1.main()");
  }
}

package com.web_tomorrow;
public class CPTest2
{
public static void main(String[] args)
  {
  System.out.println ("Run CPTest2.main()");
  CPTest1 cpt1 = new CPTest1();
  }
}
One of the most fundamental rules of Java code organization is that `package name = directory name'. We will begin by setting up a directory structure that matches the package assignment of these two classes. The classes are in a package com.web_tomorrow, so we must create the directory com/web_tomorrow to contain the source code.
[root]
  com
    web_tomorrow
      CPTest1.java
      CPTest2.java
In this document I will use the notation `[root]' to mean `whatever directory contains the structure described above', that is, the root of the directory layout. This will vary, of course, according to how you install the files.

Basic principles

Let's try to compile CPTest1.java on its own using the command-line javac program. To disable the class search path completely (so any existing setting does not interfere with the example), we can run javac with the option `-classpath ""'.

As a first attempt, let's change directory to the location of CPTest1.java, and try to compile it by specifying its name on the javac command line.

cd [root]/com/web_tomorrow
javac -classpath "" CPTest1.java
This operation succeeds, because the compiler is able to find CPTest1.java (it is in the working directory), and because CPTest1 does not reference any other classes. The output file, CPTest1.class ends up in the same directory as CPTest1.java because, again, you haven't given the compiler information to do anything else. So far so good. Now let's try the same thing with CPTest2. Still in the `web_tomorrow' directory, execute this command:
javac -classpath "" CPTest2.java
This operation should fail, even though the directory is the same as the previous step, and CPTest1 and CPTest2 are in the same package. The error message will be something like this:
PTest2.java:7: cannot resolve symbol
symbol  : class CPTest1  
location: class com.web_tomorrow.CPTest2
  CPTest1 cpt1 = new CPTest1();
  ^
The difference between this case and the previous, successful, one is that CPTest2 contains a reference to CPTest1:
  CPTest1 cpt1 = new CPTest1();
What is going on here? When the compiler encounters the reference to CP1Test here, it assumes that this is a class in the same package as CP2Test that is is currently compiling. This is a correct assumption. So the compiler needs to find com.web_tomorrow.CP1Test. But it has nowhere to look, as we have explicitly set the class search path to "" (i.e., nothing).

You might think this problem can be resolved by telling the compiler to look in the current directory. The standard symbol for `current directory' is a single period (.) in both Unix and Windows systems. So try something like this:

javac -classpath "." CPTest2.java
This fails in exactly the same way as the previous example. The problem now is that although CPTest1.java is in the current directory, the class that it implements is not just CPTest1, but com.web_tomorrow.CPTest1. The compiler will look for a directory com/web_tomorrowbelow the current directory. So, overall, it is looking for a Java source or class file in the directory [home]/com/web_tomorrow/com/web_tomorrow which, of course, does not exist.

To make this compile operation work, we need to make the class search path reference not the directory containing CPTest1, but a directory root from which CPTest1 can be located by the compiler following the standard Java `package name = directory name' rule. This should work, although it's rather ugly:

javac -classpath "../.." CPTest2.java
Before seeing how we can make this less ugly, consider this example (still in the same directory):
javac -classpath "" CPTest1.java CPTest2.java
This also works, even though the class path is empty. This is because the Java compiler will look for references between any source code explicitly listed on the command line. If there are many classes, all in the same directory, we can simplify this to:
javac -classpath "" *.java 
The `*.java' expands to a list of all the .java files in the current directory. This explains why compiling many files in one operation often succeeds where attempts to compile a single file fails.

A more convenient way to compile CPTest2 on its own is like this:

cd [root]
javac -classpath "." com/web_tomorrow/CPTest2.java
In this example we specify the full path to CPTest2.java, but include `.' in the -classpath option. Again, we aren't telling the compiler to look for files in the current directory, we are telling it to begin a class search from the current directory. Because the class we are looking for is com.web_tomorrow.CPTest1, the compiler will search in ./com/web_tomorrow (that is, the directory com/web_tomorrow below the current directory). This is exactly where CPTest1.java is located.

In fact, even though I only specified CPTest2 on the command line, this practice does in fact lead to the compilation of CPTest1 as well. The compiler finds the .java file in the right place, but it can't tell whether this Java source really implements the right class, so it has to compile it. But note that if we do this:

cd [root]
javac -classpath "." com/web_tomorrow/CPTest1.java
it does not cause a compilation of CPTest2.java, because the compiler does not need to know anything about CPTest2 to compile CPTest1.

.class files separate from .java files

The examples described so far, when successful, place the output .class files alongside the .java files from which they were generated. This is a simple scheme, and very widely used. However, many developers like to keep the source tree free of generated files, and must therefore tell the Java compiler to maintain separate directories for .class files. Let's see what impact this has on the class search path.

To begin we will need to delete any .class files lurking around after the previous examples. We will also contain a new directory classes to contain the generated .class files. The procedure at the command line would be something like this:

cd [root]
rm com/web_tomorrow/*.class
mkdir classes
Don't forget to swap the `/' characters for '\' if you are using a Windows system. The directory structure now looks like this.
[root]
  com
    web_tomorrow
      CPTest1.java
      CPTest2.java
  classes
Let's compile CPTest1.java, specifying classes as the destination directory (using the -d option):
cd [root]
javac -d classes -classpath "" com/web_tomorrow/CPTest1.java 
This should succeed, but you should notice that the .class files have not been placed into the classes directory at all. Instead, we have a new directory structure like this:
[root]
  com
    web_tomorrow
      CPTest1.java
      CPTest2.java
  classes
    com
      web_tomorrow
        CPTest1.class
What has happened is that the compiler has created a directory structure to match the package structure. It has done this to be helpful, as we shall see. When we come to compile CPTest2.java we have two choices. First, we can compile it as described above, allowing the compiler to compile CPTest1 as part of the process. Alternatively, we can compile it and use the -classpath option to refer to the compiler to the .class file generated in the previous step. This method is superior, as we don't have to repeat the compilation of CPTest1.
cd [root]
javac -d classes -classpath classes com/web_tomorrow/CPTest2.java 
By doing this, we end up with this directory structure.
[root]
  com
    web_tomorrow
      CPTest1.java
      CPTest2.java
  classes
    com
      web_tomorrow
      CPTest1.class
      CPTest2.class
Of course we could have compiled both .java files in the same command, and got the same result.

JARs on the classpath

The java compiler and run-time can search for classes not only in separate files, but also in `JAR' archives. A JAR file can maintain its own directory structure, and Java follows exactly the same rules as for searching in ordinary directories. Specifically, `directory name = package name'. Because a JAR is itself a directory, to include a JAR file in the class search path, the path must reference the JAR itself, not merely the directory that contains the JAR. This is a very common error. Suppose I have a JAR myclasses.jar in directory /myclasses. To have the Java compiler look for classes in this jar, we need to specify:
javac -classpath /myclasses/myclasses.jar ...
and not merely the directory myclasses.

Multiple class search directories

In the examples above, we have told javac to search in only one directory at a time. In practice, your class search path will contain numerous directories and JAR archives. The -classpath option to javac and java allows multiple entries to be specified, but notice that the syntax is slightly different for Unix and Windows systems.

On Unix, we would do this:

javac -classpath dir1:dir2:dir3 ...
whereas on Windows we have:
javac -classpath dir1;dir2;dir3 ...
The reason for the difference is that Windows uses the colon (:) character as part of a filename, so it can't be used as a filename separator. Naturally the directory separator character is different as well: forward slash (/) for Unix and backslash (\) for Windows.

System classpath

Rather than specifying class search path on the javac command line, we can make use of a `system' class path. This is the class path that will be used by both the Java compiler and the JVM in the absence of specific instructions to the contrary. In both Unix and Windows systems, this is done by setting an environment variable. For example, in Linux with the bash shell:
CLASSPATH=/myclasses/myclasses.jar;export CLASSPATH 
and in Windows:
set CLASSPATH=c:\myclasses\myclasses.jar
This procedure is fine for short-term changes to the system CLASSPATH, but if you want these changes to be persistent you will need to arrange this yourself. Details vary from system to system. On a Linux system, for example, I would put the commands in the file .bashrc in my home directory. On Windows 2000/NT there is a `Control Panel' page for this.

Setting the system CLASSPATH is a useful procedure if you have JARs full of classes that you use all the time. For example, if I am developing Enterprise JavaBean (EJB) applications using Sun's J2EE `Reference Implementation', all the EJB-related classes are in a JAR called `j2ee.jar' that comes with the distribution. I want this JAR on the class search path all the time. In addition, most people want to ensure that the current directory is on the search path, whatever the current directory happens to be. So in my .bashrc file I have this line:

CLASSPATH=/usr/j2ee/j2ee.jar:.;export CLASSPATH 
where the `.' indicates `current directory'.

It is easy to overlook that the -classpath option on the command line replaces the default, system class path; it does not add to it. So what should I do if I want to set the class path to include the default system classpath plus some other entries? I could simply use the -classpath option and list the default entries in addition to my extras. However, a better way is to reference the CLASSPATH environment variable. The syntax for this is different -- of course -- on Windows and Unix systems. On Unix:

javac -classpath $CLASSPATH:dir1:dir2 ...
where $CLASSPATH expands to the current setting of the CLASSPATH environment variable. On Windows:
javac -classpath %CLASSPATH%;dir1:dir2 ...
Finally, please note that if directories in your class search path have spaces in their names, you may have to use double-quotes on the command line to prevent the CLASSPATH being split up. For example:
javac -classpath "%CLASSPATH%";dir1:dir2 ...


//
//the article ended! the following is got from Sun Microsystem Document.






Setting the class path

Synopsis

The class path is the path that the Java runtime environment searches for classes and other resource files. The class search path (more commonly known by the shorter name, "class path") can be set using either the -classpath option when calling a JDK tool (the preferred method) or by setting the CLASSPATH environment variable. The -classpath option is preferred because you can set it individually for each application without affecting other applications and without other applications modifying its value.

C:>sdkTool-classpathclasspath1;classpath2...

-or-

C:> set CLASSPATH=classpath1;classpath2...

where:

sdkTool
A command-line tool, such as java, javac, javadoc, or apt. For a listing, see JDK Tools.
classpath1;classpath2
Class paths to the .jar, .zip or .class files. Each classpath should end with a filename or directory depending on what you are setting the class path to:
  • For a .jar or .zip file that contains .class files, the class path ends with the name of the .zip or .jar file.
  • For .class files in an unnamed package, the class path ends with the directory that contains the .class files.
  • For .class files in a named package, the class path ends with the directory that contains the "root" package (the first package in the full package name).

Multiple path entries are separated by semi-colons. With the set command, it's important to omit spaces from around the equals sign (=).

The default class path is the current directory. Setting the CLASSPATH variable or using the -classpath command-line option overrides that default, so if you want to include the current directory in the search path, you must include "." in the new settings.

Classpath entries that are neither directories nor archives (.zip or .jar files) nor * are ignored.

Description

The class path tells JDK tools and applications where to find third-party and user-defined classes -- that is, classes that are not Java extensions or part of the Java platform. The class path needs to find any classes you've compiled with the javac compiler -- its default is the current directory to conveniently enable those classes to be found.

The JDK, the JVM and other JDK tools find classes by searching the Java platform (bootstrap) classes, any extension classes, and the class path, in that order. (For details on the search strategy, see How Classes Are Found.) Class libraries for most applications will want to take advantage of the extensions mechanism. You only need to set the class path when you want to load a class that's (a) not in the current directory or in any of its subdirectories, and (b) not in a location specified by the extensions mechanism.

If you are upgrading from an older version of the JDK, your startup settings may include CLASSPATH settings that are no longer needed. You should remove any settings that are not application-specific, such as classes.zip. Some third-party applications that use the Java Virtual Machine may modify your CLASSPATH environment variable to include the libaries they use. Such settings can remain.

You can change the class path by using the JDK tools' -classpath option when you invoke the JVM or other JDK tools or by using the CLASSPATH environment variable. Using the -classpath option is preferred over setting CLASSPATH environment variable because you can set it individually for each application without affecting other applications and without other applications modifying its value.

Classes can be stored either in directories (folders) or in archive files. The Java platform classes are stored in rt.jar. For more details on archives and information on how the class path works, see Understanding the class path and package names near the end of this document.

Important Note: Some older versions of the JDK sofware included a <jdk-dir>/classes entry in the default class path. That directory exists for use by the JDK software, and should not be used for application classes. Application classes should be placed in a directory outside of the JDK directory hierarcy. That way, installing a new JDK does not force you to reinstall application classes. For compatibility with older versions, applications that use the <jdk-dir>/classes directory as a class library will run in the current version, but there is no guarantee that they will run in future versions.

Using the JDK tools' -classpath option

The JDK tools java, jdb, javac, and javah have a -classpath option which replaces the path or paths specified by the CLASSPATH environment variable while the tool runs. This is the recommended option for changing class path settings, because each application can have the class path it needs without interfering with any other application.

The runtime tool java has a -cp option, as well. This option is an abbreviation for -classpath.

For very special cases, both java and javac have options that let you change the path they use to find their own class libraries. The vast majority of users will never to need to use those options, however.

Using the CLASSPATH environment variable

In general, you will want to use the -classpath command-line option, as explained in the previous section. This section shows you how to set the CLASSPATH environment variable if you want to do that, or clear settings left over from a previous installation.

Setting CLASSPATH

The CLASSPATH environment variable is modified with the set command. The format is:

set CLASSPATH=path1;path2 ...

The paths should begin with the letter specifying the drive, for example, C:\. That way, the classes will still be found if you happen to switch to a different drive. (If the path entries start with backslash (\) and you are on drive D:, for example, then the classes will be expected on D:, rather thanC:.)

Clearing CLASSPATH

If your CLASSPATH environment variable has been set to a value that is not correct, or if your startup file or script is setting an incorrect path, you can unset CLASSPATH by using:

C:> set CLASSPATH=

This command unsets CLASSPATH for the current command prompt window only. You should also delete or modify your startup settings to ensure that you have the right CLASSPATH settings in future sessions.

Changing Startup Settings

If the CLASSPATH variable is set at system startup, the place to look for it depends on your operating system:
Operating SystemMethod
Windows 95 and 98Examine autoexec.bat for the set command.
Other (Windows NT, Windows 2000, ...)The CLASSPATH environment variable can be set using the System utility in the Control Panel.

Understanding class path wildcards

Class path entries can contain the basename wildcard character *, which is considered equivalent to specifying a list of all the files in the directory with the extension .jar or .JAR. For example, the class path entry foo/* specifies all JAR files in the directory named foo. A classpath entry consisting simply of * expands to a list of all the jar files in the current directory.

A class path entry that contains * will not match class files. To match both classes and JAR files in a single directory foo, use either foo;foo/* or foo/*;foo. The order chosen determines whether the classes and resources in foo are loaded before JAR files in foo, or vice versa.

Subdirectories are not searched recursively. For example, foo/* looks for JAR files only in foo, not in foo/bar, foo/baz, etc.

The order in which the JAR files in a directory are enumerated in the expanded class path is not specified and may vary from platform to platform and even from moment to moment on the same machine. A well-constructed application should not depend upon any particular order. If a specific order is required then the JAR files can be enumerated explicitly in the class path.

Expansion of wildcards is done early, prior to the invocation of a program's main method, rather than late, during the class-loading process itself. Each element of the input class path containing a wildcard is replaced by the (possibly empty) sequence of elements generated by enumerating the JAR files in the named directory. For example, if the directory foo contains a.jar, b.jar, and c.jar, then the class path foo/* is expanded into foo/a.jar;foo/b.jar;foo/c.jar, and that string would be the value of the system property java.class.path.

The CLASSPATH environment variable is not treated any differently from the -classpath (or -cp) command-line option. That is, wildcards are honored in all these cases. However, class path wildcards are not honored in the Class-Path jar-manifest header.

Understanding the class path and package names

Java classes are organized into packages which are mapped to directories in the file system. But, unlike the file system, whenever you specify a package name, you specify the whole package name -- never part of it. For example, the package name for java.awt.Button is always specified as java.awt.

For example, suppose you want the Java runtime to find a class named Cool.class in the package utility.myapp. If the path to that directory is C:\java\MyClasses\utility\myapp, you would set the class path so that it contains C:\java\MyClasses.

To run that app, you could use the following JVM command:

C:> java -classpath C:\java\MyClasses utility.myapp.Cool

When the app runs, the JVM uses the class path settings to find any other classes defined in the utility.myapp package that are used by the Cool class.

Note that the entire package name is specified in the command. It is not possible, for example, to set the class path so it contains C:\java\MyClasses\utility and use the command java myapp.Cool. The class would not be found.

(You may be wondering what defines the package name for a class. The answer is that the package name is part of the class and cannot be modified, except by recompiling the class.)

Note: An interesting consequence of the package specification mechanism is that files which are part of the same package may actually exist in different directories. The package name will be the same for each class, but the path to each file may start from a different directory in the class path.

Folders and archive files

When classes are stored in a directory (folder), like c:\java\MyClasses\utility\myapp, then the class path entry points to the directory that contains the first element of the package name. (in this case, C:\java\MyClasses, since the package name is utility.myapp.)

But when classes are stored in an archive file (a .zip or .jar file) the class path entry is the path to and including the .zip or .jar file. For example, to use a class library that is in a .jar file, the command would look something like this:

C:> java -classpath C:\java\MyClasses\myclasses.jar utility.myapp.Cool

Multiple specifications

To find class files in the directory C:\java\MyClasses as well as classes in C:\java\OtherClasses, you would set the class path to:

C:> java -classpath C:\java\MyClasses;C:\java\OtherClasses ...

Note that the two paths are separated by a semicolon.

Specification order

The order in which you specify multiple class path entries is important. The Java interpreter will look for classes in the directories in the order they appear in the class path variable. In the example above, the Java interpreter will first look for a needed class in the directory C:\java\MyClasses. Only if it doesn't find a class with the proper name in that directory will the interpreter look in the C:\java\OtherClasses directory.


Copyright 漏 2004-2006 Sun Microsystems, Inc. All Rights Reserved.

Sun
Java Software





honzeland 2007-01-31 14:32 鍙戣〃璇勮
]]>
Java/J2EE涓枃闂緇堟瀬瑙e喅涔嬮亾 http://www.tkk7.com/honzeland/articles/82284.htmlhonzelandhonzelandMon, 20 Nov 2006 07:31:00 GMThttp://www.tkk7.com/honzeland/articles/82284.htmlhttp://www.tkk7.com/honzeland/comments/82284.htmlhttp://www.tkk7.com/honzeland/articles/82284.html#Feedback0http://www.tkk7.com/honzeland/comments/commentRss/82284.htmlhttp://www.tkk7.com/honzeland/services/trackbacks/82284.html 鏉挎ˉ閲屼漢 http://www.jdon.com 2005/06/29

銆銆Java涓枃闂涓鐩村洶鎵扮潃寰堝鍒濆鑰咃紝濡傛灉浜嗚В浜咼ava緋葷粺鐨勪腑鏂囬棶棰樺師鐞嗭紝鎴戜滑灝卞彲浠ュ涓枃闂鑳藉閲囧彇鏍規湰鐨勮В鍐充箣閬撱?/p>

銆銆鏈鍙よ佺殑瑙e喅鏂規鏄嬌鐢⊿tring鐨勫瓧鑺傜爜杞崲錛岃繖縐嶆柟妗堥棶棰樻槸涓嶆柟渚匡紝鎴戜滑闇瑕佺牬鍧忓璞″皝瑁呮э紝榪涜瀛楄妭鐮佽漿鎹€?/p>

銆銆榪樻湁涓縐嶆柟寮忔槸瀵笿2EE瀹瑰櫒榪涜緙栫爜璁劇疆錛屽鏋淛2EE搴旂敤緋葷粺鑴辯璇ュ鍣紝鍒欎細鍙戠敓涔辯爜錛岃屼笖鎸囧畾瀹瑰櫒閰嶇疆涓嶇鍚圝2EE搴旂敤鍜屽鍣ㄥ垎紱葷殑鍘熷垯銆?/p>

銆銆鍦↗ava鍐呴儴榪愮畻涓紝娑夊強鍒扮殑鎵鏈夊瓧絎︿覆閮戒細琚漿鍖栦負UTF-8緙栫爜鏉ヨ繘琛岃繍綆椼傞偅涔堬紝鍦ㄨJava杞寲涔嬪墠錛屽瓧絎︿覆鏄粈涔堟牱鐨勫瓧絎﹂泦錛?Java鎬繪槸鏍規嵁鎿嶄綔緋葷粺鐨勯粯璁ょ紪鐮佸瓧絎﹂泦鏉ュ喅瀹氬瓧絎︿覆鐨勫垵濮嬬紪鐮侊紝鑰屼笖Java緋葷粺鐨勮緭鍏ュ拰杈撳嚭鐨勯兘鏄噰鍙栨搷浣滅郴緇熺殑榛樿緙栫爜銆?/p>

銆銆鍥犳錛屽鏋滆兘緇熶竴Java緋葷粺鐨勮緭鍏ャ佽緭鍑哄拰鎿嶄綔緋葷粺3鑰呯殑緙栫爜瀛楃闆嗗悎錛屽皢鑳藉浣縅ava緋葷粺姝g‘澶勭悊鍜屾樉紺烘眽瀛椼傝繖鏄鐞咼ava緋葷粺姹夊瓧鐨勪竴涓師鍒欙紝浣嗘槸鍦ㄥ疄闄呴」鐩腑錛岃兘澶熸紜姄浣忓拰鎺у埗浣廕ava緋葷粺鐨勮緭鍏ュ拰杈撳嚭閮ㄥ垎鏄瘮杈冮毦鐨勩侸2EE涓紝鐢變簬娑夊強鍒板閮ㄦ祻瑙堝櫒鍜屾暟鎹簱絳夛紝鎵浠ヤ腑鏂囬棶棰樹貢鐮佹樉寰楅潪甯哥獊鍑恒?/p>

銆銆J2EE搴旂敤紼嬪簭鏄繍琛屽湪J2EE瀹瑰櫒涓傚湪榪欎釜緋葷粺涓紝杈撳叆閫斿緞鏈夊緢澶氱錛氫竴縐嶆槸閫氳繃欏甸潰琛ㄥ崟鎵撳寘鎴愯姹傦紙request錛夊彂寰鏈嶅姟鍣ㄧ殑錛涚浜岀鏄氳繃鏁版嵁搴撹鍏ワ紱榪樻湁絎?縐嶈緭鍏ユ瘮杈冨鏉傦紝JSP鍦ㄧ涓嬈¤繍琛屾椂鎬繪槸琚紪璇戞垚Servlet錛孞SP涓父甯稿寘鍚腑鏂囧瓧絎︼紝閭d箞緙栬瘧浣跨敤javac鏃訛紝Java灝嗘牴鎹粯璁ょ殑鎿嶄綔緋葷粺緙栫爜浣滀負鍒濆緙栫爜銆傞櫎闈炵壒鍒寚瀹氾紝濡傚湪Jbuilder/eclipse涓彲浠ユ寚瀹氶粯璁ょ殑瀛楃闆嗐?/p>

銆銆杈撳嚭閫斿緞涔熸湁鍑犵錛氱涓縐嶆槸JSP欏甸潰鐨勮緭鍑恒傜敱浜嶫SP欏甸潰宸茬粡琚紪璇戞垚Servlet錛岄偅涔堝湪杈撳嚭鏃訛紝涔熷皢鏍規嵁鎿嶄綔緋葷粺鐨勯粯璁ょ紪鐮佹潵閫夋嫨杈撳嚭緙栫爜錛岄櫎闈炴寚瀹氳緭鍑虹紪鐮佹柟寮忥紱榪樻湁杈撳嚭閫斿緞鏄暟鎹簱錛屽皢瀛楃涓茶緭鍑哄埌鏁版嵁搴撱?/p>

銆銆鐢辨鐪嬫潵錛屼竴涓狫2EE緋葷粺鐨勮緭鍏ヨ緭鍑烘槸闈炲父澶嶆潅錛岃屼笖鏄姩鎬佸彉鍖栫殑錛岃孞ava鏄法騫沖彴榪愯鐨勶紝鍦ㄥ疄闄呯紪璇戝拰榪愯涓紝閮藉彲鑳芥秹鍙婂埌涓嶅悓鐨勬搷浣滅郴緇燂紝濡傛灉浠葷敱Java鑷敱鏍規嵁鎿嶄綔緋葷粺鏉ュ喅瀹氳緭鍏ヨ緭鍑虹殑緙栫爜瀛楃闆嗭紝榪欏皢涓嶅彲鎺у埗鍦板嚭鐜頒貢鐮併?/p>

銆銆姝f槸鐢變簬Java鐨勮法騫沖彴鐗規э紝浣垮緱瀛楃闆嗛棶棰樺繀欏葷敱鍏蜂綋緋葷粺鏉ョ粺涓瑙e喅錛屾墍浠ュ湪涓涓狫ava搴旂敤緋葷粺涓紝瑙e喅涓枃涔辯爜鐨勬牴鏈姙娉曟槸鏄庣‘鎸囧畾鏁翠釜搴旂敤緋葷粺緇熶竴瀛楃闆嗐?/strong>

銆銆鎸囧畾緇熶竴瀛楃闆嗘椂錛屽埌搴曟槸鎸囧畾ISO8859_1 銆丟BK榪樻槸UTF-8鍛紵

銆銆錛?錛夊緇熶竴鎸囧畾涓篒SO8859_1錛屽洜涓虹洰鍓嶅ぇ澶氭暟杞歡閮芥槸瑗挎柟浜虹紪鍒剁殑錛屼粬浠粯璁ょ殑瀛楃闆嗗氨鏄疘SO8859_1錛屽寘鎷搷浣滅郴緇烲inux鍜屾暟鎹簱MySQL絳夈傝繖鏍鳳紝濡傛灉鎸囧畾Jive緇熶竴緙栫爜涓篒SO8859_1錛岄偅涔堝氨鏈変笅闈?涓幆鑺傚繀欏繪妸鎻★細

銆銆寮鍙戝拰緙栬瘧浠g爜鏃舵寚瀹氬瓧絎﹂泦涓篒SO8859_1銆?/p>

銆銆榪愯鎿嶄綔緋葷粺鐨勯粯璁ょ紪鐮佸繀欏繪槸ISO8859_1錛屽Linux銆?/p>

銆銆鍦↗SP澶撮儴澹版槑錛?lt;%@ page contentType="text/html;charset=ISO8859_1" %>銆?/p>

銆銆錛?錛夊鏋滅粺涓鎸囧畾涓篏BK涓枃瀛楃闆嗭紝涓婅堪3涓幆鑺傚悓鏍烽渶瑕佸仛鍒幫紝涓嶅悓鐨勬槸鍙兘榪愯鍦ㄩ粯璁ょ紪鐮佷負GBK鐨勬搷浣滅郴緇燂紝濡備腑鏂嘩indows銆?/p>

銆銆緇熶竴緙栫爜涓篒SO8859_1鍜孏BK铏界劧甯︽潵緙栧埗浠g爜鐨勬柟渚匡紝浣嗘槸鍚勮嚜鍙兘鍦ㄧ浉搴旂殑鎿嶄綔緋葷粺涓婅繍琛屻備絾鏄篃鐮村潖浜咼ava璺ㄥ鉤鍙拌繍琛岀殑浼樿秺鎬э紝鍙湪涓瀹氳寖鍥村唴琛屽緱閫氥備緥濡傦紝涓轟簡浣垮緱GBK緙栫爜鍦╨inux涓婅繍琛岋紝璁劇疆Linux緙栫爜涓篏BK銆?/p>

銆銆閭d箞鏈夋病鏈変竴縐嶉櫎浜嗗簲鐢ㄧ郴緇熶互澶栦笉闇瑕佽繘琛屼換浣曢檮鍔犺緗殑涓枃緙栫爜鏍規湰瑙e喅鏂規鍛紵

銆銆灝咼ava/J2EE緋葷粺鐨勭粺涓緙栫爜瀹氫箟涓篣TF-8銆俇TF-8緙栫爜鏄竴縐嶅吋瀹規墍鏈夎璦鐨勭紪鐮佹柟寮忥紝鎯熶竴姣旇緝楹葷儲鐨勫氨鏄鎵懼埌搴旂敤緋葷粺鐨勬墍鏈夊嚭鍏ュ彛錛岀劧鍚庝嬌鐢║TF-8鍘燴滅粨鎵庘濆畠銆?/p>

銆銆涓涓狫2EE搴旂敤緋葷粺闇瑕佸仛涓嬪垪鍑犳宸ヤ綔錛?/p>

  1. 寮鍙戝拰緙栬瘧浠g爜鏃舵寚瀹氬瓧絎﹂泦涓篣TF-8銆侸Builder鍜孍clipse閮藉彲浠ュ湪欏圭洰灞炴т腑璁劇疆銆?
  2. 浣跨敤榪囨護鍣紝濡傛灉鎵鏈夎姹傞兘緇忚繃涓涓猄ervlet鎺у埗鍒嗛厤鍣紝閭d箞浣跨敤Servlet鐨刦ilter鎵ц璇彞錛屽皢鎵鏈夋潵鑷祻瑙堝櫒鐨勮姹傦紙request錛夎漿鎹負UTF-8錛屽洜涓烘祻瑙堝櫒鍙戣繃鏉ョ殑璇鋒眰鍖呮牴鎹祻瑙堝櫒鎵鍦ㄧ殑鎿嶄綔緋葷粺緙栫爜錛屽彲鑳芥槸鍚勭褰㈠紡緙栫爜銆傚叧閿竴鍙ワ細
    request.setCharacterEncoding("UTF-8")銆?br />緗戜笂鏈夋filter鐨勬簮鐮侊紝Jdon妗嗘灦婧愮爜涓璫om.jdon.util.SetCharacterEncodingFilter
    闇瑕侀厤緗畐eb.xml 嬋媧昏Filter銆?
  3. 鍦↗SP澶撮儴澹版槑錛?lt;%@ page contentType="text/html;charset= UTF-8" %>銆?
  4. 鍦↗sp鐨刪tml浠g爜涓紝澹版槑UTF-8:
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5. 璁懼畾鏁版嵁搴撹繛鎺ユ柟寮忔槸UTF-8銆備緥濡傝繛鎺YSQL鏃墮厤緗甎RL濡備笅錛?br />jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=UTF-8
    娉ㄦ剰錛屼笂榪板啓娉曟槸JBoss鐨刴ysql-ds.xml鍐欐硶錛屽浜忕綉鍙嬫彁紺猴紝鍦╰omcat涓?amp;amp;瑕佸啓鎴?amp;鍗沖彲銆備竴鑸叾浠栨暟鎹簱閮藉彲浠ラ氳繃綆$悊璁劇疆璁懼畾UTF-8
  6. 鍏朵粬鍜屽鐣屼氦浜掓椂鑳藉璁懼畾緙栫爜鏃跺氨璁懼畾UTF-8錛屼緥濡傝鍙栨枃浠訛紝鎿嶄綔XML絳夈?
銆銆銆銆 絎旇呬互鍓嶅湪Jsp/Servlet鏃跺氨閲囧彇榪欎釜鍘熷垯錛屽悗鏉ヤ嬌鐢⊿truts銆乀apestry銆丒JB銆丠ibernate銆丣don絳夋鏋舵椂錛屼粠鏈涔辯爜鍥版壈榪囷紝鍙互璇撮傚悎鍚勭鏋舵瀯銆傚笇鏈涙湰鏂規渚涙洿澶氬垵瀛﹁呭垎浜紝鍑忓皯Java/J2EE鐨勭涓涓嫤璺檸錛屼篃閬垮厤鍥犱負閲囧彇涓浜涗復鏃惰В鍐蟲柟妗堬紝瀵艱嚧涓枃闂涓鐩村嚭鐜板湪鏂扮殑鎶鏈灦鏋勪腑銆?

honzeland 2006-11-20 15:31 鍙戣〃璇勮
]]>
String鍜孲tringBuffer涔嬫瑙?http://www.tkk7.com/honzeland/articles/80328.htmlhonzelandhonzelandFri, 10 Nov 2006 02:44:00 GMThttp://www.tkk7.com/honzeland/articles/80328.htmlhttp://www.tkk7.com/honzeland/comments/80328.htmlhttp://www.tkk7.com/honzeland/articles/80328.html#Feedback0http://www.tkk7.com/honzeland/comments/commentRss/80328.htmlhttp://www.tkk7.com/honzeland/services/trackbacks/80328.html闃呰鍏ㄦ枃

honzeland 2006-11-10 10:44 鍙戣〃璇勮
]]>
主站蜘蛛池模板: 亚洲国产专区一区| 亚洲色精品VR一区区三区| 182tv免费观看在线视频| 亚洲日日做天天做日日谢| 波多野结衣一区二区免费视频| 一区二区在线免费视频| 亚洲导航深夜福利| 一本久久综合亚洲鲁鲁五月天| 3344在线看片免费| 亚洲一久久久久久久久| 亚洲精品无码av人在线观看| 国内精品乱码卡1卡2卡3免费| 色屁屁www影院免费观看视频| 91在线亚洲精品专区| 亚洲欧洲精品成人久久奇米网 | 亚洲人成网站色在线观看| 亚洲日本一区二区一本一道| 57pao国产成视频免费播放| 深夜福利在线视频免费| 亚洲另类春色校园小说| 亚洲日韩乱码中文无码蜜桃臀网站 | 日韩精品免费一线在线观看| 亚洲国产成人手机在线电影bd| 精品国产日韩亚洲一区| 一二三四在线播放免费观看中文版视频 | 亚洲乱理伦片在线观看中字| 久久久久久亚洲精品中文字幕| 免费大学生国产在线观看p| 国产免费一区二区三区| 久久九九全国免费| 日韩毛片在线免费观看| 亚洲欧洲精品成人久久曰| 亚洲精品视频在线| 91麻豆精品国产自产在线观看亚洲| 最近高清国语中文在线观看免费 | 亚洲国产精品成人AV无码久久综合影院| 日韩免费精品视频| 50岁老女人的毛片免费观看 | 免费一级大黄特色大片| 一二三四在线播放免费观看中文版视频 | 亚洲国产模特在线播放|