﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-思想者-文章分类-Java technology</title><link>http://www.blogjava.net/Microhelp/category/1814.html</link><description>君子博学而日参省乎己，则智明而行无过也。</description><language>zh-cn</language><lastBuildDate>Wed, 28 Feb 2007 20:48:16 GMT</lastBuildDate><pubDate>Wed, 28 Feb 2007 20:48:16 GMT</pubDate><ttl>60</ttl><item><title>session详解</title><link>http://www.blogjava.net/Microhelp/articles/session.html</link><dc:creator>Microhelp</dc:creator><author>Microhelp</author><pubDate>Thu, 17 Aug 2006 02:33:00 GMT</pubDate><guid>http://www.blogjava.net/Microhelp/articles/session.html</guid><wfw:comment>http://www.blogjava.net/Microhelp/comments/64066.html</wfw:comment><comments>http://www.blogjava.net/Microhelp/articles/session.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/Microhelp/comments/commentRss/64066.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/Microhelp/services/trackbacks/64066.html</trackback:ping><description><![CDATA[转贴来自:<a href="http://blog.tomxp.com/view/467.html">http://blog.tomxp.com/view/467.html</a><br /><br />作者：郎云鹏（dev2dev ID: hippiewolf）<br /><br />摘要：虽然session机制在web应用程序中被采用已经很长时间了，但是仍然有很多人不清楚session机制的本质，以至不能正确的应用这一技术。本文将详细讨论session的工作机制并且对在Java web application中应用session机制时常见的问题作出解答。<br /><br />目录：<br />一、术语session<br />二、HTTP协议与状态保持<br />三、理解cookie机制<br />四、理解session机制<br />五、理解javax.servlet.http.HttpSession<br />六、HttpSession常见问题<br />七、跨应用程序的session共享<br />八、总结<br />参考文档<br /><br />一、术语session<br />在我的经验里，session这个词被滥用的程度大概仅次于transaction，更加有趣的是transaction与session在某些语境下的含义是相同的。<br /><br />session，中文经常翻译为会话，其本来的含义是指有始有终的一系列动作/消息，比如打电话时从拿起电话拨号到挂断电话这中间的一系列过程可以称之为一个 session。有时候我们可以看到这样的话“在一个浏览器会话期间，...”，这里的会话一词用的就是其本义，是指从一个浏览器窗口打开到关闭这个期间 ①。最混乱的是“用户（客户端）在一次会话期间”这样一句话，它可能指用户的一系列动作（一般情况下是同某个具体目的相关的一系列动作，比如从登录到选购商品到结账登出这样一个网上购物的过程，有时候也被称为一个transaction），然而有时候也可能仅仅是指一次连接，也有可能是指含义①，其中的差别只能靠上下文来推断②。<br /><br />然而当session一词与网络协议相关联时，它又往往隐含了“面向连接”和/或“保持状态”这样两个含义， “面向连接”指的是在通信双方在通信之前要先建立一个通信的渠道，比如打电话，直到对方接了电话通信才能开始，与此相对的是写信，在你把信发出去的时候你并不能确认对方的地址是否正确，通信渠道不一定能建立，但对发信人来说，通信已经开始了。“保持状态”则是指通信的一方能够把一系列的消息关联起来，使得消息之间可以互相依赖，比如一个服务员能够认出再次光临的老顾客并且记得上次这个顾客还欠店里一块钱。这一类的例子有“一个TCP session”或者 “一个POP3 session”③。<br /><br />而到了web服务器蓬勃发展的时代，session在web开发语境下的语义又有了新的扩展，它的含义是指一类用来在客户端与服务器之间保持状态的解决方案④。有时候session也用来指这种解决方案的存储结构，如“把xxx保存在session 里”⑤。由于各种用于web开发的语言在一定程度上都提供了对这种解决方案的支持，所以在某种特定语言的语境下，session也被用来指代该语言的解决方案，比如经常把Java里提供的javax.servlet.http.HttpSession简称为session⑥。<br /><br />鉴于这种混乱已不可改变，本文中session一词的运用也会根据上下文有不同的含义，请大家注意分辨。<br />在本文中，使用中文“浏览器会话期间”来表达含义①，使用“session机制”来表达含义④，使用“session”表达含义⑤，使用具体的“HttpSession”来表达含义⑥<br /><br />二、HTTP协议与状态保持<br />HTTP 协议本身是无状态的，这与HTTP协议本来的目的是相符的，客户端只需要简单的向服务器请求下载某些文件，无论是客户端还是服务器都没有必要纪录彼此过去的行为，每一次请求之间都是独立的，好比一个顾客和一个自动售货机或者一个普通的（非会员制）大卖场之间的关系一样。<br /><br />然而聪明（或者贪心？）的人们很快发现如果能够提供一些按需生成的动态信息会使web变得更加有用，就像给有线电视加上点播功能一样。这种需求一方面迫使HTML逐步添加了表单、脚本、DOM等客户端行为，另一方面在服务器端则出现了CGI规范以响应客户端的动态请求，作为传输载体的HTTP协议也添加了文件上载、 cookie这些特性。其中cookie的作用就是为了解决HTTP协议无状态的缺陷所作出的努力。至于后来出现的session机制则是又一种在客户端与服务器之间保持状态的解决方案。<br /><br />让我们用几个例子来描述一下cookie和session机制之间的区别与联系。笔者曾经常去的一家咖啡店有喝5杯咖啡免费赠一杯咖啡的优惠，然而一次性消费5杯咖啡的机会微乎其微，这时就需要某种方式来纪录某位顾客的消费数量。想象一下其实也无外乎下面的几种方案：<br />1、该店的店员很厉害，能记住每位顾客的消费数量，只要顾客一走进咖啡店，店员就知道该怎么对待了。这种做法就是协议本身支持状态。<br />2、发给顾客一张卡片，上面记录着消费的数量，一般还有个有效期限。每次消费时，如果顾客出示这张卡片，则此次消费就会与以前或以后的消费相联系起来。这种做法就是在客户端保持状态。<br />3、发给顾客一张会员卡，除了卡号之外什么信息也不纪录，每次消费时，如果顾客出示该卡片，则店员在店里的纪录本上找到这个卡号对应的纪录添加一些消费信息。这种做法就是在服务器端保持状态。<br /><br />由于HTTP协议是无状态的，而出于种种考虑也不希望使之成为有状态的，因此，后面两种方案就成为现实的选择。具体来说cookie机制采用的是在客户端保持状态的方案，而session机制采用的是在服务器端保持状态的方案。同时我们也看到，由于采用服务器端保持状态的方案在客户端也需要保存一个标识，所以session机制可能需要借助于cookie机制来达到保存标识的目的，但实际上它还有其他选择。<br /><br />三、理解cookie机制 <br />cookie机制的基本原理就如上面的例子一样简单，但是还有几个问题需要解决：“会员卡”如何分发；“会员卡”的内容；以及客户如何使用“会员卡”。<br /><br />正统的cookie分发是通过扩展HTTP协议来实现的，服务器通过在HTTP的响应头中加上一行特殊的指示以提示浏览器按照指示生成相应的cookie。然而纯粹的客户端脚本如JavaScript或者VBScript也可以生成cookie。<br /><br />而cookie 的使用是由浏览器按照一定的原则在后台自动发送给服务器的。浏览器检查所有存储的cookie，如果某个cookie所声明的作用范围大于等于将要请求的资源所在的位置，则把该cookie附在请求资源的HTTP请求头上发送给服务器。意思是麦当劳的会员卡只能在麦当劳的店里出示，如果某家分店还发行了自己的会员卡，那么进这家店的时候除了要出示麦当劳的会员卡，还要出示这家店的会员卡。<br /><br />cookie的内容主要包括：名字，值，过期时间，路径和域。<br />其中域可以指定某一个域比如.google.com，相当于总店招牌，比如宝洁公司，也可以指定一个域下的具体某台机器比如www.google.com或者froogle.google.com，可以用飘柔来做比。<br />路径就是跟在域名后面的URL路径，比如/或者/foo等等，可以用某飘柔专柜做比。<br />路径与域合在一起就构成了cookie的作用范围。<br />如果不设置过期时间，则表示这个cookie的生命期为浏览器会话期间，只要关闭浏览器窗口，cookie就消失了。这种生命期为浏览器会话期的 cookie被称为会话cookie。会话cookie一般不存储在硬盘上而是保存在内存里，当然这种行为并不是规范规定的。如果设置了过期时间，浏览器就会把cookie保存到硬盘上，关闭后再次打开浏览器，这些cookie仍然有效直到超过设定的过期时间。<br /><br />存储在硬盘上的cookie 可以在不同的浏览器进程间共享，比如两个IE窗口。而对于保存在内存里的cookie，不同的浏览器有不同的处理方式。对于IE，在一个打开的窗口上按 Ctrl-N（或者从文件菜单）打开的窗口可以与原窗口共享，而使用其他方式新开的IE进程则不能共享已经打开的窗口的内存cookie；对于 Mozilla Firefox0.8，所有的进程和标签页都可以共享同样的cookie。一般来说是用javascript的window.open打开的窗口会与原窗口共享内存cookie。浏览器对于会话cookie的这种只认cookie不认人的处理方式经常给采用session机制的web应用程序开发者造成很大的困扰。<br /><br />下面就是一个goolge设置cookie的响应头的例子<br />HTTP/1.1 302 Found<br />Location: http://www.google.com/intl/zh-CN/<br />Set-Cookie: PREF=ID=0565f77e132de138:NW=1:TM=1098082649:LM=1098082649:S=KaeaCFPo49RiA_d8; expires=Sun, 17-Jan-2038 19:14:07 GMT; path=/; domain=.google.com<br />Content-Type: text/html<br /><br /><br /><br /><br />这是使用HTTPLook这个HTTP Sniffer软件来俘获的HTTP通讯纪录的一部分<br /><br /><br /><br /><br />浏览器在再次访问goolge的资源时自动向外发送cookie<br /><br /> <br /><br /><br />使用Firefox可以很容易的观察现有的cookie的值<br />使用HTTPLook配合Firefox可以很容易的理解cookie的工作原理。<br /><br /><br /><br /><br />IE也可以设置在接受cookie前询问<br /><br /> <br /><br /><br />这是一个询问接受cookie的对话框。<br /><br />四、理解session机制<br />session机制是一种服务器端的机制，服务器使用一种类似于散列表的结构（也可能就是使用散列表）来保存信息。<br /><br />当程序需要为某个客户端的请求创建一个session的时候，服务器首先检查这个客户端的请求里是否已包含了一个session标识 - 称为 session id，如果已包含一个session id则说明以前已经为此客户端创建过session，服务器就按照session id把这个 session检索出来使用（如果检索不到，可能会新建一个），如果客户端请求不包含session id，则为此客户端创建一个session并且生成一个与此session相关联的session id，session id的值应该是一个既不会重复，又不容易被找到规律以仿造的字符串，这个 session id将被在本次响应中返回给客户端保存。<br /><br />保存这个session id的方式可以采用cookie，这样在交互过程中浏览器可以自动的按照规则把这个标识发挥给服务器。一般这个cookie的名字都是类似于SEEESIONID，而。比如weblogic对于web应用程序生成的cookie，JSESSIONID= ByOK3vjFD75aPnrF7C2HmdnV6QZcEbzWoWiBYEnLerjQ99zWpBng!-145788764，它的名字就是 JSESSIONID。<br /><br />由于cookie可以被人为的禁止，必须有其他机制以便在cookie被禁止时仍然能够把session id传递回服务器。经常被使用的一种技术叫做URL重写，就是把session id直接附加在URL路径的后面，附加方式也有两种，一种是作为URL路径的附加信息，表现形式为http://...../xxx;jsessionid= ByOK3vjFD75aPnrF7C2HmdnV6QZcEbzWoWiBYEnLerjQ99zWpBng!-145788764<br />另一种是作为查询字符串附加在URL后面，表现形式为http://...../xxx?jsessionid=ByOK3vjFD75aPnrF7C2HmdnV6QZcEbzWoWiBYEnLerjQ99zWpBng!-145788764<br />这两种方式对于用户来说是没有区别的，只是服务器在解析的时候处理的方式不同，采用第一种方式也有利于把session id的信息和正常程序参数区分开来。<br />为了在整个交互过程中始终保持状态，就必须在每个客户端可能请求的路径后面都包含这个session id。<br /><br />另一种技术叫做表单隐藏字段。就是服务器会自动修改表单，添加一个隐藏字段，以便在表单提交时能够把session id传递回服务器。比如下面的表单<br />&lt;form name="testform" action="/xxx"&gt;<br />&lt;input type="text"&gt;<br />&lt;/form&gt;<br />在被传递给客户端之前将被改写成<br />&lt;form name="testform" action="/xxx"&gt;<br />&lt;input type="hidden" name="jsessionid" value="ByOK3vjFD75aPnrF7C2HmdnV6QZcEbzWoWiBYEnLerjQ99zWpBng!-145788764"&gt;<br />&lt;input type="text"&gt;<br />&lt;/form&gt;<br />这种技术现在已较少应用，笔者接触过的很古老的iPlanet6(SunONE应用服务器的前身)就使用了这种技术。<br />实际上这种技术可以简单的用对action应用URL重写来代替。<br /><br />在谈论session机制的时候，常常听到这样一种误解“只要关闭浏览器，session就消失了”。其实可以想象一下会员卡的例子，除非顾客主动对店家提出销卡，否则店家绝对不会轻易删除顾客的资料。对session来说也是一样的，除非程序通知服务器删除一个session，否则服务器会一直保留，程序一般都是在用户做log off的时候发个指令去删除session。然而浏览器从来不会主动在关闭之前通知服务器它将要关闭，因此服务器根本不会有机会知道浏览器已经关闭，之所以会有这种错觉，是大部分session机制都使用会话cookie来保存session id，而关闭浏览器后这个 session id就消失了，再次连接服务器时也就无法找到原来的session。如果服务器设置的cookie被保存到硬盘上，或者使用某种手段改写浏览器发出的HTTP请求头，把原来的session id发送给服务器，则再次打开浏览器仍然能够找到原来的session。<br /><br />恰恰是由于关闭浏览器不会导致session被删除，迫使服务器为seesion设置了一个失效时间，当距离客户端上一次使用session的时间超过这个失效时间时，服务器就可以认为客户端已经停止了活动，才会把session删除以节省存储空间。<br /><br />五、理解javax.servlet.http.HttpSession<br />HttpSession是Java平台对session机制的实现规范，因为它仅仅是个接口，具体到每个web应用服务器的提供商，除了对规范支持之外，仍然会有一些规范里没有规定的细微差异。这里我们以BEA的Weblogic Server8.1作为例子来演示。<br /><br />首先，Weblogic Server提供了一系列的参数来控制它的HttpSession的实现，包括使用cookie的开关选项，使用URL重写的开关选项，session持久化的设置，session失效时间的设置，以及针对cookie的各种设置，比如设置cookie的名字、路径、域， cookie的生存时间等。<br /><br />一般情况下，session都是存储在内存里，当服务器进程被停止或者重启的时候，内存里的session也会被清空，如果设置了session的持久化特性，服务器就会把session保存到硬盘上，当服务器进程重新启动或这些信息将能够被再次使用， Weblogic Server支持的持久性方式包括文件、数据库、客户端cookie保存和复制。<br /><br />复制严格说来不算持久化保存，因为session实际上还是保存在内存里，不过同样的信息被复制到各个cluster内的服务器进程中，这样即使某个服务器进程停止工作也仍然可以从其他进程中取得session。<br /><br />cookie生存时间的设置则会影响浏览器生成的cookie是否是一个会话cookie。默认是使用会话cookie。有兴趣的可以用它来试验我们在第四节里提到的那个误解。<br /><br />cookie的路径对于web应用程序来说是一个非常重要的选项，Weblogic Server对这个选项的默认处理方式使得它与其他服务器有明显的区别。后面我们会专题讨论。<br /><br />关于session的设置参考[5] http://e-docs.bea.com/wls/docs70/webapp/weblogic_xml.html#1036869<br /><br />六、HttpSession常见问题<br />（在本小节中session的含义为⑤和⑥的混合）<br /><br /><br />1、session在何时被创建<br />一个常见的误解是以为session在有客户端访问时就被创建，然而事实是直到某server端程序调用 HttpServletRequest.getSession(true)这样的语句时才被创建，注意如果JSP没有显示的使用 &lt;% @page session="false"%&gt; 关闭session，则JSP文件在编译成Servlet时将会自动加上这样一条语句 HttpSession session = HttpServletRequest.getSession(true);这也是JSP中隐含的 session对象的来历。<br /><br />由于session会消耗内存资源，因此，如果不打算使用session，应该在所有的JSP中关闭它。<br /><br />2、session何时被删除<br />综合前面的讨论，session在下列情况下被删除a.程序调用HttpSession.invalidate();或b.距离上一次收到客户端发送的session id时间间隔超过了session的超时设置;或c.服务器进程被停止（非持久session）<br /><br />3、如何做到在浏览器关闭时删除session<br />严格的讲，做不到这一点。可以做一点努力的办法是在所有的客户端页面里使用javascript代码window.oncolose来监视浏览器的关闭动作，然后向服务器发送一个请求来删除session。但是对于浏览器崩溃或者强行杀死进程这些非常规手段仍然无能为力。<br /><br />4、有个HttpSessionListener是怎么回事<br />你可以创建这样的listener去监控session的创建和销毁事件，使得在发生这样的事件时你可以做一些相应的工作。注意是session的创建和销毁动作触发listener，而不是相反。类似的与HttpSession有关的listener还有 HttpSessionBindingListener，HttpSessionActivationListener和 HttpSessionAttributeListener。<br /><br />5、存放在session中的对象必须是可序列化的吗<br />不是必需的。要求对象可序列化只是为了session能够在集群中被复制或者能够持久保存或者在必要时server能够暂时把session交换出内存。在 Weblogic Server的session中放置一个不可序列化的对象在控制台上会收到一个警告。我所用过的某个iPlanet版本如果 session中有不可序列化的对象，在session销毁时会有一个Exception，很奇怪。<br /><br />6、如何才能正确的应付客户端禁止cookie的可能性<br />对所有的URL使用URL重写，包括超链接，form的action，和重定向的URL，具体做法参见[6]<br />http://e-docs.bea.com/wls/docs70/webapp/sessions.html#100770<br /><br />7、开两个浏览器窗口访问应用程序会使用同一个session还是不同的session<br />参见第三小节对cookie的讨论，对session来说是只认id不认人，因此不同的浏览器，不同的窗口打开方式以及不同的cookie存储方式都会对这个问题的答案有影响。<br /><br />8、如何防止用户打开两个浏览器窗口操作导致的session混乱<br />这个问题与防止表单多次提交是类似的，可以通过设置客户端的令牌来解决。就是在服务器每次生成一个不同的id返回给客户端，同时保存在session里，客户端提交表单时必须把这个id也返回服务器，程序首先比较返回的id与保存在session里的值是否一致，如果不一致则说明本次操作已经被提交过了。可以参看《J2EE核心模式》关于表示层模式的部分。需要注意的是对于使用javascript window.open打开的窗口，一般不设置这个id，或者使用单独的id，以防主窗口无法操作，建议不要再window.open打开的窗口里做修改操作，这样就可以不用设置。<br /><br />9、为什么在Weblogic Server中改变session的值后要重新调用一次session.setValue<br />做这个动作主要是为了在集群环境中提示Weblogic Server session中的值发生了改变，需要向其他服务器进程复制新的session值。<br /><br />10、为什么session不见了<br />排除session正常失效的因素之外，服务器本身的可能性应该是微乎其微的，虽然笔者在iPlanet6SP1加若干补丁的Solaris版本上倒也遇到过；浏览器插件的可能性次之，笔者也遇到过3721插件造成的问题；理论上防火墙或者代理服务器在cookie处理上也有可能会出现问题。<br />出现这一问题的大部分原因都是程序的错误，最常见的就是在一个应用程序中去访问另外一个应用程序。我们在下一节讨论这个问题。<br /><br />七、跨应用程序的session共享<br /><br />常常有这样的情况，一个大项目被分割成若干小项目开发，为了能够互不干扰，要求每个小项目作为一个单独的web应用程序开发，可是到了最后突然发现某几个小项目之间需要共享一些信息，或者想使用session来实现SSO(single sign on)，在session中保存login的用户信息，最自然的要求是应用程序间能够访问彼此的session。<br /><br />然而按照Servlet规范，session的作用范围应该仅仅限于当前应用程序下，不同的应用程序之间是不能够互相访问对方的session的。各个应用服务器从实际效果上都遵守了这一规范，但是实现的细节却可能各有不同，因此解决跨应用程序session共享的方法也各不相同。<br /><br />首先来看一下Tomcat是如何实现web应用程序之间session的隔离的，从 Tomcat设置的cookie路径来看，它对不同的应用程序设置的cookie路径是不同的，这样不同的应用程序所用的session id是不同的，因此即使在同一个浏览器窗口里访问不同的应用程序，发送给服务器的session id也可以是不同的。<br /><br /><br />  <br /><br />根据这个特性，我们可以推测Tomcat中session的内存结构大致如下。<br /><br /><br /> <br /><br />笔者以前用过的iPlanet也采用的是同样的方式，估计SunONE与iPlanet之间不会有太大的差别。对于这种方式的服务器，解决的思路很简单，实际实行起来也不难。要么让所有的应用程序共享一个session id，要么让应用程序能够获得其他应用程序的session id。<br /><br />iPlanet中有一种很简单的方法来实现共享一个session id，那就是把各个应用程序的cookie路径都设为/（实际上应该是/NASApp，对于应用程序来讲它的作用相当于根）。<br />&lt;session-info&gt;<br />&lt;path&gt;/NASApp&lt;/path&gt;<br />&lt;/session-info&gt;<br /><br />需要注意的是，操作共享的session应该遵循一些编程约定，比如在session attribute名字的前面加上应用程序的前缀，使得 setAttribute("name", "neo")变成setAttribute("app1.name", "neo")，以防止命名空间冲突，导致互相覆盖。<br /><br /><br />在Tomcat中则没有这么方便的选择。在Tomcat版本3上，我们还可以有一些手段来共享session。对于版本4以上的Tomcat，目前笔者尚未发现简单的办法。只能借助于第三方的力量，比如使用文件、数据库、JMS或者客户端cookie，URL参数或者隐藏字段等手段。<br /><br />我们再看一下Weblogic Server是如何处理session的。<br /><br /><br />  <br /><br />从截屏画面上可以看到Weblogic Server对所有的应用程序设置的cookie的路径都是/，这是不是意味着在Weblogic Server中默认的就可以共享session了呢？然而一个小实验即可证明即使不同的应用程序使用的是同一个session，各个应用程序仍然只能访问自己所设置的那些属性。这说明Weblogic Server中的session的内存结构可能如下<br /><br /><br /> <br /><br />对于这样一种结构，在 session机制本身上来解决session共享的问题应该是不可能的了。除了借助于第三方的力量，比如使用文件、数据库、JMS或者客户端 cookie，URL参数或者隐藏字段等手段，还有一种较为方便的做法，就是把一个应用程序的session放到ServletContext中，这样另外一个应用程序就可以从ServletContext中取得前一个应用程序的引用。示例代码如下，<br /><br />应用程序A<br />context.setAttribute("appA", session); <br /><br />应用程序B<br />contextA = context.getContext("/appA");<br />HttpSession sessionA = (HttpSession)contextA.getAttribute("appA"); <br /><br />值得注意的是这种用法不可移植，因为根据ServletContext的JavaDoc，应用服务器可以处于安全的原因对于context.getContext("/appA");返回空值，以上做法在Weblogic Server 8.1中通过。<br /><br />那么Weblogic Server为什么要把所有的应用程序的cookie路径都设为/呢？原来是为了SSO，凡是共享这个session的应用程序都可以共享认证的信息。一个简单的实验就可以证明这一点，修改首先登录的那个应用程序的描述符weblogic.xml，把cookie路径修改为/appA 访问另外一个应用程序会重新要求登录，即使是反过来，先访问cookie路径为/的应用程序，再访问修改过路径的这个，虽然不再提示登录，但是登录的用户信息也会丢失。注意做这个实验时认证方式应该使用FORM，因为浏览器和web服务器对basic认证方式有其他的处理方式，第二次请求的认证不是通过 session来实现的。具体请参看[7] secion 14.8 Authorization，你可以修改所附的示例程序来做这些试验。<br /><br />八、总结<br />session机制本身并不复杂，然而其实现和配置上的灵活性却使得具体情况复杂多变。这也要求我们不能把仅仅某一次的经验或者某一个浏览器，服务器的经验当作普遍适用的经验，而是始终需要具体情况具体分析。<br /><br />关于作者：<br />郎云鹏（dev2dev ID: hippiewolf），软件工程师，从事J2EE开发<br />电子邮件：langyunpeng@yahoo.com.cn<br />地址：大连软件园路31号科技大厦A座大连博涵咨询服务有限公司<br /><br />参考文档：<br />[1] Preliminary Specification http://wp.netscape.com/newsref/std/cookie_spec.html<br />[2] RFC2109 http://www.rfc-editor.org/rfc/rfc2109.txt<br />[3] RFC2965 http://www.rfc-editor.org/rfc/rfc2965.txt<br />[4] The Unofficial Cookie FAQ http://www.cookiecentral.com/faq/<br />[5] http://e-docs.bea.com/wls/docs70/webapp/weblogic_xml.html#1036869<br />[6] http://e-docs.bea.com/wls/docs70/webapp/sessions.html#100770<br />[7] RFC2616 <a href="http://www.rfc-editor.org/rfc/rfc2616.txt"><font color="#002c99">http://www.rfc-editor.org/rfc/rfc2616.txt</font></a><br /><img src ="http://www.blogjava.net/Microhelp/aggbug/64066.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/Microhelp/" target="_blank">Microhelp</a> 2006-08-17 10:33 <a href="http://www.blogjava.net/Microhelp/articles/session.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Java正则表达式详解</title><link>http://www.blogjava.net/Microhelp/articles/11074.html</link><dc:creator>Microhelp</dc:creator><author>Microhelp</author><pubDate>Thu, 25 Aug 2005 08:09:00 GMT</pubDate><guid>http://www.blogjava.net/Microhelp/articles/11074.html</guid><wfw:comment>http://www.blogjava.net/Microhelp/comments/11074.html</wfw:comment><comments>http://www.blogjava.net/Microhelp/articles/11074.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/Microhelp/comments/commentRss/11074.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/Microhelp/services/trackbacks/11074.html</trackback:ping><description><![CDATA[<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>如果你曾经用过Perl或任何其他内建正则表达式支持的语言，你一定知道用正则表达式处理文本和匹配模式是多么简单。如果你不熟悉这个术语，那么“正则表达式”（Regular Expression）就是一个字符构成的串，它定义了一个用来搜索匹配字符串的模式。 </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>许多语言，包括Perl、PHP、Python、JavaScript和JScript，都支持用正则表达式处理文本，一些文本编辑器用正则表达式实现高级“搜索-替换”功能。那么Java又怎样呢？本文写作时，一个包含了用正则表达式进行文本处理的Java规范需求（Specification Request）已经得到认可，你可以期待在JDK的下一版本中看到它。 </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>然而，如果现在就需要使用正则表达式，又该怎么办呢？你可以从Apache.org下载源代码开放的Jakarta-ORO库。本文接下来的内容先简要地介绍正则表达式的入门知识，然后以Jakarta-ORO API为例介绍如何使用正则表达式。 </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14><B><FONT size=4>一、正则表达式基础知识</FONT></B> </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>我们先从简单的开始。假设你要搜索一个包含字符“cat”的字符串，搜索用的正则表达式就是“cat”。如果搜索对大小写不敏感，单词“catalog”、“Catherine”、“sophisticated”都可以匹配。也就是说： </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14><IMG src="file:///E:/DEVELOP/Language/JAVA/Articles/JS/CSDN技术中心%20Java正则表达式详解.files/01_7_31_4_a.jpg" border=0> </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14><B>1.1 句点符号</B> </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>假设你在玩英文拼字游戏，想要找出三个字母的单词，而且这些单词必须以“t”字母开头，以“n”字母结束。另外，假设有一本英文字典，你可以用正则表达式搜索它的全部内容。要构造出这个正则表达式，你可以使用一个通配符——句点符号“.”。这样，完整的表达式就是“t.n”，它匹配“tan”、“ten”、“tin”和“ton”，还匹配“t#n”、“tpn”甚至“t n”，还有其他许多无意义的组合。这是因为句点符号匹配所有字符，包括空格、Tab字符甚至换行符： </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14><IMG src="file:///E:/DEVELOP/Language/JAVA/Articles/JS/CSDN技术中心%20Java正则表达式详解.files/01_7_31_4_b.jpg" border=0> </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14><B>1.2 方括号符号</B> </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>为了解决句点符号匹配范围过于广泛这一问题，你可以在方括号（“[]”）里面指定看来有意义的字符。此时，只有方括号里面指定的字符才参与匹配。也就是说，正则表达式“t[aeio]n”只匹配“tan”、“Ten”、“tin”和“ton”。但“Toon”不匹配，因为在方括号之内你只能匹配单个字符： </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14><IMG src="file:///E:/DEVELOP/Language/JAVA/Articles/JS/CSDN技术中心%20Java正则表达式详解.files/01_7_31_4_c.jpg" border=0></TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14><B>1.3 “或”符号</B> </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>如果除了上面匹配的所有单词之外，你还想要匹配“toon”，那么，你可以使用“|”操作符。“|”操作符的基本意义就是“或”运算。要匹配“toon”，使用“t(a|e|i|o|oo)n”正则表达式。这里不能使用方扩号，因为方括号只允许匹配单个字符；这里必须使用圆括号“()”。圆括号还可以用来分组，具体请参见后面介绍。 </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14><IMG src="file:///E:/DEVELOP/Language/JAVA/Articles/JS/CSDN技术中心%20Java正则表达式详解.files/01_7_31_4_d.jpg" border=0> </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14><B>1.4 表示匹配次数的符号</B> </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>表一显示了表示匹配次数的符号，这些符号用来确定紧靠该符号左边的符号出现的次数： </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>
<P align=center><IMG src="file:///E:/DEVELOP/Language/JAVA/Articles/JS/CSDN技术中心%20Java正则表达式详解.files/01_7_31_4n.jpg" border=0></P></TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>假设我们要在文本文件中搜索美国的社会安全号码。这个号码的格式是999-99-9999。用来匹配它的正则表达式如图一所示。在正则表达式中，连字符（“-”）有着特殊的意义，它表示一个范围，比如从0到9。因此，匹配社会安全号码中的连字符号时，它的前面要加上一个转义字符“\”。 </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>
<P align=center><IMG src="file:///E:/DEVELOP/Language/JAVA/Articles/JS/CSDN技术中心%20Java正则表达式详解.files/01_7_31_4a.gif" border=0> </P></TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>
<P align=center>图一：匹配所有123-12-1234形式的社会安全号码</P></TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>假设进行搜索的时候，你希望连字符号可以出现，也可以不出现——即，999-99-9999和999999999都属于正确的格式。这时，你可以在连字符号后面加上“？”数量限定符号，如图二所示： </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>
<P align=center><IMG src="file:///E:/DEVELOP/Language/JAVA/Articles/JS/CSDN技术中心%20Java正则表达式详解.files/01_7_31_4b.gif" border=0> </P></TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>
<P align=center>图二：匹配所有123-12-1234和123121234形式的社会安全号码</P></TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>下面我们再来看另外一个例子。美国汽车牌照的一种格式是四个数字加上二个字母。它的正则表达式前面是数字部分“[0-9]{4}”，再加上字母部分“[A-Z]{2}”。图三显示了完整的正则表达式。 </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>
<P align=center><IMG src="file:///E:/DEVELOP/Language/JAVA/Articles/JS/CSDN技术中心%20Java正则表达式详解.files/01_7_31_4c.gif" border=0> </P></TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>
<P align=center>图三：匹配典型的美国汽车牌照号码，如8836KV</P></TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>1.5 “否”符号 </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>“^”符号称为“否”符号。如果用在方括号内，“^”表示不想要匹配的字符。例如，图四的正则表达式匹配所有单词，但以“X”字母开头的单词除外。 </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>
<P align=center><IMG src="file:///E:/DEVELOP/Language/JAVA/Articles/JS/CSDN技术中心%20Java正则表达式详解.files/01_7_31_4d.gif" border=0> </P></TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>
<P align=center>图四：匹配所有单词，但“X”开头的除外</P></TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>1.6 圆括号和空白符号 </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>假设要从格式为“June 26, 1951”的生日日期中提取出月份部分，用来匹配该日期的正则表达式可以如图五所示： </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>
<P align=center><IMG src="file:///E:/DEVELOP/Language/JAVA/Articles/JS/CSDN技术中心%20Java正则表达式详解.files/01_7_31_4e.gif" border=0> </P></TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>
<P align=center>图五：匹配所有Moth DD,YYYY格式的日期</P></TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>新出现的“\s”符号是空白符号，匹配所有的空白字符，包括Tab字符。如果字符串正确匹配，接下来如何提取出月份部分呢？只需在月份周围加上一个圆括号创建一个组，然后用ORO API（本文后面详细讨论）提取出它的值。修改后的正则表达式如图六所示： </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>
<P align=center><IMG src="file:///E:/DEVELOP/Language/JAVA/Articles/JS/CSDN技术中心%20Java正则表达式详解.files/01_7_31_4f.gif" border=0> </P></TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>
<P align=center>图六：匹配所有Month DD,YYYY格式的日期，定义月份值为第一个组</P></TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14><B>1.7 其它符号</B> </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>为简便起见，你可以使用一些为常见正则表达式创建的快捷符号。如表二所示： </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>表二：常用符号 </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>
<P align=center><IMG src="file:///E:/DEVELOP/Language/JAVA/Articles/JS/CSDN技术中心%20Java正则表达式详解.files/01_7_31_4o.jpg" border=0> </P></TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>例如，在前面社会安全号码的例子中，所有出现“[0-9]”的地方我们都可以使用“\d”。修改后的正则表达式如图七所示： </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>
<P align=center><IMG src="file:///E:/DEVELOP/Language/JAVA/Articles/JS/CSDN技术中心%20Java正则表达式详解.files/01_7_31_4g.gif" border=0> </P></TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>
<P align=center>图七：匹配所有123-12-1234格式的社会安全号码</P></TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14><B><FONT size=4>二、Jakarta-ORO库</FONT></B> </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>有许多源代码开放的正则表达式库可供Java程序员使用，而且它们中的许多支持Perl 5兼容的正则表达式语法。我在这里选用的是Jakarta-ORO正则表达式库，它是最全面的正则表达式API之一，而且它与Perl 5正则表达式完全兼容。另外，它也是优化得最好的API之一。 </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>Jakarta-ORO库以前叫做OROMatcher，Daniel Savarese大方地把它赠送给了Jakarta Project。你可以按照本文最后参考资源的说明下载它。 </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>我首先将简要介绍使用Jakarta-ORO库时你必须创建和访问的对象，然后介绍如何使用Jakarta-ORO API。 </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14><B>▲ PatternCompiler对象</B> </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>首先，创建一个Perl5Compiler类的实例，并把它赋值给PatternCompiler接口对象。Perl5Compiler是PatternCompiler接口的一个实现，允许你把正则表达式编译成用来匹配的Pattern对象。 </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14><IMG src="file:///E:/DEVELOP/Language/JAVA/Articles/JS/CSDN技术中心%20Java正则表达式详解.files/01_7_31_4_e.jpg" border=0> </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14><B>▲ Pattern对象</B> </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>要把正则表达式编译成Pattern对象，调用compiler对象的compile()方法，并在调用参数中指定正则表达式。例如，你可以按照下面这种方式编译正则表达式“t[aeio]n”： </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14><IMG src="file:///E:/DEVELOP/Language/JAVA/Articles/JS/CSDN技术中心%20Java正则表达式详解.files/01_7_31_4_f.jpg" border=0> </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>默认情况下，编译器创建一个大小写敏感的模式（pattern）。因此，上面代码编译得到的模式只匹配“tin”、“tan”、 “ten”和“ton”，但不匹配“Tin”和“taN”。要创建一个大小写不敏感的模式，你应该在调用编译器的时候指定一个额外的参数： </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14><IMG src="file:///E:/DEVELOP/Language/JAVA/Articles/JS/CSDN技术中心%20Java正则表达式详解.files/01_7_31_4_g.jpg" border=0> </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>创建好Pattern对象之后，你就可以通过PatternMatcher类用该Pattern对象进行模式匹配。 </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14><B>▲ PatternMatcher对象</B> </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>PatternMatcher对象根据Pattern对象和字符串进行匹配检查。你要实例化一个Perl5Matcher类并把结果赋值给PatternMatcher接口。Perl5Matcher类是PatternMatcher接口的一个实现，它根据Perl 5正则表达式语法进行模式匹配： </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14><IMG src="file:///E:/DEVELOP/Language/JAVA/Articles/JS/CSDN技术中心%20Java正则表达式详解.files/01_7_31_4_h.jpg" border=0> </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>使用PatternMatcher对象，你可以用多个方法进行匹配操作，这些方法的第一个参数都是需要根据正则表达式进行匹配的字符串： </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>· boolean matches(String input, Pattern pattern)：当输入字符串和正则表达式要精确匹配时使用。换句话说，正则表达式必须完整地描述输入字符串。 </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>· boolean matchesPrefix(String input, Pattern pattern)：当正则表达式匹配输入字符串起始部分时使用。 </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>· boolean contains(String input, Pattern pattern)：当正则表达式要匹配输入字符串的一部分时使用（即，它必须是一个子串）。 </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>另外，在上面三个方法调用中，你还可以用PatternMatcherInput对象作为参数替代String对象；这时，你可以从字符串中最后一次匹配的位置开始继续进行匹配。当字符串可能有多个子串匹配给定的正则表达式时，用PatternMatcherInput对象作为参数就很有用了。用PatternMatcherInput对象作为参数替代String时，上述三个方法的语法如下： </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>· boolean matches(PatternMatcherInput input, Pattern pattern) </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>· boolean matchesPrefix(PatternMatcherInput input, Pattern pattern) </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>· boolean contains(PatternMatcherInput input, Pattern pattern) </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14><B><FONT size=4>三、应用实例</FONT></B> </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>下面我们来看看Jakarta-ORO库的一些应用实例。 </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14><B>3.1 日志文件处理</B> </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>任务：分析一个Web服务器日志文件，确定每一个用户花在网站上的时间。在典型的BEA WebLogic日志文件中，日志记录的格式如下： </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14><IMG src="file:///E:/DEVELOP/Language/JAVA/Articles/JS/CSDN技术中心%20Java正则表达式详解.files/01_7_31_4_i.jpg" border=0></TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>分析这个日志记录，可以发现，要从这个日志文件提取的内容有两项：IP地址和页面访问时间。你可以用分组符号（圆括号）从日志记录提取出IP地址和时间标记。 </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>首先我们来看看IP地址。IP地址有4个字节构成，每一个字节的值在0到255之间，各个字节通过一个句点分隔。因此，IP地址中的每一个字节有至少一个、最多三个数字。图八显示了为IP地址编写的正则表达式： </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>
<P align=center><IMG src="file:///E:/DEVELOP/Language/JAVA/Articles/JS/CSDN技术中心%20Java正则表达式详解.files/01_7_31_4h.gif" border=0> </P></TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>
<P align=center>图八：匹配IP地址</P></TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>IP地址中的句点字符必须进行转义处理（前面加上“\”），因为IP地址中的句点具有它本来的含义，而不是采用正则表达式语法中的特殊含义。句点在正则表达式中的特殊含义本文前面已经介绍。 </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>日志记录的时间部分由一对方括号包围。你可以按照如下思路提取出方括号里面的所有内容：首先搜索起始方括号字符（“[”），提取出所有不超过结束方括号字符（“]”）的内容，向前寻找直至找到结束方括号字符。图九显示了这部分的正则表达式。 </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>
<P align=center><IMG src="file:///E:/DEVELOP/Language/JAVA/Articles/JS/CSDN技术中心%20Java正则表达式详解.files/01_7_31_4i.gif" border=0> </P></TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>
<P align=center>图九：匹配至少一个字符，直至找到“]”</P></TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>现在，把上述两个正则表达式加上分组符号（圆括号）后合并成单个表达式，这样就可以从日志记录提取出IP地址和时间。注意，为了匹配“- -”（但不提取它），正则表达式中间加入了“\s-\s-\s”。完整的正则表达式如图十所示。 </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>
<P align=center><IMG src="file:///E:/DEVELOP/Language/JAVA/Articles/JS/CSDN技术中心%20Java正则表达式详解.files/01_7_31_4j.gif" border=0> </P></TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>
<P align=center>图十：匹配IP地址和时间标记</P></TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>现在正则表达式已经编写完毕，接下来可以编写使用正则表达式库的Java代码了。 </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>为使用Jakarta-ORO库，首先创建正则表达式字符串和待分析的日志记录字符串： </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14><IMG src="file:///E:/DEVELOP/Language/JAVA/Articles/JS/CSDN技术中心%20Java正则表达式详解.files/01_7_31_4_j.jpg" border=0> </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>这里使用的正则表达式与图十的正则表达式差不多完全相同，但有一点例外：在Java中，你必须对每一个向前的斜杠（“\”）进行转义处理。图十不是Java的表示形式，所以我们要在每个“\”前面加上一个“\”以免出现编译错误。遗憾的是，转义处理过程很容易出现错误，所以应该小心谨慎。你可以首先输入未经转义处理的正则表达式，然后从左到右依次把每一个“\”替换成“\\”。如果要复检，你可以试着把它输出到屏幕上。 </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>初始化字符串之后，实例化PatternCompiler对象，用PatternCompiler编译正则表达式创建一个Pattern对象： </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14><IMG src="file:///E:/DEVELOP/Language/JAVA/Articles/JS/CSDN技术中心%20Java正则表达式详解.files/01_7_31_4_k.jpg" border=0> </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>现在，创建PatternMatcher对象，调用PatternMatcher接口的contain()方法检查匹配情况： </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14><IMG src="file:///E:/DEVELOP/Language/JAVA/Articles/JS/CSDN技术中心%20Java正则表达式详解.files/01_7_31_4_l.jpg" border=0> </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>接下来，利用PatternMatcher接口返回的MatchResult对象，输出匹配的组。由于logEntry字符串包含匹配的内容，你可以看到类如下面的输出： </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14><IMG src="file:///E:/DEVELOP/Language/JAVA/Articles/JS/CSDN技术中心%20Java正则表达式详解.files/01_7_31_4_m.jpg" border=0></TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14><B>3.2 HTML处理实例一</B> </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>下面一个任务是分析HTML页面内FONT标记的所有属性。HTML页面内典型的FONT标记如下所示： </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14><FONT face="Arial, Serif" color=red size=+2></FONT><IMG src="file:///E:/DEVELOP/Language/JAVA/Articles/JS/CSDN技术中心%20Java正则表达式详解.files/01_7_31_4_n.jpg" border=0> </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>程序将按照如下形式，输出每一个FONT标记的属性： </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14><IMG src="file:///E:/DEVELOP/Language/JAVA/Articles/JS/CSDN技术中心%20Java正则表达式详解.files/01_7_31_4_o.jpg" border=0> </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>在这种情况下，我建议你使用两个正则表达式。第一个如图十一所示，它从字体标记提取出“"face="Arial, Serif" size="+2" color="red"”。 </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>
<P align=center><IMG src="file:///E:/DEVELOP/Language/JAVA/Articles/JS/CSDN技术中心%20Java正则表达式详解.files/01_7_31_4k.gif" border=0> </P></TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>
<P align=center>图十一：匹配FONT标记的所有属性</P></TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>第二个正则表达式如图十二所示，它把各个属性分割成名字-值对。 </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>
<P align=center><IMG src="file:///E:/DEVELOP/Language/JAVA/Articles/JS/CSDN技术中心%20Java正则表达式详解.files/01_7_31_4l.gif" border=0> </P></TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>
<P align=center>图十二：匹配单个属性，并把它分割成名字-值对</P></TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>分割结果为： </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14><IMG src="file:///E:/DEVELOP/Language/JAVA/Articles/JS/CSDN技术中心%20Java正则表达式详解.files/01_7_31_4_p.jpg" border=0> </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>现在我们来看看完成这个任务的Java代码。首先创建两个正则表达式字符串，用Perl5Compiler把它们编译成Pattern对象。编译正则表达式的时候，指定Perl5Compiler.CASE_INSENSITIVE_MASK选项，使得匹配操作不区分大小写。 </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>接下来，创建一个执行匹配操作的Perl5Matcher对象。 </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14><IMG src="file:///E:/DEVELOP/Language/JAVA/Articles/JS/CSDN技术中心%20Java正则表达式详解.files/01_7_31_4_q.jpg" border=0> </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>假设有一个String类型的变量html，它代表了HTML文件中的一行内容。如果html字符串包含FONT标记，匹配器将返回true。此时，你可以用匹配器对象返回的MatchResult对象获得第一个组，它包含了FONT的所有属性： </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14><IMG src="file:///E:/DEVELOP/Language/JAVA/Articles/JS/CSDN技术中心%20Java正则表达式详解.files/01_7_31_4_r.jpg" border=0> </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>接下来创建一个PatternMatcherInput对象。这个对象允许你从最后一次匹配的位置开始继续进行匹配操作，因此，它很适合于提取FONT标记内属性的名字-值对。创建PatternMatcherInput对象，以参数形式传入待匹配的字符串。然后，用匹配器实例提取出每一个FONT的属性。这通过指定PatternMatcherInput对象（而不是字符串对象）为参数，反复地调用PatternMatcher对象的contains()方法完成。PatternMatcherInput对象之中的每一次迭代将把它内部的指针向前移动，下一次检测将从前一次匹配位置的后面开始。 </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>本例的输出结果如下： </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14><IMG src="file:///E:/DEVELOP/Language/JAVA/Articles/JS/CSDN技术中心%20Java正则表达式详解.files/01_7_31_4_s.jpg" border=0> </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14><B>3.3 HTML处理实例二</B> </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>下面我们来看看另一个处理HTML的例子。这一次，我们假定Web服务器从widgets.acme.com移到了newserver.acme.com。现在你要修改一些页面中的链接： </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14><IMG src="file:///E:/DEVELOP/Language/JAVA/Articles/JS/CSDN技术中心%20Java正则表达式详解.files/01_7_31_4_t.jpg" border=0> </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>执行这个搜索的正则表达式如图十三所示： </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>
<P align=center><IMG src="file:///E:/DEVELOP/Language/JAVA/Articles/JS/CSDN技术中心%20Java正则表达式详解.files/01_7_31_4m.gif" border=0> </P></TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>
<P align=center>图十三：匹配修改前的链接</P></TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>如果能够匹配这个正则表达式，你可以用下面的内容替换图十三的链接： </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14><A href="http://newserver.acme.com/interface.html#$1"></A><IMG src="file:///E:/DEVELOP/Language/JAVA/Articles/JS/CSDN技术中心%20Java正则表达式详解.files/01_7_31_4_u.jpg" border=0></TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>注意#字符的后面加上了$1。Perl正则表达式语法用$1、$2等表示已经匹配且提取出来的组。图十三的表达式把所有作为一个组匹配和提取出来的内容附加到链接的后面。 </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>现在，返回Java。就象前面我们所做的那样，你必须创建测试字符串，创建把正则表达式编译到Pattern对象所必需的对象，以及创建一个PatternMatcher对象：<IMG src="file:///E:/DEVELOP/Language/JAVA/Articles/JS/CSDN技术中心%20Java正则表达式详解.files/01_7_31_4_v.jpg" border=0> </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>接下来，用com.oroinc.text.regex包Util类的substitute()静态方法进行替换，输出结果字符串： </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14><IMG src="file:///E:/DEVELOP/Language/JAVA/Articles/JS/CSDN技术中心%20Java正则表达式详解.files/01_7_31_4_w.jpg" border=0> </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>Util.substitute()方法的语法如下： </TD></TR></TBODY></TABLE>
<TABLE height=17 width=620 align=center>
<TBODY>
<TR>
<TD class=a14 height=13><IMG src="file:///E:/DEVELOP/Language/JAVA/Articles/JS/CSDN技术中心%20Java正则表达式详解.files/01_7_31_4_x.jpg" border=0> </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14>这个调用的前两个参数是以前创建的PatternMatcher和Pattern对象。第三个参数是一个Substiution对象，它决定了替换操作如何进行。本例使用的是Perl5Substitution对象，它能够进行Perl5风格的替换。第四个参数是想要进行替换操作的字符串，最后一个参数允许指定是否替换模式的所有匹配子串（Util.SUBSTITUTE_ALL），或只替换指定的次数。 </TD></TR></TBODY></TABLE>
<TABLE width=620 align=center>
<TBODY>
<TR>
<TD class=a14><B>【结束语】</B>在这篇文章中，我为你介绍了正则表达式的强大功能。只要正确运用，正则表达式能够在字符串提取和文本修改中起到很大的作用。另外，我还介绍了如何在Java程序中通过Jakarta-ORO库利用正则表达式。至于最终采用老式的字符串处理方式（使用StringTokenizer，charAt，和substring），还是采用正则表达式，这就有待你自己决定了。</TD></TR></TBODY></TABLE><img src ="http://www.blogjava.net/Microhelp/aggbug/11074.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/Microhelp/" target="_blank">Microhelp</a> 2005-08-25 16:09 <a href="http://www.blogjava.net/Microhelp/articles/11074.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>ECLIPSE初试</title><link>http://www.blogjava.net/Microhelp/articles/6655.html</link><dc:creator>Microhelp</dc:creator><author>Microhelp</author><pubDate>Fri, 24 Jun 2005 02:07:00 GMT</pubDate><guid>http://www.blogjava.net/Microhelp/articles/6655.html</guid><wfw:comment>http://www.blogjava.net/Microhelp/comments/6655.html</wfw:comment><comments>http://www.blogjava.net/Microhelp/articles/6655.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/Microhelp/comments/commentRss/6655.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/Microhelp/services/trackbacks/6655.html</trackback:ping><description><![CDATA[<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT size=4><FONT color=#000080><FONT face=Arial><SPAN lang=EN-US>ECLIPSE </SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">使用初步</SPAN></FONT></FONT></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT size=4><FONT color=#000080><FONT face=Arial><SPAN lang=EN-US style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'">1 </SPAN><SPAN style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'">集成<SPAN lang=EN-US>lomboz</SPAN>和<SPAN lang=EN-US>tomcat<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p></SPAN></SPAN></FONT></FONT></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'"><FONT size=4><FONT color=#000080><FONT face=Arial>网上有很多文章<SPAN lang=EN-US>,</SPAN>不再赘诉<SPAN lang=EN-US>,</SPAN>只要把相应的插件下载后解压<SPAN lang=EN-US>,</SPAN>拷贝到<SPAN lang=EN-US>eclipse</SPAN>对应的目录即可<SPAN lang=EN-US>.</SPAN>如果安装正确将在工具栏上有相应的图标<SPAN lang=EN-US>(</SPAN>如图<SPAN lang=EN-US>).<o:p></o:p></SPAN></FONT></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'"><FONT size=4><FONT color=#000080><FONT face=Arial><?xml:namespace prefix = v ns = "urn:schemas-microsoft-com:vml" /><v:shapetype id=_x0000_t75 coordsize="21600,21600" o:spt="75" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@5xe" filled="f" stroked="f"><v:stroke joinstyle="miter"></v:stroke><v:formulas><v:f eqn="if lineDrawn pixelLineWidth 0"></v:f><v:f eqn="sum @0 1 0"></v:f><v:f eqn="sum 0 0 @1"></v:f><v:f eqn="prod @2 1 2"></v:f><v:f eqn="prod @3 21600 pixelWidth"></v:f><v:f eqn="prod @3 21600 pixelHeight"></v:f><v:f eqn="sum @0 0 1"></v:f><v:f eqn="prod @6 1 2"></v:f><v:f eqn="prod @7 21600 pixelWidth"></v:f><v:f eqn="sum @8 21600 0"></v:f><v:f eqn="prod @7 21600 pixelHeight"></v:f><v:f eqn="sum @10 21600 0"></v:f></v:formulas><v:path o:extrusionok="f" gradientshapeok="t" o:connecttype="rect"></v:path><o:lock v:ext="edit" aspectratio="t"></o:lock></v:shapetype><o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT size=4><FONT color=#000080><FONT face=Arial><SPAN lang=EN-US style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'"><SPAN lang=EN-US><IMG height=83 alt=1.JPG src="http://www.blogjava.net/images/blogjava_net/microhelp/Java/eclipse/1.JPG" width=594 border=0></SPAN><BR>2 </SPAN><SPAN style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'">创建工程<SPAN lang=EN-US><o:p></o:p></SPAN></SPAN></FONT></FONT></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'"><FONT size=4><FONT color=#000080><FONT face=Arial>FILE-&gt;NEW-&gt;PROJECT<BR><IMG height=500 alt=2.JPG src="http://www.blogjava.net/images/blogjava_net/microhelp/Java/eclipse/2.JPG" width=525 border=0><BR><o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'"><o:p><FONT face=Arial color=#000080 size=4></FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'"><FONT size=4><FONT color=#000080><FONT face=Arial>选择<SPAN lang=EN-US>Java Project,Next<o:p></o:p></SPAN></FONT></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'"><o:p><FONT face=Arial color=#000080 size=4></FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'"><FONT size=4><FONT color=#000080><FONT face=Arial><IMG height=538 alt=3.JPG src="http://www.blogjava.net/images/blogjava_net/microhelp/Java/eclipse/3.JPG" width=525 border=0><BR>填写工程名称<SPAN lang=EN-US>,</SPAN>选择分开的源文件和输出文件<SPAN lang=EN-US>.Next-&gt;Finish.<o:p></o:p></SPAN></FONT></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'"><FONT face=Arial color=#000080 size=4>右击<SPAN lang=EN-US>src</SPAN>文件夹<SPAN lang=EN-US>,</SPAN>新建一个<SPAN lang=EN-US>package,</SPAN>名称为</FONT><SPAN lang=EN-US><FONT face=Arial color=#000080 size=4>world<BR><IMG height=417 alt=4.JPG src="http://www.blogjava.net/images/blogjava_net/microhelp/Java/eclipse/4.JPG" width=525 border=0><BR></FONT></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'"><FONT face=Arial color=#000080 size=4>工程结构可以在导航栏中看到<SPAN lang=EN-US>(</SPAN>如图</FONT><SPAN lang=EN-US><FONT size=4><FONT color=#000080><FONT face=Arial>)<BR><IMG height=159 alt=5.JPG src="http://www.blogjava.net/images/blogjava_net/microhelp/Java/eclipse/5.JPG" width=199 border=0><BR><o:p></o:p></FONT></FONT></FONT></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'"><o:p><FONT face=Arial color=#000080 size=4></FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'"><FONT size=4><FONT color=#000080><FONT face=Arial>右击<SPAN lang=EN-US>world,</SPAN>新建一个<SPAN lang=EN-US>class,</SPAN>名称为<SPAN lang=EN-US>Helloworld<o:p></o:p></SPAN></FONT></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'"><o:p><FONT face=Arial color=#000080 size=4></FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'"><FONT size=4><FONT color=#000080><FONT face=Arial><IMG height=544 alt=6.JPG src="http://www.blogjava.net/images/blogjava_net/microhelp/Java/eclipse/6.JPG" width=576 border=0><BR>选择菜单<SPAN lang=EN-US>project,</SPAN>选择<SPAN lang=EN-US>Build Automatically,</SPAN>保存后可以看到在<SPAN lang=EN-US>bin</SPAN>文件夹里出现相应的<SPAN lang=EN-US>class</SPAN>文件<SPAN lang=EN-US>.<o:p></o:p></SPAN></FONT></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'"><FONT size=4><FONT color=#000080><FONT face=Arial>在<SPAN lang=EN-US>main</SPAN>方法中增加如下内容<SPAN lang=EN-US>:<o:p></o:p></SPAN></FONT></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'"><FONT size=4><FONT color=#000080><FONT face=Arial>int testValue = 0;<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'"><FONT size=4><FONT color=#000080><FONT face=Arial><SPAN style="mso-tab-count: 2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>System.out.println("Hello World");<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'"><FONT size=4><FONT color=#000080><FONT face=Arial><SPAN style="mso-tab-count: 2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>testValue = testValue + 2;<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'"><FONT size=4><FONT color=#000080><FONT face=Arial><SPAN style="mso-tab-count: 2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>System.out.println("testValue:" + testValue);<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT size=4><FONT color=#000080><FONT face=Arial><SPAN style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'">菜单<SPAN lang=EN-US>run-&gt;run</SPAN></SPAN><SPAN lang=EN-US style="mso-ascii-font-family: 宋体">…</SPAN><SPAN lang=EN-US style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'"><o:p></o:p></SPAN></FONT></FONT></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><o:p><FONT face=Arial color=#000080 size=4>&nbsp;<IMG height=560 alt=7.JPG src="http://www.blogjava.net/images/blogjava_net/microhelp/Java/eclipse/7.JPG" width=657 border=0></FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'"><o:p><FONT face=Arial color=#000080 size=4>&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'"><o:p><FONT face=Arial color=#000080 size=4></FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'"><FONT size=4><FONT color=#000080><FONT face=Arial>双击<SPAN lang=EN-US>Configuration</SPAN>里的<SPAN lang=EN-US>Java Application.</SPAN>点击<SPAN lang=EN-US>Run</SPAN>按钮<SPAN lang=EN-US>.</SPAN>可以看到<SPAN lang=EN-US>Console</SPAN>里的输出如下<SPAN lang=EN-US>:<o:p></o:p></SPAN></FONT></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'"><o:p><FONT face=Arial color=#000080 size=4></FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT size=4><FONT color=#000080><FONT face=Arial><SPAN lang=EN-US style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'"><IMG height=133 alt=8.JPG src="http://www.blogjava.net/images/blogjava_net/microhelp/Java/eclipse/8.JPG" width=417 border=0><BR>3.</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'">断点测试<SPAN lang=EN-US><o:p></o:p></SPAN></SPAN></FONT></FONT></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT size=4><FONT color=#000080><FONT face=Arial><SPAN lang=EN-US style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'">双击第一句话左侧<SPAN lang=EN-US>,</SPAN>或者右击选择<SPAN lang=EN-US>Toggle Breakpoint.</SPAN>添加断点<SPAN lang=EN-US>.</SPAN>然后选择<SPAN lang=EN-US>Debug.</SPAN>可以按<SPAN lang=EN-US>F6</SPAN>逐步调试<SPAN lang=EN-US>,</SPAN>可以观察<SPAN lang=EN-US>testValue</SPAN>的值<SPAN lang=EN-US>,</SPAN>和看到<SPAN lang=EN-US>Console</SPAN>的输出<SPAN lang=EN-US>.<o:p></o:p></SPAN></SPAN></FONT></FONT></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT size=4><FONT color=#000080><FONT face=Arial><SPAN lang=EN-US style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'">4.Junit</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'">的使用<SPAN lang=EN-US><o:p></o:p></SPAN></SPAN></FONT></FONT></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN style="COLOR: #333333; FONT-FAMILY: 宋体; mso-bidi-font-size: 9.0pt; mso-bidi-font-family: Tahoma"><FONT size=4><FONT color=#000080><FONT face=Arial>首先我们给<SPAN lang=EN-US>Hello</SPAN>类添加一个<SPAN lang=EN-US>abs()</SPAN>方法，作用是返回绝对值<SPAN lang=EN-US><o:p></o:p></SPAN></FONT></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><FONT size=4><FONT color=#000080><FONT face=Arial>public int abs(int n){<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><FONT size=4><FONT color=#000080><FONT face=Arial><SPAN style="mso-tab-count: 2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>return n&gt;0?n:(-n);<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><FONT size=4><FONT color=#000080><FONT face=Arial><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN>}<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN style="COLOR: #333333; FONT-FAMILY: 宋体; mso-bidi-font-size: 9.0pt; mso-bidi-font-family: Tahoma"><FONT size=4><FONT color=#000080><FONT face=Arial>选中<SPAN lang=EN-US>HelloWorld.java</SPAN>，右键点击，选择<SPAN lang=EN-US>New-&gt;JUnit Test Case</SPAN>：<SPAN lang=EN-US><o:p></o:p></SPAN></FONT></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT size=4><FONT color=#000080><FONT face=Arial><SPAN lang=EN-US style="COLOR: #333333; mso-bidi-font-size: 9.0pt; mso-bidi-font-family: Tahoma">Eclipse</SPAN><SPAN style="COLOR: #333333; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 9.0pt; mso-bidi-font-family: Tahoma">在询问是否添加</SPAN><SPAN lang=EN-US style="COLOR: #333333; mso-bidi-font-size: 9.0pt; mso-bidi-font-family: Tahoma">junit.jar</SPAN><SPAN style="COLOR: #333333; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 9.0pt; mso-bidi-font-family: Tahoma">包之后，确定后新建一个</SPAN><SPAN lang=EN-US>Helloworld</SPAN><SPAN lang=EN-US style="COLOR: #333333; mso-bidi-font-size: 9.0pt; mso-bidi-font-family: Tahoma">Test</SPAN><SPAN style="COLOR: #333333; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 9.0pt; mso-bidi-font-family: Tahoma">类，用来测试</SPAN><SPAN lang=EN-US>Helloworld</SPAN><SPAN style="COLOR: #333333; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 9.0pt; mso-bidi-font-family: Tahoma">类。</SPAN><SPAN lang=EN-US style="COLOR: #333333; mso-bidi-font-size: 9.0pt; mso-bidi-font-family: Tahoma"><o:p></o:p></SPAN></FONT></FONT></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT size=4><FONT color=#000080><FONT face=Arial><SPAN style="COLOR: #333333; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 9.0pt; mso-bidi-font-family: Tahoma">选中</SPAN><SPAN lang=EN-US style="COLOR: #333333; mso-bidi-font-size: 9.0pt; mso-bidi-font-family: Tahoma">setUp()</SPAN><SPAN style="COLOR: #333333; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 9.0pt; mso-bidi-font-family: Tahoma">和</SPAN><SPAN lang=EN-US style="COLOR: #333333; mso-bidi-font-size: 9.0pt; mso-bidi-font-family: Tahoma">tearDown()</SPAN><SPAN style="COLOR: #333333; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 9.0pt; mso-bidi-font-family: Tahoma">，然后点击“</SPAN><SPAN lang=EN-US style="COLOR: #333333; mso-bidi-font-size: 9.0pt; mso-bidi-font-family: Tahoma">Next</SPAN><SPAN style="COLOR: #333333; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 9.0pt; mso-bidi-font-family: Tahoma">”：</SPAN><SPAN lang=EN-US style="COLOR: #333333; mso-bidi-font-size: 9.0pt; mso-bidi-font-family: Tahoma"><o:p></o:p></SPAN></FONT></FONT></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="COLOR: #333333; mso-bidi-font-size: 9.0pt; mso-bidi-font-family: Tahoma"><o:p><FONT face=Arial color=#000080 size=4></FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT size=4><FONT color=#000080><FONT face=Arial><SPAN style="COLOR: #333333; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 9.0pt; mso-bidi-font-family: Tahoma"><IMG height=498 alt=9.JPG src="http://www.blogjava.net/images/blogjava_net/microhelp/Java/eclipse/9.JPG" width=544 border=0><BR>选择要测试的方法</SPAN><SPAN lang=EN-US style="COLOR: #333333; mso-bidi-font-size: 9.0pt; mso-bidi-font-family: Tahoma">,</SPAN><SPAN style="COLOR: #333333; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 9.0pt; mso-bidi-font-family: Tahoma">在前面打勾</SPAN><SPAN lang=EN-US style="COLOR: #333333; mso-bidi-font-size: 9.0pt; mso-bidi-font-family: Tahoma">.<o:p></o:p></SPAN></FONT></FONT></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="COLOR: #333333; mso-bidi-font-size: 9.0pt; mso-bidi-font-family: Tahoma"><o:p><FONT face=Arial color=#000080 size=4></FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT size=4><FONT color=#000080><FONT face=Arial><SPAN style="COLOR: #333333; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 9.0pt; mso-bidi-font-family: Tahoma"><IMG height=498 alt=10.JPG src="http://www.blogjava.net/images/blogjava_net/microhelp/Java/eclipse/10.JPG" width=544 border=0><BR>再在</SPAN><SPAN lang=EN-US style="COLOR: #333333; mso-bidi-font-size: 9.0pt; mso-bidi-font-family: Tahoma">HelloWorldtest</SPAN><SPAN style="COLOR: #333333; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 9.0pt; mso-bidi-font-family: Tahoma">中添加测试代码</SPAN><SPAN lang=EN-US style="COLOR: #333333; mso-bidi-font-size: 9.0pt; mso-bidi-font-family: Tahoma">:<o:p></o:p></SPAN></FONT></FONT></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="COLOR: #333333; mso-bidi-font-size: 9.0pt; mso-bidi-font-family: Tahoma"><o:p><FONT face=Arial color=#000080 size=4></FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT size=4><FONT color=#000080><FONT face=Arial><SPAN style="COLOR: #333333; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 9.0pt; mso-bidi-font-family: Tahoma"><IMG height=458 alt=11.JPG src="http://www.blogjava.net/images/blogjava_net/microhelp/Java/eclipse/11.JPG" width=553 border=0><BR>点击</SPAN><SPAN lang=EN-US style="COLOR: #333333; mso-bidi-font-size: 9.0pt; mso-bidi-font-family: Tahoma">Run as Junit,</SPAN><SPAN style="COLOR: #333333; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 9.0pt; mso-bidi-font-family: Tahoma">左侧导航栏切换到</SPAN><SPAN lang=EN-US style="COLOR: #333333; mso-bidi-font-size: 9.0pt; mso-bidi-font-family: Tahoma">Junit.</SPAN><SPAN style="COLOR: #333333; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 9.0pt; mso-bidi-font-family: Tahoma">如果测试通过</SPAN><SPAN lang=EN-US style="COLOR: #333333; mso-bidi-font-size: 9.0pt; mso-bidi-font-family: Tahoma">,</SPAN><SPAN style="COLOR: #333333; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 9.0pt; mso-bidi-font-family: Tahoma">将为绿色显示</SPAN><SPAN lang=EN-US style="COLOR: #333333; mso-bidi-font-size: 9.0pt; mso-bidi-font-family: Tahoma">.<o:p></o:p></SPAN></FONT></FONT></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT size=4><FONT color=#000080><FONT face=Arial><SPAN lang=EN-US style="COLOR: #333333; mso-bidi-font-size: 9.0pt; mso-bidi-font-family: Tahoma">5.</SPAN><SPAN style="COLOR: #333333; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 9.0pt; mso-bidi-font-family: Tahoma">添加</SPAN><SPAN lang=EN-US style="COLOR: #333333; mso-bidi-font-size: 9.0pt; mso-bidi-font-family: Tahoma">tomcat</SPAN><SPAN style="COLOR: #333333; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 9.0pt; mso-bidi-font-family: Tahoma">应用程序</SPAN><SPAN lang=EN-US style="COLOR: #333333; mso-bidi-font-size: 9.0pt; mso-bidi-font-family: Tahoma"><o:p></o:p></SPAN></FONT></FONT></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="COLOR: #333333; mso-bidi-font-size: 9.0pt; mso-bidi-font-family: Tahoma"><o:p><FONT face=Arial color=#000080 size=4></FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT size=4><FONT color=#000080><FONT face=Arial><SPAN style="COLOR: #333333; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 9.0pt; mso-bidi-font-family: Tahoma"><IMG height=500 alt=12.JPG src="http://www.blogjava.net/images/blogjava_net/microhelp/Java/eclipse/12.JPG" width=525 border=0><BR>按照向导完成即可</SPAN><SPAN lang=EN-US style="COLOR: #333333; mso-bidi-font-size: 9.0pt; mso-bidi-font-family: Tahoma">.</SPAN><SPAN style="COLOR: #333333; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 9.0pt; mso-bidi-font-family: Tahoma">可以看到在</SPAN><SPAN lang=EN-US style="COLOR: #333333; mso-bidi-font-size: 9.0pt; mso-bidi-font-family: Tahoma">server.xml</SPAN><SPAN style="COLOR: #333333; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 9.0pt; mso-bidi-font-family: Tahoma">中增加了如下内容</SPAN><SPAN lang=EN-US style="COLOR: #333333; mso-bidi-font-size: 9.0pt; mso-bidi-font-family: Tahoma">:<o:p></o:p></SPAN></FONT></FONT></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="COLOR: #333333; mso-bidi-font-size: 9.0pt; mso-bidi-font-family: Tahoma"><FONT size=4><FONT color=#000080><FONT face=Arial>&lt;Context path="/testApp" reloadable="true" docBase="D:\eclipse\workspace\testApp" workDir="D:\eclipse\workspace\testApp\work\org\apache\jsp" /&gt;<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT size=4><FONT color=#000080><FONT face=Arial><SPAN style="COLOR: #333333; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 9.0pt; mso-bidi-font-family: Tahoma">如果右击</SPAN><SPAN lang=EN-US style="COLOR: #333333; mso-bidi-font-size: 9.0pt; mso-bidi-font-family: Tahoma">src</SPAN><SPAN style="COLOR: #333333; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 9.0pt; mso-bidi-font-family: Tahoma">添加包和类的话</SPAN><SPAN lang=EN-US style="COLOR: #333333; mso-bidi-font-size: 9.0pt; mso-bidi-font-family: Tahoma">,</SPAN><SPAN style="COLOR: #333333; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 9.0pt; mso-bidi-font-family: Tahoma">保存后会自动添加到</SPAN><SPAN lang=EN-US style="COLOR: #333333; mso-bidi-font-size: 9.0pt; mso-bidi-font-family: Tahoma">classes</SPAN><SPAN style="COLOR: #333333; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 9.0pt; mso-bidi-font-family: Tahoma">中</SPAN><SPAN lang=EN-US style="COLOR: #333333; mso-bidi-font-size: 9.0pt; mso-bidi-font-family: Tahoma">.<o:p></o:p></SPAN></FONT></FONT></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT size=4><FONT color=#000080><FONT face=Arial><SPAN style="COLOR: #333333; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 9.0pt; mso-bidi-font-family: Tahoma">右击</SPAN><SPAN lang=EN-US style="COLOR: #333333; mso-bidi-font-size: 9.0pt; mso-bidi-font-family: Tahoma">testApp</SPAN><SPAN style="COLOR: #333333; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 9.0pt; mso-bidi-font-family: Tahoma">新建文件</SPAN><SPAN lang=EN-US style="COLOR: #333333; mso-bidi-font-size: 9.0pt; mso-bidi-font-family: Tahoma">index.jsp,</SPAN><SPAN style="COLOR: #333333; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-size: 9.0pt; mso-bidi-font-family: Tahoma">添加如下内容</SPAN><SPAN lang=EN-US style="COLOR: #333333; mso-bidi-font-size: 9.0pt; mso-bidi-font-family: Tahoma">:<o:p></o:p></SPAN></FONT></FONT></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT size=4><FONT color=#000080><FONT face=Arial><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: yellow; COLOR: red; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: yellow">&lt;%@</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: yellow; COLOR: black; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: yellow"> </SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: yellow; COLOR: #7f0055; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: yellow">page</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: yellow; COLOR: black; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: yellow"> </SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: yellow; COLOR: #7f0055; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: yellow">contentType</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: yellow; COLOR: black; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: yellow">=</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: yellow; COLOR: #2a00ff; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: yellow">"text/html;charset=GBK"</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: yellow; COLOR: red; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: yellow">%&gt;</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"> </SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></FONT></FONT></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p><FONT face=Arial color=#000080 size=4>&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT size=4><FONT color=#000080><FONT face=Arial><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #2a00ff; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white">&lt;html&gt;</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></FONT></FONT></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT size=4><FONT color=#000080><FONT face=Arial><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #2a00ff; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white">&lt;head&gt;</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></FONT></FONT></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT size=4><FONT color=#000080><FONT face=Arial><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-tab-count: 2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #2a00ff; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white">&lt;title&gt;</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></FONT></FONT></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT size=4><FONT color=#000080><FONT face=Arial><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-tab-count: 3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>Helloworld</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></FONT></FONT></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT size=4><FONT color=#000080><FONT face=Arial><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-tab-count: 2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #2a00ff; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white">&lt;/title&gt;</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></FONT></FONT></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT size=4><FONT color=#000080><FONT face=Arial><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #2a00ff; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white">&lt;/head&gt;</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></FONT></FONT></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT size=4><FONT color=#000080><FONT face=Arial><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #2a00ff; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white">&lt;body&gt;</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></FONT></FONT></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT size=4><FONT color=#000080><FONT face=Arial><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-tab-count: 2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>Helloworld!</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></FONT></FONT></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT size=4><FONT color=#000080><FONT face=Arial><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #2a00ff; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white">&lt;/body&gt;</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></FONT></FONT></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT size=4><FONT color=#000080><FONT face=Arial><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #2a00ff; FONT-FAMILY: 'Courier New'; mso-highlight: white">&lt;/html&gt;</SPAN><SPAN lang=EN-US style="COLOR: #333333; mso-bidi-font-size: 9.0pt; mso-bidi-font-family: Tahoma"><o:p></o:p></SPAN></FONT></FONT></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'"><FONT size=4><FONT color=#000080><FONT face=Arial>然后启动<SPAN lang=EN-US>tomcat,</SPAN>在浏览器中输入<SPAN lang=EN-US><A href="http://127.0.0.1:8080/testApp/index.jsp,即可看到index的内容了">http://127.0.0.1:8080/testApp/index.jsp,<SPAN lang=EN-US><SPAN lang=EN-US>即可看到index</SPAN></SPAN><SPAN lang=EN-US><SPAN lang=EN-US>的内容了</SPAN></SPAN></A>.<o:p></o:p></SPAN></FONT></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'"><FONT size=4><FONT color=#000080><FONT face=Arial>如何创建<SPAN lang=EN-US>JAR</SPAN>包<SPAN lang=EN-US>:<o:p></o:p></SPAN></FONT></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'"><FONT size=4><FONT color=#000080><FONT face=Arial>右击工程<SPAN lang=EN-US>,</SPAN>导出成<SPAN lang=EN-US>JAR<o:p></o:p></SPAN></FONT></FONT></FONT></SPAN></P><img src ="http://www.blogjava.net/Microhelp/aggbug/6655.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/Microhelp/" target="_blank">Microhelp</a> 2005-06-24 10:07 <a href="http://www.blogjava.net/Microhelp/articles/6655.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>一个简单的STRUTS例子</title><link>http://www.blogjava.net/Microhelp/articles/6651.html</link><dc:creator>Microhelp</dc:creator><author>Microhelp</author><pubDate>Fri, 24 Jun 2005 01:38:00 GMT</pubDate><guid>http://www.blogjava.net/Microhelp/articles/6651.html</guid><wfw:comment>http://www.blogjava.net/Microhelp/comments/6651.html</wfw:comment><comments>http://www.blogjava.net/Microhelp/articles/6651.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/Microhelp/comments/commentRss/6651.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/Microhelp/services/trackbacks/6651.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 一个简单的struts例子1、&nbsp; 生成一个新的Web应用，假设我们生成的应用程序的根目录在/Webapps/mystruts目录。2、&nbsp; 到http://jakarta.apache.org/ 下载Struts的安装文件3、&nbsp; 在/Webapps/mystruts目录下新建WEB-INF/lib目录从解压的文件中拷贝下列jar文件到/Webapps/mys...&nbsp;&nbsp;<a href='http://www.blogjava.net/Microhelp/articles/6651.html'>阅读全文</a><img src ="http://www.blogjava.net/Microhelp/aggbug/6651.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/Microhelp/" target="_blank">Microhelp</a> 2005-06-24 09:38 <a href="http://www.blogjava.net/Microhelp/articles/6651.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>如何建立Tomcat的连接池</title><link>http://www.blogjava.net/Microhelp/articles/6650.html</link><dc:creator>Microhelp</dc:creator><author>Microhelp</author><pubDate>Fri, 24 Jun 2005 01:34:00 GMT</pubDate><guid>http://www.blogjava.net/Microhelp/articles/6650.html</guid><wfw:comment>http://www.blogjava.net/Microhelp/comments/6650.html</wfw:comment><comments>http://www.blogjava.net/Microhelp/articles/6650.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/Microhelp/comments/commentRss/6650.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/Microhelp/services/trackbacks/6650.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 如何建立tomcat的连接池&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs...&nbsp;&nbsp;<a href='http://www.blogjava.net/Microhelp/articles/6650.html'>阅读全文</a><img src ="http://www.blogjava.net/Microhelp/aggbug/6650.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/Microhelp/" target="_blank">Microhelp</a> 2005-06-24 09:34 <a href="http://www.blogjava.net/Microhelp/articles/6650.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>ANT的使用</title><link>http://www.blogjava.net/Microhelp/articles/6648.html</link><dc:creator>Microhelp</dc:creator><author>Microhelp</author><pubDate>Fri, 24 Jun 2005 01:23:00 GMT</pubDate><guid>http://www.blogjava.net/Microhelp/articles/6648.html</guid><wfw:comment>http://www.blogjava.net/Microhelp/comments/6648.html</wfw:comment><comments>http://www.blogjava.net/Microhelp/articles/6648.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/Microhelp/comments/commentRss/6648.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/Microhelp/services/trackbacks/6648.html</trackback:ping><description><![CDATA[<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 18pt; TEXT-INDENT: -18pt; mso-list: l0 level1 lfo1; tab-stops: list 18.0pt"><FONT face=Arial><FONT size=4><FONT color=#000080><SPAN lang=EN-US style="FONT-FAMILY: 宋体; mso-bidi-font-family: 宋体"><SPAN style="mso-list: Ignore">1、<SPAN style="FONT: 7pt 'Times New Roman'">&nbsp; </SPAN></SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体">下载<SPAN lang=EN-US>ANT,</SPAN>解压 设置<SPAN lang=EN-US>ANT_HOME</SPAN>为解压好的目录。<SPAN lang=EN-US><?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p></SPAN></SPAN></FONT></FONT></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 18pt; TEXT-INDENT: -18pt; mso-list: l0 level1 lfo1; tab-stops: list 18.0pt"><FONT face=Arial><FONT size=4><FONT color=#000080><SPAN lang=EN-US style="FONT-FAMILY: 宋体; mso-bidi-font-family: 宋体"><SPAN style="mso-list: Ignore">2、<SPAN style="FONT: 7pt 'Times New Roman'">&nbsp; </SPAN></SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体">设置<SPAN lang=EN-US>JAVA_HOME<o:p></o:p></SPAN></SPAN></FONT></FONT></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 18pt; TEXT-INDENT: -18pt; mso-list: l0 level1 lfo1; tab-stops: list 18.0pt"><FONT face=Arial><FONT size=4><FONT color=#000080><SPAN lang=EN-US style="FONT-FAMILY: 宋体; mso-bidi-font-family: 宋体"><SPAN style="mso-list: Ignore">3、<SPAN style="FONT: 7pt 'Times New Roman'">&nbsp; </SPAN></SPAN></SPAN><SPAN style="COLOR: #333333; FONT-FAMILY: 宋体; mso-bidi-font-family: Tahoma; mso-bidi-font-size: 9.0pt">将<SPAN lang=EN-US>bin</SPAN>目录加入<SPAN lang=EN-US>PATH</SPAN>环境变量</SPAN><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><o:p></o:p></SPAN></FONT></FONT></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 18pt; TEXT-INDENT: -18pt; mso-list: l0 level1 lfo1; tab-stops: list 18.0pt"><FONT face=Arial><FONT size=4><FONT color=#000080><SPAN lang=EN-US style="FONT-FAMILY: 宋体; mso-bidi-font-family: 宋体"><SPAN style="mso-list: Ignore">4、<SPAN style="FONT: 7pt 'Times New Roman'">&nbsp; </SPAN></SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体">编写<SPAN lang=EN-US>BUILD.XML</SPAN>文件。<SPAN lang=EN-US><o:p></o:p></SPAN></SPAN></FONT></FONT></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><FONT face=Arial><FONT size=4><FONT color=#000080><SPAN style="mso-spacerun: yes">&nbsp;</SPAN><o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><FONT face=Arial><FONT size=4><FONT color=#000080>&lt;project name="MyProject" default="dist" basedir="."&gt;<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><o:p><FONT face=Arial color=#000080 size=4>&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><FONT face=Arial><FONT size=4><FONT color=#000080><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN>&lt;description&gt;<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><o:p><FONT face=Arial color=#000080 size=4>&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><FONT face=Arial><FONT size=4><FONT color=#000080><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>simple example build file<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><o:p><FONT face=Arial color=#000080 size=4>&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><FONT face=Arial><FONT size=4><FONT color=#000080><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN>&lt;/description&gt;<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><o:p><FONT face=Arial color=#000080 size=4>&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><FONT face=Arial><FONT size=4><FONT color=#000080><SPAN style="mso-spacerun: yes">&nbsp; </SPAN>&lt;!-- set global properties for this build --&gt;<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><FONT face=Arial><FONT size=4><FONT color=#000080><SPAN style="mso-spacerun: yes">&nbsp; </SPAN><o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><FONT face=Arial><FONT size=4><FONT color=#000080><SPAN style="mso-spacerun: yes">&nbsp; </SPAN>&lt;property name="src" location="src"/&gt;<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><o:p><FONT face=Arial color=#000080 size=4>&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><FONT face=Arial><FONT size=4><FONT color=#000080><SPAN style="mso-spacerun: yes">&nbsp; </SPAN>&lt;property name="build" location="build"/&gt;<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><o:p><FONT face=Arial color=#000080 size=4>&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><FONT face=Arial><FONT size=4><FONT color=#000080><SPAN style="mso-spacerun: yes">&nbsp; </SPAN>&lt;property name="dist"<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>location="dist"/&gt;<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><o:p><FONT face=Arial color=#000080 size=4>&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><FONT face=Arial><FONT size=4><FONT color=#000080><SPAN style="mso-spacerun: yes">&nbsp;</SPAN><o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><o:p><FONT face=Arial color=#000080 size=4>&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><FONT face=Arial><FONT size=4><FONT color=#000080><SPAN style="mso-spacerun: yes">&nbsp; </SPAN>&lt;target name="init"&gt;<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><o:p><FONT face=Arial color=#000080 size=4>&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><FONT face=Arial><FONT size=4><FONT color=#000080><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN>&lt;!-- Create the time stamp --&gt;<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><o:p><FONT face=Arial color=#000080 size=4>&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><FONT face=Arial><FONT size=4><FONT color=#000080><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN>&lt;tstamp/&gt;<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><o:p><FONT face=Arial color=#000080 size=4>&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><FONT face=Arial><FONT size=4><FONT color=#000080><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN>&lt;!-- Create the build directory structure used by compile --&gt;<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><o:p><FONT face=Arial color=#000080 size=4>&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><FONT face=Arial><FONT size=4><FONT color=#000080><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN>&lt;mkdir dir="${build}"/&gt;<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><o:p><FONT face=Arial color=#000080 size=4>&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><FONT face=Arial><FONT size=4><FONT color=#000080><SPAN style="mso-spacerun: yes">&nbsp; </SPAN>&lt;/target&gt;<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><o:p><FONT face=Arial color=#000080 size=4>&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><FONT face=Arial><FONT size=4><FONT color=#000080><SPAN style="mso-spacerun: yes">&nbsp;</SPAN><o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><o:p><FONT face=Arial color=#000080 size=4>&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><FONT face=Arial><FONT size=4><FONT color=#000080><SPAN style="mso-spacerun: yes">&nbsp; </SPAN>&lt;target name="compile" depends="init"<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><o:p><FONT face=Arial color=#000080 size=4>&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><FONT face=Arial><FONT size=4><FONT color=#000080><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>description="compile the source " &gt;<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><o:p><FONT face=Arial color=#000080 size=4>&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><FONT face=Arial><FONT size=4><FONT color=#000080><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN>&lt;!-- Compile the java code from ${src} into ${build} --&gt;<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><o:p><FONT face=Arial color=#000080 size=4>&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><FONT face=Arial><FONT size=4><FONT color=#000080><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN>&lt;javac srcdir="${src}" destdir="${build}"/&gt;<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><o:p><FONT face=Arial color=#000080 size=4>&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><FONT face=Arial><FONT size=4><FONT color=#000080><SPAN style="mso-spacerun: yes">&nbsp; </SPAN>&lt;/target&gt;<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><o:p><FONT face=Arial color=#000080 size=4>&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><FONT face=Arial><FONT size=4><FONT color=#000080><SPAN style="mso-spacerun: yes">&nbsp;</SPAN><o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><o:p><FONT face=Arial color=#000080 size=4>&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><FONT face=Arial><FONT size=4><FONT color=#000080><SPAN style="mso-spacerun: yes">&nbsp; </SPAN>&lt;target name="dist" depends="compile"<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><o:p><FONT face=Arial color=#000080 size=4>&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><FONT face=Arial><FONT size=4><FONT color=#000080><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>description="generate the distribution" &gt;<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><o:p><FONT face=Arial color=#000080 size=4>&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><FONT face=Arial><FONT size=4><FONT color=#000080><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN>&lt;!-- Create the distribution directory --&gt;<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><o:p><FONT face=Arial color=#000080 size=4>&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><FONT face=Arial><FONT size=4><FONT color=#000080><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN>&lt;mkdir dir="${dist}/lib"/&gt;<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><o:p><FONT face=Arial color=#000080 size=4>&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><FONT face=Arial><FONT size=4><FONT color=#000080><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN>&lt;!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file --&gt;<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><o:p><FONT face=Arial color=#000080 size=4>&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><FONT face=Arial><FONT size=4><FONT color=#000080><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN>&lt;jar jarfile="${dist}/lib/MyProject-${DSTAMP}.jar" basedir="${build}"/&gt;<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><o:p><FONT face=Arial color=#000080 size=4>&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><FONT face=Arial><FONT size=4><FONT color=#000080><SPAN style="mso-spacerun: yes">&nbsp; </SPAN>&lt;/target&gt;<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><o:p><FONT face=Arial color=#000080 size=4>&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><o:p><FONT face=Arial color=#000080 size=4>&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><FONT face=Arial><FONT size=4><FONT color=#000080><SPAN style="mso-spacerun: yes">&nbsp; </SPAN>&lt;target name="clean"<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><o:p><FONT face=Arial color=#000080 size=4>&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><FONT face=Arial><FONT size=4><FONT color=#000080><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>description="clean up" &gt;<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><o:p><FONT face=Arial color=#000080 size=4>&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><FONT face=Arial><FONT size=4><FONT color=#000080><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN>&lt;!-- Delete the ${build} and ${dist} directory trees --&gt;<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><o:p><FONT face=Arial color=#000080 size=4>&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><FONT face=Arial><FONT size=4><FONT color=#000080><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN>&lt;delete dir="${build}"/&gt;<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><o:p><FONT face=Arial color=#000080 size=4>&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><FONT face=Arial><FONT size=4><FONT color=#000080><SPAN style="mso-spacerun: yes">&nbsp;</SPAN><SPAN style="mso-spacerun: yes">&nbsp;&nbsp; </SPAN>&lt;delete dir="${dist}"/&gt;<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><o:p><FONT face=Arial color=#000080 size=4>&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><FONT face=Arial><FONT size=4><FONT color=#000080><SPAN style="mso-spacerun: yes">&nbsp; </SPAN>&lt;/target&gt;<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><o:p><FONT face=Arial color=#000080 size=4>&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><FONT face=Arial><FONT size=4><FONT color=#000080>&lt;/project&gt;<o:p></o:p></FONT></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-FAMILY: 宋体"><o:p><FONT face=Arial color=#000080 size=4>&nbsp;</FONT></o:p></SPAN></P><img src ="http://www.blogjava.net/Microhelp/aggbug/6648.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/Microhelp/" target="_blank">Microhelp</a> 2005-06-24 09:23 <a href="http://www.blogjava.net/Microhelp/articles/6648.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Jdbc连接Mysql</title><link>http://www.blogjava.net/Microhelp/articles/6647.html</link><dc:creator>Microhelp</dc:creator><author>Microhelp</author><pubDate>Fri, 24 Jun 2005 01:12:00 GMT</pubDate><guid>http://www.blogjava.net/Microhelp/articles/6647.html</guid><wfw:comment>http://www.blogjava.net/Microhelp/comments/6647.html</wfw:comment><comments>http://www.blogjava.net/Microhelp/articles/6647.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/Microhelp/comments/commentRss/6647.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/Microhelp/services/trackbacks/6647.html</trackback:ping><description><![CDATA[<P><FONT face=Arial color=#000080 size=4>安装JDBC驱动MySQL Connector/J 3.1.6</FONT></P>
<P><FONT face=Arial color=#000080 size=4>下载:http://dev.mysql.com/downloads/index.html</FONT></P>
<P><FONT face=Arial color=#000080 size=4>解压后将其中的mysql-connector-java-3.1.6-bin.jar复制到D:\Tomcat 5.5\common\lib或者D:\Java\jdk1.5.0_01\jre\lib\ext或者其他地方(则需要设置classpath指向该路径)</FONT></P>
<P><FONT face=Arial color=#000080 size=4>测试代码如下：</FONT></P>
<P><FONT face=Arial color=#000080 size=4>&lt;%@ page contentType="text/html;charset=GB2312"%&gt;<BR>&lt;%@ page language="java" import="java.sql.*"%&gt;<BR>&lt;%<BR>String mySqlDriver = "org.gjt.mm.mysql.Driver";<BR>String url = "jdbc:mysql://localhost/test";//数据库连接<BR>&nbsp;Connection conn;<BR>&nbsp;Class.forName(mySqlDriver);<BR>&nbsp;conn = DriverManager.getConnection( url,"root","cleversoft");//用户名与密码<BR>&nbsp;if(conn==null){<BR>System.out.println("get Conn Error");<BR>}<BR>Statement stmt=conn.createStatement();<BR>ResultSet rs=null;<BR>rs=stmt.executeQuery("select * from newtable");<BR>while(rs.next())<BR>{//循环显示记录<BR>%&gt;<BR>id:&lt;%=rs.getString("id")%&gt;&nbsp;name1:&lt;%=rs.getString("name1")%&gt;&nbsp;name2:(&lt;%=rs.getString("name2")%&gt;)<BR>&lt;%="&lt;br&gt;"%&gt;<BR>&lt;%<BR>}<BR>rs.close();<BR>stmt.close();<BR>conn.close();<BR>%&gt; </FONT></P><img src ="http://www.blogjava.net/Microhelp/aggbug/6647.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/Microhelp/" target="_blank">Microhelp</a> 2005-06-24 09:12 <a href="http://www.blogjava.net/Microhelp/articles/6647.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Jdbc连接Sqlserver</title><link>http://www.blogjava.net/Microhelp/articles/6646.html</link><dc:creator>Microhelp</dc:creator><author>Microhelp</author><pubDate>Fri, 24 Jun 2005 01:06:00 GMT</pubDate><guid>http://www.blogjava.net/Microhelp/articles/6646.html</guid><wfw:comment>http://www.blogjava.net/Microhelp/comments/6646.html</wfw:comment><comments>http://www.blogjava.net/Microhelp/articles/6646.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/Microhelp/comments/commentRss/6646.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/Microhelp/services/trackbacks/6646.html</trackback:ping><description><![CDATA[<P><BR><FONT color=#000080 size=4>1、下载sqlserver驱动for jdbc<BR>2、安装驱动<BR>3、在C:\Program Files\Microsoft SQL Server 2000 Driver for JDBC下将三个jar文件拷贝到Tomcat\common\lib下<BR></FONT></P>
<P><FONT color=#000080 size=4><BR>测试程序如下：<BR>&lt;%@&nbsp; page&nbsp; contentType="text/html;charset=gb2312"%&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>&lt;%@&nbsp; page&nbsp; import="java.sql.*"%&gt;&nbsp;&nbsp;&nbsp; <BR>&lt;html&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>&lt;body&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>&lt;%Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>String&nbsp; url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs";&nbsp;&nbsp;&nbsp; <BR>//pubs为你的数据库的&nbsp;&nbsp;&nbsp; <BR>String&nbsp; user="sa";&nbsp;&nbsp;&nbsp; <BR>String&nbsp; password="sa";&nbsp;&nbsp;&nbsp; <BR>Connection&nbsp; conn=&nbsp; DriverManager.getConnection(url,user,password);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>Statement&nbsp; stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>String&nbsp; sql="select&nbsp; *&nbsp; from&nbsp; test";&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>ResultSet&nbsp; rs=stmt.executeQuery(sql);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>while(rs.next())&nbsp; {%&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>您的第一个字段内容为：&lt;%=rs.getString(1)%&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>您的第二个字段内容为：&lt;%=rs.getString(2)%&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>&lt;%}%&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>&lt;%out.print("数据库操作成功，恭喜你");%&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>&lt;%rs.close();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>stmt.close();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>conn.close();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>%&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>&lt;/body&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>&lt;/html&gt;&nbsp; </FONT></P><img src ="http://www.blogjava.net/Microhelp/aggbug/6646.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/Microhelp/" target="_blank">Microhelp</a> 2005-06-24 09:06 <a href="http://www.blogjava.net/Microhelp/articles/6646.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>