﻿<?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>IT博客-快乐着飞舞着-随笔分类-Java</title><link>http://www.cnitblog.com/reene/category/899.html</link><description>---Nothing to do  is  doing nothing</description><language>zh-cn</language><lastBuildDate>Mon, 26 Sep 2011 14:14:06 GMT</lastBuildDate><pubDate>Mon, 26 Sep 2011 14:14:06 GMT</pubDate><ttl>60</ttl><item><title>Spring + hibernate 的错误 </title><link>http://www.cnitblog.com/reene/archive/2006/08/08/14952.html</link><dc:creator>快乐着飞舞着</dc:creator><author>快乐着飞舞着</author><pubDate>Tue, 08 Aug 2006 11:00:00 GMT</pubDate><guid>http://www.cnitblog.com/reene/archive/2006/08/08/14952.html</guid><wfw:comment>http://www.cnitblog.com/reene/comments/14952.html</wfw:comment><comments>http://www.cnitblog.com/reene/archive/2006/08/08/14952.html#Feedback</comments><slash:comments>8</slash:comments><wfw:commentRss>http://www.cnitblog.com/reene/comments/commentRss/14952.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/reene/services/trackbacks/14952.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 18				:				55				:				43				,				750				 ERROR [ContextLoaderPlugIn] Context initialization failedorg.springframework.beans.factory.BeanCreationException: Error creating bean with name ...&nbsp;&nbsp;<a href='http://www.cnitblog.com/reene/archive/2006/08/08/14952.html'>阅读全文</a><img src ="http://www.cnitblog.com/reene/aggbug/14952.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/reene/" target="_blank">快乐着飞舞着</a> 2006-08-08 19:00 <a href="http://www.cnitblog.com/reene/archive/2006/08/08/14952.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>一个诡异问题终于得已解决</title><link>http://www.cnitblog.com/reene/archive/2006/08/08/14949.html</link><dc:creator>快乐着飞舞着</dc:creator><author>快乐着飞舞着</author><pubDate>Tue, 08 Aug 2006 09:49:00 GMT</pubDate><guid>http://www.cnitblog.com/reene/archive/2006/08/08/14949.html</guid><wfw:comment>http://www.cnitblog.com/reene/comments/14949.html</wfw:comment><comments>http://www.cnitblog.com/reene/archive/2006/08/08/14949.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cnitblog.com/reene/comments/commentRss/14949.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/reene/services/trackbacks/14949.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 最近在学着在 struts 中使用 spring ,但是在发布到服务器上时总是有如表一的错误 。所有的配置文件 都是正确的，在网上就根本就没有什么解决办法。下载一个例子能发布成功，配置文件基本一致，我仔细比较了两个文件 发现差别就是 发布到服务器上 的war文件是否在/WEB-INF/lib文件夹下包含了struts 的所有类库，我当时觉得问题就是出在这里。把所有的依赖类库复制到jboss ser...&nbsp;&nbsp;<a href='http://www.cnitblog.com/reene/archive/2006/08/08/14949.html'>阅读全文</a><img src ="http://www.cnitblog.com/reene/aggbug/14949.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/reene/" target="_blank">快乐着飞舞着</a> 2006-08-08 17:49 <a href="http://www.cnitblog.com/reene/archive/2006/08/08/14949.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Hibernate 的 主键生成</title><link>http://www.cnitblog.com/reene/archive/2006/08/05/14782.html</link><dc:creator>快乐着飞舞着</dc:creator><author>快乐着飞舞着</author><pubDate>Sat, 05 Aug 2006 13:51:00 GMT</pubDate><guid>http://www.cnitblog.com/reene/archive/2006/08/05/14782.html</guid><wfw:comment>http://www.cnitblog.com/reene/comments/14782.html</wfw:comment><comments>http://www.cnitblog.com/reene/archive/2006/08/05/14782.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/reene/comments/commentRss/14782.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/reene/services/trackbacks/14782.html</trackback:ping><description><![CDATA[Hibernate 的主键生成 中的 uuid.hex本来为什么不输出long 类型的 而要输出一个string 类型的呢？ 总觉得一个varchar2 作为主键怪怪的 。。。<br /><img src ="http://www.cnitblog.com/reene/aggbug/14782.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/reene/" target="_blank">快乐着飞舞着</a> 2006-08-05 21:51 <a href="http://www.cnitblog.com/reene/archive/2006/08/05/14782.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Hibernate 的错误 </title><link>http://www.cnitblog.com/reene/archive/2006/07/23/14094.html</link><dc:creator>快乐着飞舞着</dc:creator><author>快乐着飞舞着</author><pubDate>Sun, 23 Jul 2006 15:21:00 GMT</pubDate><guid>http://www.cnitblog.com/reene/archive/2006/07/23/14094.html</guid><wfw:comment>http://www.cnitblog.com/reene/comments/14094.html</wfw:comment><comments>http://www.cnitblog.com/reene/archive/2006/07/23/14094.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cnitblog.com/reene/comments/commentRss/14094.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/reene/services/trackbacks/14094.html</trackback:ping><description><![CDATA[2006-07-23 23:14:53,796 [main] WARN  org.hibernate.impl.SessionFactoryObjectFactory - Could not bind factory to JNDI<br />javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial<br /> at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:645)<br /> at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:247)<br /> at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:284)<br /> at javax.naming.InitialContext.getNameParser(InitialContext.java:439)<br /> at org.hibernate.util.NamingHelper.bind(NamingHelper.java:52)<br /> at org.hibernate.impl.SessionFactoryObjectFactory.addInstance(SessionFactoryObjectFactory.java:90)<br /> at org.hibernate.impl.SessionFactoryImpl.&lt;init&gt;(SessionFactoryImpl.java:291)<br /> at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1176)<br /> at reene.study.hibernate.test.TestMain.main(TestMain.java:19)<br /><br /><br />疑惑中<br /><br />待解决....<br />在网上找到如下内容：<br />（3）报错：Could not bind factory to JNDI<br />此错误是配置文件的问题，如果hibernate配置文件有session_factory_name这个变量，&lt;session-factory name="foo"&gt; 会试图将一个SessionFactory实例以foo为名bind到jndi上，而有的application container不支持jndi绑定。把这个变量去掉即可。<br /><br />解决问题 <br /><br />把引用代码也要修改：<br />大致改成这样：<br /><br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #008080">1</span> <span style="COLOR: #000000">SessionFactory sf </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> Configuration().configure()<br /></span><span style="COLOR: #008080">2</span> <span style="COLOR: #000000">                    .buildSessionFactory();<br /></span><span style="COLOR: #008080">3</span> <span style="COLOR: #000000">            Session session </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> sf.openSession();<br /></span><span style="COLOR: #008080">4</span> <span style="COLOR: #000000">            Transaction tx </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> session.beginTransaction();</span></div><br /><br /><br /><img src ="http://www.cnitblog.com/reene/aggbug/14094.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/reene/" target="_blank">快乐着飞舞着</a> 2006-07-23 23:21 <a href="http://www.cnitblog.com/reene/archive/2006/07/23/14094.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>"struts中文问题"，"struts国际化问题"??终极解决方案[转载]</title><link>http://www.cnitblog.com/reene/archive/2006/06/13/12193.html</link><dc:creator>快乐着飞舞着</dc:creator><author>快乐着飞舞着</author><pubDate>Tue, 13 Jun 2006 13:32:00 GMT</pubDate><guid>http://www.cnitblog.com/reene/archive/2006/06/13/12193.html</guid><wfw:comment>http://www.cnitblog.com/reene/comments/12193.html</wfw:comment><comments>http://www.cnitblog.com/reene/archive/2006/06/13/12193.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/reene/comments/commentRss/12193.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/reene/services/trackbacks/12193.html</trackback:ping><description><![CDATA[<table class="fixedTable blogpost" cellspacing="0" width="100%" border="0">
				<tbody>
						<tr>
								<td class="ellipse">
										<span class="bvTitle" id="subjcns!F0C1403231EB3BB0!151">
												<strong>引用 自：<br /><a href="http://hellocoffeecat.spaces.msn.com/Blog/cns!F0C1403231EB3BB0!151.entry">http://hellocoffeecat.spaces.msn.com/Blog/cns!F0C1403231EB3BB0!151.entry</a><br /><a href="http://hellocoffeecat.spaces.msn.com/blog/cns!F0C1403231EB3BB0!151.trak">http://hellocoffeecat.spaces.msn.com/blog/cns!F0C1403231EB3BB0!151.trak</a><br /><br /><br />"struts中文问题"，"struts国际化问题"??终极解决方案</strong>
										</span>
								</td>
						</tr>
						<tr>
								<td class="bvh8">
										<strong>
										</strong>
								</td>
						</tr>
						<tr>
								<td id="msgcns!F0C1403231EB3BB0!151">
										<div>Java本身就支持多国语言编码，不需要写任何程序，可以很简单的 <br />实现。 <br />秘诀就是两点： <br /><br />1、所有HTML/JSP页面全部采用UTF-8编码 <br /><br />2、客户端浏览器完全支持UTF-8编码 <br /><br />步骤： <br />1、首先把所有的HTML/JSP的ContentType都设为UTF-8 <br /><br />2、然后对于JSP程序中的非ASCII码提示信息都不应该写在程序里面，都应该放在 <br />application.properties里面统一管理。 <br /><br />3、对HTML用native2ascii工具统一做一次处理，把HTML中的非ASCII码都转换为Unicode编码。 <br /><br />4、针对不同的语言，写不同的application.properties，比如说简体中文是 <br />application_zh_CN.properties，繁体中文是application_zh_TW.properties这样，然后对这些配置信 <br />息文件同样用native2ascii工具处理一次，把非ASCII码统统转为Unicode编码。 <br /><br />5、在Servlet的request.getCharacterEncoding()获得客户端的操作系统默认编码，然后set到Struts <br />的HTTPSession的Locale中。 <br /><br />OK！现在不同的客户访问，就会显示不同的语言版本了。你可以看看此时你的浏览器的字符集，就是 <br />UTF-8。现在你的网站和Google一样了，嘿嘿，其实你有心的话，看看你的浏览器访问Google的时候是 <br />什么字符集吧 <br /><br />切记：所有的HTML/JSP都要设为UTF-8编码，所有的文件中的非ASCII码字符都要用native2ascii工具转 <br />为用ASCII表示的Unicode编码。 <br />－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－<br />－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－<br />原创<br />－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－<br />上面所述是我从网上下的一篇于中文问题的解决方案，确切的说应该是关于Struts的国际化问题，下面我结合我的实践谈谈具体如何实现Struts的国际化问题，我对理论不是非常精通，我只能完全凭自己的理解和实践来讲述，所以下面讲的内容可能不是非常正确，还请大家原谅。但有一点可以肯定，我通过自己的努力解决了Struts的中文问题，并实现Struts的国际化，其实一切并不复杂，下面是具体步骤：<br /><br />0.遇到的问题(这些问题也许不会同时出现)<br />a.中文数据从数据库中到jsp中后就变成了"????"<br />b.做好的中文properties文件，其中的中文value在页面显示乱码<br />c.jsp文件中的中文到浏览器后显示时也是乱码（建议不要在jsp文件中输入中文，尽量放在properties文件中）<br />d.由jsp传给bean的中文值，再由bean传回页面又是乱码<br />e.当更换本地浏览器的语言选项时，Web应用程序不能自动根据你的locale选择合适的*.properties文件。导致Web应用程序不能国际化。<br />1.环境：<br />Web服务器： Tomcat 5.0.19<br />操作系统： Win2000 Server<br />JVM ： jdk 1.4<br />数 据 库： Oracle 8.1.7<br />开发工具： struts studio 5.2 pro for eclipse<br />2.先将所有*.jsp 网页中开头处加入<br /><br />&lt;%@ page language="java" contentType="text/html; charset=utf-8" %&gt;<br />再设置&lt;html:html locale = "true"&gt;<br /><br />3.然后编辑好两个*.properties文件，放在classes文件夹下你指定的地方，这里是放在/web-inf/classes/com/wiley 下，它们分别是：<br /><br />ApplicationResources.properties (英文资源文件)<br />ApplicationResources_zh.properties (中文资源文件)<br />随便用什么工具编写都行啊！<br />4.将ApplicationResources_zh.properties转码成gb2312。上面引文说要转成UTF-8，结果我试了，不行。转成gb2312就行了，操作是。<br />将ApplicationResources_zh.properties更名为ApplicationResources_xx.properties<br />在DOS命令行进入ApplicationResources_xx.properties所在的文件夹<br />使用命令：native2ascii -encoding gb2312 ApplicationResources_xx.properties ApplicationResources_zh.properties(至于你为什么会出现“native2ascii不是内部命令”，，请查其它资料，可能你要设置环境变量，因为他是jdk的文件夹bin下的一个应用程序)<br />5.接下来配置struts-config.xml,很简单，我们加入：<br /><br />&lt;message-resources parameter="com.wiley.ApplicationResources"/&gt; 就行了；<br /><br />到此已能解决大多数中文问题。如上面所说的a,b,e 现在打开浏览器，选择菜单：工具》internet选项》语言，将“中文－中国[zh-cn]”删掉，添加一个“英语－英国[zh-gb]”确定后，重启Tomcat,输入网址你就会发现，你的页面的文本信息就会用的是ApplicationResources.properties (英文资源文件)中的内容。如果换回“中文－中国[zh-cn]”，它就会显示ApplicationResources_zh.properties (中文资源文件)中的中文内容。<br /><br />至于问题“c.jsp文件中的中文到浏览器后显示时也是乱码” 你就要用与第4步类似的方法来重新对*.jsp 文件编码，这时-encoding的参数就要用UTF-8了，如果你用的也是struts studio 5.2 pro for eclipse工具，这一步就免了。它会自动用UTF-8的格式存储。<br />至于问题“d.由jsp传给bean的中文值，再由bean传回页面又是乱码”的解决，我只是加了个过滤器。<br />你可以现在web.xml中加入：<br />&lt;filter&gt;<br />&lt;filter-name&gt;Set Character Encoding&lt;/filter-name&gt;<br />&lt;filter-class&gt;com.wiley.SetCharacterEncodingFilter&lt;/filter-class&gt;<br />&lt;init-param&gt;<br />&lt;param-name&gt;encoding&lt;/param-name&gt;<br />&lt;param-value&gt;utf-8&lt;/param-value&gt; <br />&lt;/init-param&gt;<br />&lt;init-param&gt;<br />&lt;param-name&gt;ignore&lt;/param-name&gt;<br />&lt;param-value&gt;true&lt;/param-value&gt;<br />&lt;/init-param&gt;<br />&lt;/filter&gt;<br />&lt;filter-mapping&gt;<br />&lt;filter-name&gt;Set Character Encoding&lt;/filter-name&gt;<br />&lt;servlet-name&gt;action&lt;/servlet-name&gt;<br />&lt;/filter-mapping&gt;<br /><br />然后在你指定的包内加个java文件 我放在了/web-inf/classes/com/wiley 里，下面是源代码：<br /><br /><br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #008000">/*</span><span style="COLOR: #008000"><br />* XP Forum<br />* <br />* Copyright (c) 2002-2003 RedSoft Group. All rights reserved.<br />*<br /></span><span style="COLOR: #008000">*/</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #0000ff">package</span><span style="COLOR: #000000"> com.huahang.tj.struts.filters;<br /><br /></span><span style="COLOR: #0000ff">import</span><span style="COLOR: #000000"> javax.servlet.</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">;<br /></span><span style="COLOR: #0000ff">import</span><span style="COLOR: #000000"> java.io.IOException;<br /><br /></span><span style="COLOR: #008000">/**</span><span style="COLOR: #008000"><br />* &lt;p&gt;Filter that sets the character encoding to be used in parsing the<br />* incoming request, either unconditionally or only if the client did not<br />* specify a character encoding. Configuration of this filter is based on<br />* the following initialization parameters:&lt;/p&gt;<br />* &lt;ul&gt;<br />* &lt;li&gt;&lt;strong&gt;encoding&lt;/strong&gt; - The character encoding to be configured<br />* for this request, either conditionally or unconditionally based on<br />* the &lt;code&gt;ignore&lt;/code&gt; initialization parameter. This parameter<br />* is required, so there is no default.&lt;/li&gt;<br />* &lt;li&gt;&lt;strong&gt;ignore&lt;/strong&gt; - If set to "true", any character encoding<br />* specified by the client is ignored, and the value returned by the<br />* &lt;code&gt;selectEncoding()&lt;/code&gt; method is set. If set to "false,<br />* &lt;code&gt;selectEncoding()&lt;/code&gt; is called &lt;strong&gt;only&lt;/strong&gt; if the<br />* client has not already specified an encoding. By default, this<br />* parameter is set to "true".&lt;/li&gt;<br />* &lt;/ul&gt;<br />*<br />* &lt;p&gt;Although this filter can be used unchanged, it is also easy to<br />* subclass it and make the &lt;code&gt;selectEncoding()&lt;/code&gt; method more<br />* intelligent about what encoding to choose, based on characteristics of<br />* the incoming request (such as the values of the &lt;code&gt;Accept-Language&lt;/code&gt;<br />* and &lt;code&gt;User-Agent&lt;/code&gt; headers, or a value stashed in the current<br />* user´s session.&lt;/p&gt;<br />*<br />* </span><span style="COLOR: #808080">@author</span><span style="COLOR: #008000"> &lt;a href="mailto:jwtronics@yahoo.com"&gt;John Wong&lt;/a&gt;<br />*<br />* </span><span style="COLOR: #808080">@version</span><span style="COLOR: #008000"> $Id: SetCharacterEncodingFilter.java,v 1.1 2002/04/10 13:59:27 johnwong Exp $<br /></span><span style="COLOR: #008000">*/</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000"> SetCharacterEncodingFilter </span><span style="COLOR: #0000ff">implements</span><span style="COLOR: #000000"> Filter {<br /><br /></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> ----------------------------------------------------- Instance Variables</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000"><br /><br /></span><span style="COLOR: #008000">/**</span><span style="COLOR: #008000"><br />* The default character encoding to set for requests that pass through<br />* this filter.<br /></span><span style="COLOR: #008000">*/</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #0000ff">protected</span><span style="COLOR: #000000"> String encoding </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">null</span><span style="COLOR: #000000">;<br /><br /><br /></span><span style="COLOR: #008000">/**</span><span style="COLOR: #008000"><br />* The filter configuration object we are associated with. If this value<br />* is null, this filter instance is not currently configured.<br /></span><span style="COLOR: #008000">*/</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #0000ff">protected</span><span style="COLOR: #000000"> FilterConfig filterConfig </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">null</span><span style="COLOR: #000000">;<br /><br /><br /></span><span style="COLOR: #008000">/**</span><span style="COLOR: #008000"><br />* Should a character encoding specified by the client be ignored?<br /></span><span style="COLOR: #008000">*/</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #0000ff">protected</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">boolean</span><span style="COLOR: #000000"> ignore </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">true</span><span style="COLOR: #000000">;<br /><br /><br /></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> --------------------------------------------------------- Public Methods</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000"><br /><br /></span><span style="COLOR: #008000">/**</span><span style="COLOR: #008000"><br />* Take this filter out of service.<br /></span><span style="COLOR: #008000">*/</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000"> destroy() {<br /><br /></span><span style="COLOR: #0000ff">this</span><span style="COLOR: #000000">.encoding </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">null</span><span style="COLOR: #000000">;<br /></span><span style="COLOR: #0000ff">this</span><span style="COLOR: #000000">.filterConfig </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">null</span><span style="COLOR: #000000">;<br /><br />}<br /><br /><br /></span><span style="COLOR: #008000">/**</span><span style="COLOR: #008000"><br />* Select and set (if specified) the character encoding to be used to<br />* interpret request parameters for this request.<br />*<br />* </span><span style="COLOR: #808080">@param</span><span style="COLOR: #008000"> request The servlet request we are processing<br />* </span><span style="COLOR: #808080">@param</span><span style="COLOR: #008000"> result The servlet response we are creating<br />* </span><span style="COLOR: #808080">@param</span><span style="COLOR: #008000"> chain The filter chain we are processing<br />*<br />* </span><span style="COLOR: #808080">@exception</span><span style="COLOR: #008000"> IOException if an input/output error occurs<br />* </span><span style="COLOR: #808080">@exception</span><span style="COLOR: #008000"> ServletException if a servlet error occurs<br /></span><span style="COLOR: #008000">*/</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000"> doFilter(ServletRequest request, ServletResponse response,<br />FilterChain chain)<br /></span><span style="COLOR: #0000ff">throws</span><span style="COLOR: #000000"> IOException, ServletException {<br /><br /></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> Conditionally select and set the character encoding to be used</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000"> (ignore </span><span style="COLOR: #000000">||</span><span style="COLOR: #000000"> (request.getCharacterEncoding() </span><span style="COLOR: #000000">==</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">null</span><span style="COLOR: #000000">)) {<br />String encoding </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> selectEncoding(request);<br /></span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000"> (encoding </span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">null</span><span style="COLOR: #000000">)<br />request.setCharacterEncoding(encoding);<br />}<br /><br /></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> Pass control on to the next filter</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000">chain.doFilter(request, response);<br /><br />}<br /><br /><br /></span><span style="COLOR: #008000">/**</span><span style="COLOR: #008000"><br />* Place this filter into service.<br />*<br />* </span><span style="COLOR: #808080">@param</span><span style="COLOR: #008000"> filterConfig The filter configuration object<br /></span><span style="COLOR: #008000">*/</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000"> init(FilterConfig filterConfig) </span><span style="COLOR: #0000ff">throws</span><span style="COLOR: #000000"> ServletException {<br /><br /></span><span style="COLOR: #0000ff">this</span><span style="COLOR: #000000">.filterConfig </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> filterConfig;<br /></span><span style="COLOR: #0000ff">this</span><span style="COLOR: #000000">.encoding </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> filterConfig.getInitParameter(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">encoding</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br />String value </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> filterConfig.getInitParameter(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">ignore</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br /></span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000"> (value </span><span style="COLOR: #000000">==</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">null</span><span style="COLOR: #000000">)<br /></span><span style="COLOR: #0000ff">this</span><span style="COLOR: #000000">.ignore </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">true</span><span style="COLOR: #000000">;<br /></span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000"> (value.equalsIgnoreCase(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">true</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">))<br /></span><span style="COLOR: #0000ff">this</span><span style="COLOR: #000000">.ignore </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">true</span><span style="COLOR: #000000">;<br /></span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000"> (value.equalsIgnoreCase(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">yes</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">))<br /></span><span style="COLOR: #0000ff">this</span><span style="COLOR: #000000">.ignore </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">true</span><span style="COLOR: #000000">;<br /></span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #0000ff">this</span><span style="COLOR: #000000">.ignore </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">false</span><span style="COLOR: #000000">;<br /><br />}<br /><br /><br /></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> ------------------------------------------------------ Protected Methods</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000"><br /><br /></span><span style="COLOR: #008000">/**</span><span style="COLOR: #008000"><br />* Select an appropriate character encoding to be used, based on the<br />* characteristics of the current request and/or filter initialization<br />* parameters. If no character encoding should be set, return<br />* &lt;code&gt;null&lt;/code&gt;.<br />* &lt;p&gt;<br />* The default implementation unconditionally returns the value configured<br />* by the &lt;strong&gt;encoding&lt;/strong&gt; initialization parameter for this<br />* filter.<br />*<br />* </span><span style="COLOR: #808080">@param</span><span style="COLOR: #008000"> request The servlet request we are processing<br /></span><span style="COLOR: #008000">*/</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #0000ff">protected</span><span style="COLOR: #000000"> String selectEncoding(ServletRequest request) {<br /><br /></span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> (</span><span style="COLOR: #0000ff">this</span><span style="COLOR: #000000">.encoding);<br /><br />}<br /><br />}</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">EOC<br /></span></div><br />到此我遇到的中文问题已全部得到解决，并从中理解到struts的国际化的深刻含义。<br />我个人觉得struts作为一个功能强大的应用框架，应该早就考虑到它的国际化问题，并在实际应用中不会很复杂，只要我们遵循一些规则，就可以尽情享受struts给我们带来的无穷乐趣。希望以上所述对大家有所帮助。</div>
								</td>
						</tr>
						<tr>
								<td>
										<table cellspacing="0" border="0">
												<tbody>
												</tbody>
										</table>
								</td>
						</tr>
				</tbody>
		</table><img src ="http://www.cnitblog.com/reene/aggbug/12193.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/reene/" target="_blank">快乐着飞舞着</a> 2006-06-13 21:32 <a href="http://www.cnitblog.com/reene/archive/2006/06/13/12193.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>应用HttpClient来对付各种顽固的WEB服务器(3) [转]</title><link>http://www.cnitblog.com/reene/archive/2005/10/09/3173.html</link><dc:creator>快乐着飞舞着</dc:creator><author>快乐着飞舞着</author><pubDate>Sun, 09 Oct 2005 05:07:00 GMT</pubDate><guid>http://www.cnitblog.com/reene/archive/2005/10/09/3173.html</guid><wfw:comment>http://www.cnitblog.com/reene/comments/3173.html</wfw:comment><comments>http://www.cnitblog.com/reene/archive/2005/10/09/3173.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/reene/comments/commentRss/3173.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/reene/services/trackbacks/3173.html</trackback:ping><description><![CDATA[<TABLE style="BORDER-RIGHT: #6595d6 1px dotted; TABLE-LAYOUT: fixed; BORDER-TOP: #6595d6 1px dotted; FONT-SIZE: 12px; BORDER-LEFT: #6595d6 1px dotted; BORDER-BOTTOM: #6595d6 1px dotted" cellSpacing=0 cellPadding=6 width="95%" align=center border=0>
<TBODY>
<TR>
<TD style="WORD-WRAP: break-word" bgColor=#e8f4ff>
<P>4． 模拟输入用户名和口令进行登录</P>
<P>本小节应该说是HTTP客户端编程中最常碰见的问题，很多网站的内容都只是对注册用户可见的，这种情况下就必须要求使用正确的用户名和口令登录成功后，方可浏览到想要的页面。因为HTTP协议是无状态的，也就是连接的有效期只限于当前请求，请求内容结束后连接就关闭了。在这种情况下为了保存用户的登录信息必须使用到Cookie机制。以JSP/Servlet为例，当浏览器请求一个JSP或者是Servlet的页面时，应用服务器会返回一个参数，名为jsessionid（因不同应用服务器而异），值是一个较长的唯一字符串的Cookie，这个字符串值也就是当前访问该站点的会话标识。浏览器在每访问该站点的其他页面时候都要带上jsessionid这样的Cookie信息，应用服务器根据读取这个会话标识来获取对应的会话信息。</P>
<P>对于需要用户登录的网站，一般在用户登录成功后会将用户资料保存在服务器的会话中，这样当访问到其他的页面时候，应用服务器根据浏览器送上的Cookie中读取当前请求对应的会话标识以获得对应的会话信息，然后就可以判断用户资料是否存在于会话信息中，如果存在则允许访问页面，否则跳转到登录页面中要求用户输入帐号和口令进行登录。这就是一般使用JSP开发网站在处理用户登录的比较通用的方法。</P>
<P>这样一来，对于HTTP的客户端来讲，如果要访问一个受保护的页面时就必须模拟浏览器所做的工作，首先就是请求登录页面，然后读取Cookie值；再次请求登录页面并加入登录页所需的每个参数；最后就是请求最终所需的页面。当然在除第一次请求外其他的请求都需要附带上Cookie信息以便服务器能判断当前请求是否已经通过验证。说了这么多，可是如果你使用httpclient的话，你甚至连一行代码都无需增加，你只需要先传递登录信息执行登录过程，然后直接访问想要的页面，跟访问一个普通的页面没有任何区别，因为类HttpClient已经帮你做了所有该做的事情了，太棒了！下面的例子实现了这样一个访问的过程。</P>
<P><BR>/*</P>
<P>&nbsp;* Created on 2003-12-7 by Liudong</P>
<P>&nbsp;*/</P>
<P>package http.demo;</P>
<P>&nbsp;</P>
<P>import org.apache.commons.httpclient.*;</P>
<P>import org.apache.commons.httpclient.cookie.*;</P>
<P>import org.apache.commons.httpclient.methods.*;</P>
<P>&nbsp;</P>
<P>/**</P>
<P>&nbsp;* 用来演示登录表单的示例</P>
<P>&nbsp;* @author Liudong</P>
<P>&nbsp;*/</P>
<P>public class FormLoginDemo {</P>
<P>&nbsp;</P>
<P>&nbsp;&nbsp;&nbsp; static final String LOGON_SITE = "localhost";</P>
<P>&nbsp;&nbsp;&nbsp; static final int&nbsp;&nbsp;&nbsp; LOGON_PORT = 8080;</P>
<P>&nbsp;&nbsp;&nbsp; </P>
<P>&nbsp;&nbsp;&nbsp; public static void main(String[] args) throws Exception{</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; HttpClient client = new HttpClient();</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; client.getHostConfiguration().setHost(LOGON_SITE, LOGON_PORT);</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //模拟登录页面login.jsp-&gt;main.jsp</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PostMethod post = new PostMethod("/main.jsp");</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; NameValuePair name = new NameValuePair("name", "ld");&nbsp;&nbsp;&nbsp;&nbsp; </P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; NameValuePair pass = new NameValuePair("password", "ld");&nbsp;&nbsp;&nbsp;&nbsp; </P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; post.setRequestBody(new NameValuePair[]{name,pass});</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int status = client.executeMethod(post);</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(post.getResponseBodyAsString());</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; post.releaseConnection();&nbsp; </P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //查看cookie信息</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CookieSpec cookiespec = CookiePolicy.getDefaultSpec();</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Cookie[] cookies = cookiespec.match(LOGON_SITE, LOGON_PORT, "/", false, client.getState().getCookies());</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (cookies.length == 0) {</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("None");&nbsp;&nbsp;&nbsp; </P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } else {</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int i = 0; i &lt; cookies.length; i++) {</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(cookies[i].toString());&nbsp;&nbsp;&nbsp; </P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //访问所需的页面main2.jsp</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; GetMethod get = new GetMethod("/main2.jsp");</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; client.executeMethod(get);</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(get.getResponseBodyAsString());</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; get.releaseConnection();</P>
<P>&nbsp;&nbsp;&nbsp; }</P>
<P>}</P>
<P>5． 提交XML格式参数</P>
<P>提交XML格式的参数很简单，仅仅是一个提交时候的ContentType问题，下面的例子演示从文件文件中读取XML信息并提交给服务器的过程，该过程可以用来测试Web服务。</P>
<P>import java.io.File;</P>
<P>import java.io.FileInputStream;</P>
<P>&nbsp;</P>
<P>import org.apache.commons.httpclient.HttpClient;</P>
<P>import org.apache.commons.httpclient.methods.EntityEnclosingMethod;</P>
<P>import org.apache.commons.httpclient.methods.PostMethod;</P>
<P>&nbsp;</P>
<P>/**</P>
<P>&nbsp;* 用来演示提交XML格式数据的例子</P>
<P>&nbsp;*/</P>
<P>public class PostXMLClient {</P>
<P>&nbsp;</P>
<P>&nbsp;&nbsp;&nbsp; public static void main(String[] args) throws Exception {</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; File input = new File(“test.xml”);</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PostMethod post = new PostMethod(“http://localhost:8080/httpclient/xml.jsp”);</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 设置请求的内容直接从文件中读取</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; post.setRequestBody(new FileInputStream(input));</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (input.length() &lt; Integer.MAX_VALUE) </P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; post.setRequestContentLength(input.length());</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; post.setRequestContentLength(EntityEnclosingMethod.CONTENT_LENGTH_CHUNKED);</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 指定请求内容的类型</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; post.setRequestHeader("Content-type", "text/xml; charset=GBK");</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; HttpClient httpclient = new HttpClient(); </P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int result = httpclient.executeMethod(post); </P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("Response status code: " + result);</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("Response body: ");</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(post.getResponseBodyAsString());</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; post.releaseConnection();</P>
<P>&nbsp;&nbsp;&nbsp; }</P>
<P>}</P>
<P>&nbsp;</P></TD></TR></TBODY></TABLE>	(<a href=http://www.blogcn.com/user28/reene/blog/22216733.html target=_blank>浏览全文</a>&nbsp; <a href=http://www.blogcn.com/blog/trackback.asp?mydiary=22216733 target=_blank>引用此文</a>)<br>文章来源:<a href='http://www.blogcn.com/user28/reene/blog/22216733.html'>http://www.blogcn.com/user28/reene/blog/22216733.html</a><img src ="http://www.cnitblog.com/reene/aggbug/3173.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/reene/" target="_blank">快乐着飞舞着</a> 2005-10-09 13:07 <a href="http://www.cnitblog.com/reene/archive/2005/10/09/3173.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>应用HttpClient来对付各种顽固的WEB服务器(2) [转]</title><link>http://www.cnitblog.com/reene/archive/2005/10/09/3174.html</link><dc:creator>快乐着飞舞着</dc:creator><author>快乐着飞舞着</author><pubDate>Sun, 09 Oct 2005 05:07:00 GMT</pubDate><guid>http://www.cnitblog.com/reene/archive/2005/10/09/3174.html</guid><wfw:comment>http://www.cnitblog.com/reene/comments/3174.html</wfw:comment><comments>http://www.cnitblog.com/reene/archive/2005/10/09/3174.html#Feedback</comments><slash:comments>5</slash:comments><wfw:commentRss>http://www.cnitblog.com/reene/comments/commentRss/3174.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/reene/services/trackbacks/3174.html</trackback:ping><description><![CDATA[<TABLE style="BORDER-RIGHT: #6595d6 1px dotted; TABLE-LAYOUT: fixed; BORDER-TOP: #6595d6 1px dotted; FONT-SIZE: 12px; BORDER-LEFT: #6595d6 1px dotted; BORDER-BOTTOM: #6595d6 1px dotted" cellSpacing=0 cellPadding=6 width="95%" align=center border=0>
<TBODY>
<TR>
<TD style="WORD-WRAP: break-word" bgColor=#e8f4ff>
<P><FONT face="Courier New" size=2>2． 以GET或者POST方式向网页提交参数</FONT></P>
<P><FONT face="Courier New" size=2>其实前面一个最简单的示例中我们已经介绍了如何使用GET或者POST方式来请求一个页面，本小节与之不同的是多了提交时设定页面所需的参数，我们知道如果是GET的请求方式，那么所有参数都直接放到页面的URL后面用问号与页面地址隔开，每个参数用&amp;隔开，例如：<A href="http://java.sun.com?name=liudong&amp;mobile=123456">http://java.sun.com?name=liudong&amp;mobile=123456</A>，但是当使用POST方法时就会稍微有一点点麻烦。本小节的例子演示向如何查询手机号码所在的城市，代码如下：</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;</FONT></P>
<P><FONT face="Courier New" size=2>/*</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;* Created on 2003-12-7 by Liudong</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;*/</FONT></P>
<P><FONT face="Courier New" size=2>package http.demo;</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;</FONT></P>
<P><FONT face="Courier New" size=2>import java.io.IOException;</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;</FONT></P>
<P><FONT face="Courier New" size=2>import org.apache.commons.httpclient.*;</FONT></P>
<P><FONT face="Courier New" size=2>import org.apache.commons.httpclient.methods.*;</FONT></P>
<P><FONT face="Courier New" size=2>/**</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;* 提交参数演示</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;* 该程序连接到一个用于查询手机号码所属地的页面</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;* 以便查询号码段1330227所在的省份以及城市</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;* @author Liudong</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;*/</FONT></P>
<P><FONT face="Courier New" size=2>public class SimpleHttpClient {</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;&nbsp;&nbsp; public static void main(String[] args) throws IOException</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;&nbsp;&nbsp; {</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; HttpClient client = new HttpClient();</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; client.getHostConfiguration().setHost("<A href="http://www.imobile.com.cn">www.imobile.com.cn</A>", 80, "http");</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; HttpMethod method = getPostMethod();//使用POST方式提交数据</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; client.executeMethod(method);</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //打印服务器返回的状态</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(method.getStatusLine());</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //打印结果页面</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String response =</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; new String(method.getResponseBodyAsString().getBytes("8859_1"));</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //打印返回的信息</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(response);</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; method.releaseConnection();</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;&nbsp;&nbsp; }</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;&nbsp;&nbsp; /**</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;&nbsp;&nbsp;&nbsp; * 使用GET方式提交数据</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;&nbsp;&nbsp;&nbsp; * @return</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;&nbsp;&nbsp;&nbsp; */</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;&nbsp;&nbsp; private static HttpMethod getGetMethod(){</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return new GetMethod("/simcard.php?simcard=1330227");</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;&nbsp;&nbsp; }</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;&nbsp;&nbsp; /**</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;&nbsp;&nbsp;&nbsp; * 使用POST方式提交数据</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;&nbsp;&nbsp;&nbsp; * @return</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;&nbsp;&nbsp;&nbsp; */</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;&nbsp;&nbsp; private static HttpMethod getPostMethod(){</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PostMethod post = new PostMethod("/simcard.php");</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; NameValuePair simcard = new NameValuePair("simcard","1330227");</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; post.setRequestBody(new NameValuePair[] { simcard});</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return post;</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;&nbsp;&nbsp; }</FONT></P>
<P><FONT face="Courier New" size=2>}</FONT></P>
<P><FONT face="Courier New" size=2>在上面的例子中页面<A href="http://www.imobile.com.cn/simcard.php">http://www.imobile.com.cn/simcard.php</A>需要一个参数是simcard，这个参数值为手机号码段，即手机号码的前七位，服务器会返回提交的手机号码对应的省份、城市以及其他详细信息。GET的提交方法只需要在URL后加入参数信息，而POST则需要通过NameValuePair类来设置参数名称和它所对应的值</FONT></P>
<P><FONT face="Courier New" size=2>3． 处理页面重定向</FONT></P>
<P><FONT face="Courier New" size=2>在JSP/Servlet编程中response.sendRedirect方法就是使用HTTP协议中的重定向机制。它与JSP中的&lt;jsp:forward …&gt;的区别在于后者是在服务器中实现页面的跳转，也就是说应用容器加载了所要跳转的页面的内容并返回给客户端；而前者是返回一个状态码，这些状态码的可能值见下表，然后客户端读取需要跳转到的页面的URL并重新加载新的页面。就是这样一个过程，所以我们编程的时候就要通过HttpMethod.getStatusCode()方法判断返回值是否为下表中的某个值来判断是否需要跳转。如果已经确认需要进行页面跳转了，那么可以通过读取HTTP头中的location属性来获取新的地址。</FONT></P>
<P><FONT face="Courier New" size=2>状态码<BR>&nbsp;对应HttpServletResponse的常量<BR>&nbsp;详细描述<BR>&nbsp;<BR>301<BR>&nbsp;SC_MOVED_PERMANENTLY<BR>&nbsp;页面已经永久移到另外一个新地址<BR>&nbsp;<BR>302<BR>&nbsp;SC_MOVED_TEMPORARILY<BR>&nbsp;页面暂时移动到另外一个新的地址<BR>&nbsp;<BR>303<BR>&nbsp;SC_SEE_OTHER<BR>&nbsp;客户端请求的地址必须通过另外的URL来访问<BR>&nbsp;<BR>307<BR>&nbsp;SC_TEMPORARY_REDIRECT<BR>&nbsp;同SC_MOVED_TEMPORARILY<BR>&nbsp;</FONT></P><FONT face="Courier New" size=2>
<P><BR>下面的代码片段演示如何处理页面的重定向</P>
<P>client.executeMethod(post);</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(post.getStatusLine().toString()); </P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; post.releaseConnection();</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //检查是否重定向</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int statuscode = post.getStatusCode();</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if ((statuscode == HttpStatus.SC_MOVED_TEMPORARILY) ||</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (statuscode == HttpStatus.SC_MOVED_PERMANENTLY) ||</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (statuscode == HttpStatus.SC_SEE_OTHER) ||</P>
<P>(statuscode == HttpStatus.SC_TEMPORARY_REDIRECT)) {</P>
<P>//读取新的URL地址</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Header header = post.getResponseHeader("location");</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (header != null) {</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String newuri = header.getValue();</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if ((newuri == null) || (newuri.equals("")))</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; newuri = "/"; </P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; GetMethod redirect = new GetMethod(newuri);</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; client.executeMethod(redirect);</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("Redirect:"+ redirect.getStatusLine().toString()); </P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; redirect.releaseConnection();</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } else </P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("Invalid redirect");</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</P>
<P>我们可以自行编写两个JSP页面，其中一个页面用response.sendRedirect方法重定向到另外一个页面用来测试上面的例子。<BR></FONT></P>
<P><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-highlight: white; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA; mso-fareast-font-family: 宋体"></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-highlight: white; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA; mso-fareast-font-family: 宋体"></P></SPAN></TD></TR></TBODY></TABLE>	(<a href=http://www.blogcn.com/user28/reene/blog/22216676.html target=_blank>浏览全文</a>&nbsp; <a href=http://www.blogcn.com/blog/trackback.asp?mydiary=22216676 target=_blank>引用此文</a>)<br>文章来源:<a href='http://www.blogcn.com/user28/reene/blog/22216676.html'>http://www.blogcn.com/user28/reene/blog/22216676.html</a><img src ="http://www.cnitblog.com/reene/aggbug/3174.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/reene/" target="_blank">快乐着飞舞着</a> 2005-10-09 13:07 <a href="http://www.cnitblog.com/reene/archive/2005/10/09/3174.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>应用HttpClient来对付各种顽固的WEB服务器(1)   [转]</title><link>http://www.cnitblog.com/reene/archive/2005/10/09/3175.html</link><dc:creator>快乐着飞舞着</dc:creator><author>快乐着飞舞着</author><pubDate>Sun, 09 Oct 2005 05:07:00 GMT</pubDate><guid>http://www.cnitblog.com/reene/archive/2005/10/09/3175.html</guid><wfw:comment>http://www.cnitblog.com/reene/comments/3175.html</wfw:comment><comments>http://www.cnitblog.com/reene/archive/2005/10/09/3175.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/reene/comments/commentRss/3175.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/reene/services/trackbacks/3175.html</trackback:ping><description><![CDATA[<TABLE style="BORDER-RIGHT: #6595d6 1px dotted; TABLE-LAYOUT: fixed; BORDER-TOP: #6595d6 1px dotted; FONT-SIZE: 12px; BORDER-LEFT: #6595d6 1px dotted; BORDER-BOTTOM: #6595d6 1px dotted" cellSpacing=0 cellPadding=6 width="95%" align=center border=0>
<TBODY>
<TR>
<TD style="WORD-WRAP: break-word" bgColor=#e8f4ff>
<P>一般的情况下我们都是使用IE或者Navigator浏览器来访问一个WEB服务器，用来浏览页面查看信息或者提交一些数据等等。所访问的这些页面有的仅仅是一些普通的页面，有的需要用户登录后方可使用，或者需要认证以及是一些通过加密方式传输，例如HTTPS。目前我们使用的浏览器处理这些情况都不会构成问题。不过你可能在某些时候需要通过程序来访问这样的一些页面，比如从别人的网页中“偷”一些数据；利用某些站点提供的页面来完成某种功能，例如说我们想知道某个手机号码的归属地而我们自己又没有这样的数据，因此只好借助其他公司已有的网站来完成这个功能，这个时候我们需要向网页提交手机号码并从返回的页面中解析出我们想要的数据来。如果对方仅仅是一个很简单的页面，那我们的程序会很简单，本文也就没有必要大张旗鼓的在这里浪费口舌。但是考虑到一些服务授权的问题，很多公司提供的页面往往并不是可以通过一个简单的URL就可以访问的，而必须经过注册然后登录后方可使用提供服务的页面，这个时候就涉及到COOKIE问题的处理。我们知道目前流行的动态网页技术例如ASP、JSP无不是通过COOKIE来处理会话信息的。为了使我们的程序能使用别人所提供的服务页面，就要求程序首先登录后再访问服务页面，这过程就需要自行处理cookie，想想当你用java.net.HttpURLConnection来完成这些功能时是多么恐怖的事情啊！况且这仅仅是我们所说的顽固的WEB服务器中的一个很常见的“顽固”！再有如通过HTTP来上传文件呢？不需要头疼，这些问题有了“它”就很容易解决了！ </P>
<P></P>
<P>我们不可能列举所有可能的顽固，我们会针对几种最常见的问题进行处理。当然了，正如前面说到的，如果我们自己使用java.net.HttpURLConnection来搞定这些问题是很恐怖的事情，因此在开始之前我们先要介绍一下一个开放源码的项目，这个项目就是Apache开源组织中的httpclient，它隶属于Jakarta的commons项目，目前的版本是2.0RC2。commons下本来已经有一个net的子项目，但是又把httpclient单独提出来，可见http服务器的访问绝非易事。</P>
<P>Commons-httpclient项目就是专门设计来简化HTTP客户端与服务器进行各种通讯编程。通过它可以让原来很头疼的事情现在轻松的解决，例如你不再管是HTTP或者HTTPS的通讯方式，告诉它你想使用HTTPS方式，剩下的事情交给httpclient替你完成。本文会针对我们在编写HTTP客户端程序时经常碰到的几个问题进行分别介绍如何使用httpclient来解决它们，为了让读者更快的熟悉这个项目我们最开始先给出一个简单的例子来读取一个网页的内容，然后循序渐进解决掉前进中的所有问题。</P>
<P>1．&nbsp;读取网页(HTTP/HTTPS)内容</P>
<P>下面是我们给出的一个简单的例子用来访问某个页面</P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #3f7f5f; 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"><?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #3f7f5f; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-spacerun: yes">&nbsp;</SPAN>* Created on 2003-12-14 by Liudong</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #3f7f5f; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-spacerun: yes">&nbsp;</SPAN>*/</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><B><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #7f0055; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white">package</SPAN></B><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"> <SPAN style="COLOR: black">http.demo;</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt">&nbsp;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><B><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #7f0055; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white">import</SPAN></B><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"> <SPAN style="COLOR: black">java.io.IOException;</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt">&nbsp;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><B><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #7f0055; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white">import</SPAN></B><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"> <SPAN style="COLOR: black">org.apache.commons.httpclient.*;</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><B><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #7f0055; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white">import</SPAN></B><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"> <SPAN style="COLOR: black">org.apache.commons.httpclient.methods.*;</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #3f5fbf; 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></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-spacerun: yes">&nbsp;</SPAN><SPAN style="COLOR: #3f5fbf">*</SPAN> </SPAN><SPAN style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #3f5fbf; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-highlight: white; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'">最简单的</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #3f5fbf; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white">HTTP</SPAN><SPAN style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #3f5fbf; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-highlight: white; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'">客户端</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #3f5fbf; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white">,</SPAN><SPAN style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #3f5fbf; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-highlight: white; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'">用来演示通过</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #3f5fbf; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white">GET</SPAN><SPAN style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #3f5fbf; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-highlight: white; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'">或者</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #3f5fbf; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white">POST</SPAN><SPAN style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #3f5fbf; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-highlight: white; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'">方式访问某个页面</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-spacerun: yes">&nbsp;</SPAN><SPAN style="COLOR: #3f5fbf">*</SPAN> <B><SPAN style="COLOR: #7f9fbf">@author</SPAN></B> <SPAN style="COLOR: #3f5fbf">Liudong</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-spacerun: yes">&nbsp;</SPAN><SPAN style="COLOR: #3f5fbf">*/</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><B><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #7f0055; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white">public</SPAN></B><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"> <B><SPAN style="COLOR: #7f0055">class</SPAN></B> <SPAN style="COLOR: black">SimpleClient</SPAN> <SPAN style="COLOR: black">{</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt">&nbsp;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><B><SPAN style="COLOR: #7f0055">public</SPAN></B> <B><SPAN style="COLOR: #7f0055">static</SPAN></B> <B><SPAN style="COLOR: #7f0055">void</SPAN></B> <SPAN style="COLOR: black">main(String[]</SPAN> <SPAN style="COLOR: black">args)</SPAN> <B><SPAN style="COLOR: #7f0055">throws</SPAN></B> <SPAN style="COLOR: black">IOException</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: black">{</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: black">HttpClient</SPAN> <SPAN style="COLOR: black">client</SPAN> <SPAN style="COLOR: black">=</SPAN> <B><SPAN style="COLOR: #7f0055">new</SPAN></B> <SPAN style="COLOR: black">HttpClient();</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-tab-count: 2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: #3f7f5f">//</SPAN></SPAN><SPAN style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #3f7f5f; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-highlight: white; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'">设置代理服务器地址和端口</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-tab-count: 1">&nbsp;</SPAN><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-tab-count: 1"></SPAN><SPAN style="COLOR: #3f7f5f">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //client.getHostConfiguration().setProxy("proxy_host_addr",proxy_port);</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #3f7f5f; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN>//</SPAN><SPAN style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #3f7f5f; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-highlight: white; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'">使用</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #3f7f5f; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white">GET</SPAN><SPAN style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #3f7f5f; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-highlight: white; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'">方法</SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: #3f7f5f; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'">，如果服务器需要通过</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: #3f7f5f; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt">HTTPS</SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: #3f7f5f; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'">连接，那只需要将下面</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: #3f7f5f; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt">URL</SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: #3f7f5f; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'">中的</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: #3f7f5f; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt">http</SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: #3f7f5f; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'">换成</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: #3f7f5f; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt">https</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: black">HttpMethod</SPAN> <SPAN style="COLOR: black">method</SPAN> <SPAN style="COLOR: black">=</SPAN> <B><SPAN style="COLOR: #7f0055">new</SPAN></B> <SPAN style="COLOR: black">GetMethod(</SPAN><SPAN style="COLOR: #2a00ff">"http://java.sun.com"</SPAN><SPAN style="COLOR: black">);</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"> <o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="BACKGROUND: white; COLOR: #3f7f5f; mso-highlight: white">//</SPAN></SPAN><SPAN style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #3f7f5f; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-highlight: white; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'">使用</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #3f7f5f; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white">POST</SPAN><SPAN style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #3f7f5f; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-highlight: white; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'">方法</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: #3f7f5f">//HttpMethod method = new PostMethod("http://java.sun.com");</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"> <o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: black">client.executeMethod(method);</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-tab-count: 2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: #3f7f5f">//</SPAN></SPAN><SPAN style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #3f7f5f; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-highlight: white; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'">打印服务器返回的状态</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: black">System.out.println(method.getStatusLine());</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-tab-count: 2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: #3f7f5f">//</SPAN></SPAN><SPAN style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #3f7f5f; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-highlight: white; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'">打印返回的信息</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: black">System.out.println(method.getResponseBodyAsString());</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-tab-count: 2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: #3f7f5f">//</SPAN></SPAN><SPAN style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #3f7f5f; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-highlight: white; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'">释放连接</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: black">method.releaseConnection();</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: black">}<BR></SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-highlight: white; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA; mso-fareast-font-family: 宋体">}</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-highlight: white; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA; mso-fareast-font-family: 宋体"></SPAN>&nbsp;</P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-highlight: white; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA; mso-fareast-font-family: 宋体">在这个例子中首先创建一个HTTP客户端(HttpClient)的实例，然后选择提交的方法是GET或者POST，最后在HttpClient实例上执行提交的方法，最后从所选择的提交方法中读取服务器反馈回来的结果。这就是使用HttpClient的基本流程。其实用一行代码也就可以搞定整个请求的过程，非常的简单！</SPAN></P></TD></TR></TBODY></TABLE>	(<a href=http://www.blogcn.com/user28/reene/blog/22216549.html target=_blank>浏览全文</a>&nbsp; <a href=http://www.blogcn.com/blog/trackback.asp?mydiary=22216549 target=_blank>引用此文</a>)<br>文章来源:<a href='http://www.blogcn.com/user28/reene/blog/22216549.html'>http://www.blogcn.com/user28/reene/blog/22216549.html</a><img src ="http://www.cnitblog.com/reene/aggbug/3175.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/reene/" target="_blank">快乐着飞舞着</a> 2005-10-09 13:07 <a href="http://www.cnitblog.com/reene/archive/2005/10/09/3175.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>有用的国外开源项目网址 [转]</title><link>http://www.cnitblog.com/reene/archive/2005/10/09/3176.html</link><dc:creator>快乐着飞舞着</dc:creator><author>快乐着飞舞着</author><pubDate>Sun, 09 Oct 2005 05:07:00 GMT</pubDate><guid>http://www.cnitblog.com/reene/archive/2005/10/09/3176.html</guid><wfw:comment>http://www.cnitblog.com/reene/comments/3176.html</wfw:comment><comments>http://www.cnitblog.com/reene/archive/2005/10/09/3176.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cnitblog.com/reene/comments/commentRss/3176.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/reene/services/trackbacks/3176.html</trackback:ping><description><![CDATA[<TABLE style="BORDER-RIGHT: #6595d6 1px dotted; TABLE-LAYOUT: fixed; BORDER-TOP: #6595d6 1px dotted; FONT-SIZE: 12px; BORDER-LEFT: #6595d6 1px dotted; BORDER-BOTTOM: #6595d6 1px dotted" cellSpacing=0 cellPadding=6 width="95%" align=center border=0>
<TBODY>
<TR>
<TD style="WORD-WRAP: break-word" bgColor=#e8f4ff>
<P>sourceforge <A href="http://www.sourceforge.net">http://www.sourceforge.net</A> <BR>java.net <A href="http://www.java.net">http://www.java.net</A></P>
<P><A href="http://www.eclipse.org">www.eclipse.org</A></P>
<P><A href="http://www.opensource.org">www.opensource.org</A></P>
<P>Lomboz <A href="http://www.objectlearn.com/index.jsp">http://www.objectlearn.com/index.jsp</A> (J2EE plugin for Eclipse)<BR>htmlArea <A href="http://sourceforge.net/projects/itools-htmlarea/">http://sourceforge.net/projects/itools-htmlarea/</A> (所见即所得的在线HTML编辑器)<BR>XmlBuddy <A href="http://www.xmlbuddy.com/">http://www.xmlbuddy.com/</A> (XML Editor plugin for Eclipse)<BR>JFreeChart <A href="http://www.jfree.org/">http://www.jfree.org/</A> (用于生成图表的项目)<BR>EclipseME <A href="http://eclipseme.sourceforge.net/">http://eclipseme.sourceforge.net/</A> (J2ME Developmnt Plugin for Eclipse)<BR>mvnForum <A href="http://sourceforge.net/projects/mvnforum/">http://sourceforge.net/projects/mvnforum/</A> (论坛)<BR>jChatBox <A href="http://www.javazoom.net/index.shtml">http://www.javazoom.net/index.shtml</A> (用servlet实现的WEB聊天引擎)<BR>POI <A href="http://jakarta.apache.org/poi/index.html">http://jakarta.apache.org/poi/index.html</A> (用于处理Excel,WORD等文档的项目)<BR>FileUpload <A href="http://jakarta.apache.org/commons/fileupload/">http://jakarta.apache.org/commons/fileupload/</A> (用于处理HTTP文件上传得项目)<BR>PDFBox <A href="http://sourceforge.net/projects/pdfbox/">http://sourceforge.net/projects/pdfbox/</A> (处理PDF文档的项目)<BR>Lucene <A href="http://jakarta.apache.org/lucene/index.html">http://jakarta.apache.org/lucene/index.html</A> (搜索引擎)<BR>Digester <A href="http://jakarta.apache.org/commons/digester/">http://jakarta.apache.org/commons/digester/</A> (处理XML信息的项目)<BR>DBCP <A href="http://jakarta.apache.org/commons/dbcp/">http://jakarta.apache.org/commons/dbcp/</A> (数据库连接池)<BR>AXIS <A href="http://ws.apache.org/axis/">http://ws.apache.org/axis/</A> (WebService 的实现框架)<BR>Jetspeed <A href="http://portals.apache.org/jetspeed-1/">http://portals.apache.org/jetspeed-1/</A> (Portal)<BR>HSQLDB <A href="http://sourceforge.net/projects/hsqldb/">http://sourceforge.net/projects/hsqldb/</A> (Im memory Database Engine)<BR>CEWOLF <A href="http://sourceforge.net/projects/cewolf/">http://sourceforge.net/projects/cewolf/</A> (一套标签库实现Web报表,使用的是jFreeChart引擎)<BR>Struts Menu <A href="http://sourceforge.net/projects/struts-menu/">http://sourceforge.net/projects/struts-menu/</A> (基于Struts的Web菜单项目)<BR>htmlparser <A href="http://sourceforge.net/projects/htmlparser/">http://sourceforge.net/projects/htmlparser/</A> (用于解析HTML信息的项目)<BR>Mondrian <A href="http://sourceforge.net/projects/mondrian/(Open">http://sourceforge.net/projects/mondrian/(Open</A> Source OLAP Database)<BR>ProGuard <A href="http://sourceforge.net/projects/proguard/(Java">http://sourceforge.net/projects/proguard/(Java</A>的混淆器)<BR>InfoGlue <A href="http://sourceforge.net/projects/infoglue/">http://sourceforge.net/projects/infoglue/</A> (J2EE 内容管理系统)<BR>JPivot <A href="http://sourceforge.net/projects/jpivot/">http://sourceforge.net/projects/jpivot/</A> (基于WEB的OLAP 展现)<BR><A href="http://java-source.net/">http://java-source.net/</A></P>
<P>(5)几个常用JAVA开源项目的地址荟萃:<BR>Lomboz&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <A href="http://www.objectlearn.com/index.jsp">http://www.objectlearn.com/index.jsp</A> (J2EE plugin for Eclipse)<BR>htmlArea&nbsp;&nbsp;&nbsp; <A href="http://sourceforge.net/projects/itools-htmlarea/">http://sourceforge.net/projects/itools-htmlarea/</A> (所见即所得的在线HTML编辑器)<BR>XmlBuddy&nbsp;&nbsp; <A href="http://www.xmlbuddy.com/">http://www.xmlbuddy.com/</A> (XML Editor plugin for Eclipse)<BR>JFreeChart <A href="http://www.jfree.org/">http://www.jfree.org/</A> (用于生成图表的项目)<BR>EclipseME&nbsp;&nbsp; <A href="http://eclipseme.sourceforge.net/">http://eclipseme.sourceforge.net/</A> (J2ME Developmnt Plugin for Eclipse)<BR>mvnForum&nbsp; <A href="http://sourceforge.net/projects/mvnforum/">http://sourceforge.net/projects/mvnforum/</A> (论坛)<BR>jChatBox&nbsp;&nbsp;&nbsp; <A href="http://www.javazoom.net/index.shtml">http://www.javazoom.net/index.shtml</A> (用servlet实现的WEB聊天引擎)<BR>POI&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <A href="http://jakarta.apache.org/poi/index.html">http://jakarta.apache.org/poi/index.html</A> (用于处理Excel,WORD等文档的项目)<BR>FileUpload <A href="http://jakarta.apache.org/commons/fileupload/">http://jakarta.apache.org/commons/fileupload/</A> (用于处理HTTP文件上传得项目)<BR>PDFBox&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <A href="http://sourceforge.net/projects/pdfbox/">http://sourceforge.net/projects/pdfbox/</A> (处理PDF文档的项目)<BR>Lucene&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <A href="http://jakarta.apache.org/lucene/index.html">http://jakarta.apache.org/lucene/index.html</A> (搜索引擎)<BR>Digester&nbsp;&nbsp;&nbsp; <A href="http://jakarta.apache.org/commons/digester/">http://jakarta.apache.org/commons/digester/</A> (处理XML信息的项目)<BR>DBCP&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <A href="http://jakarta.apache.org/commons/dbcp/">http://jakarta.apache.org/commons/dbcp/</A>&nbsp; (数据库连接池)<BR>AXIS&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <A href="http://ws.apache.org/axis/">http://ws.apache.org/axis/</A> (WebService 的实现框架)<BR>Jetspeed&nbsp;&nbsp; <A href="http://portals.apache.org/jetspeed-1/">http://portals.apache.org/jetspeed-1/</A> (Portal)<BR>HSQLDB&nbsp;&nbsp;&nbsp; <A href="http://sourceforge.net/projects/hsqldb/">http://sourceforge.net/projects/hsqldb/</A> (Im memory Database Engine)<BR>CEWOLF&nbsp;&nbsp; <A href="http://sourceforge.net/projects/cewolf/">http://sourceforge.net/projects/cewolf/</A>&nbsp; (一套标签库实现Web报表,使用的是jFreeChart引擎)<BR>Struts Menu <A href="http://sourceforge.net/projects/struts-menu/">http://sourceforge.net/projects/struts-menu/</A>&nbsp; (基于Struts的Web菜单项目)<BR>htmlparser&nbsp;&nbsp; <A href="http://sourceforge.net/projects/htmlparser/">http://sourceforge.net/projects/htmlparser/</A>&nbsp;&nbsp; (用于解析HTML信息的项目)<BR>Mondrian&nbsp;&nbsp;&nbsp;&nbsp; <A href="http://sourceforge.net/projects/mondrian/">http://sourceforge.net/projects/mondrian/</A>&nbsp; (Open Source OLAP Database)<BR>ProGuard&nbsp;&nbsp;&nbsp;&nbsp; <A href="http://sourceforge.net/projects/proguard/">http://sourceforge.net/projects/proguard/</A> (Java的混淆器)<BR>InfoGlue&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <A href="http://sourceforge.net/projects/infoglue/">http://sourceforge.net/projects/infoglue/</A>&nbsp; (J2EE 内容管理系统)<BR>JPivot&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <A href="http://sourceforge.net/projects/jpivot/">http://sourceforge.net/projects/jpivot/</A>&nbsp; (基于WEB的OLAP 展现)<BR>HttpClient&nbsp;&nbsp; <A href="http://jakarta.apache.org/commons/httpclient/">http://jakarta.apache.org/commons/httpclient/</A>&nbsp; 处理http客户端的接口<BR></P></TD></TR></TBODY></TABLE>	(<a href=http://www.blogcn.com/user28/reene/blog/22215741.html target=_blank>浏览全文</a>&nbsp; <a href=http://www.blogcn.com/blog/trackback.asp?mydiary=22215741 target=_blank>引用此文</a>)<br>文章来源:<a href='http://www.blogcn.com/user28/reene/blog/22215741.html'>http://www.blogcn.com/user28/reene/blog/22215741.html</a><img src ="http://www.cnitblog.com/reene/aggbug/3176.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/reene/" target="_blank">快乐着飞舞着</a> 2005-10-09 13:07 <a href="http://www.cnitblog.com/reene/archive/2005/10/09/3176.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>应用HttpClient来对付各种顽固的WEB服务器(4) [转]</title><link>http://www.cnitblog.com/reene/archive/2005/10/09/3172.html</link><dc:creator>快乐着飞舞着</dc:creator><author>快乐着飞舞着</author><pubDate>Sun, 09 Oct 2005 05:07:00 GMT</pubDate><guid>http://www.cnitblog.com/reene/archive/2005/10/09/3172.html</guid><wfw:comment>http://www.cnitblog.com/reene/comments/3172.html</wfw:comment><comments>http://www.cnitblog.com/reene/archive/2005/10/09/3172.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/reene/comments/commentRss/3172.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/reene/services/trackbacks/3172.html</trackback:ping><description><![CDATA[<TABLE style="BORDER-RIGHT: #6595d6 1px dotted; TABLE-LAYOUT: fixed; BORDER-TOP: #6595d6 1px dotted; FONT-SIZE: 12px; BORDER-LEFT: #6595d6 1px dotted; BORDER-BOTTOM: #6595d6 1px dotted" cellSpacing=0 cellPadding=6 width="95%" align=center border=0>
<TBODY>
<TR>
<TD style="WORD-WRAP: break-word" bgColor=#e8f4ff>
<P><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-highlight: white; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA; mso-fareast-font-family: 宋体">6．&nbsp;通过HTTP上传文件</SPAN></P>
<P><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-highlight: white; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA; mso-fareast-font-family: 宋体">httpclient使用了单独的一个HttpMethod子类来处理文件的上传，这个类就是MultipartPostMethod，该类已经封装了文件上传的细节，我们要做的仅仅是告诉它我们要上传文件的全路径即可，下面的代码片段演示如何使用这个类。</SPAN></P><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-highlight: white; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA; mso-fareast-font-family: 宋体">
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white">MultipartPostMethod</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"> <SPAN style="COLOR: black">filePost</SPAN> <SPAN style="COLOR: black">=</SPAN> <B><SPAN style="COLOR: #7f0055">new</SPAN></B> <SPAN style="COLOR: black">MultipartPostMethod(targetURL);</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white">filePost.addParameter(</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #2a00ff; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white">"fileName"</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; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"> <SPAN style="COLOR: black">targetFilePath);</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white">HttpClient</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"> <SPAN style="COLOR: black">client</SPAN> <SPAN style="COLOR: black">=</SPAN> <B><SPAN style="COLOR: #7f0055">new</SPAN></B> <SPAN style="COLOR: black">HttpClient();</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #3f7f5f; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white">//</SPAN><SPAN style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #3f7f5f; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-highlight: white">由于要上传的文件可能比较大</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #3f7f5f; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white">,</SPAN><SPAN style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #3f7f5f; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; 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></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white">client.getHttpConnectionManager().</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="COLOR: black">getParams().setConnectionTimeout(5000);</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P><B><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #7f0055; FONT-FAMILY: 'Courier New'; mso-highlight: white; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA; mso-fareast-font-family: 宋体">int</SPAN></B><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-highlight: white; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA; mso-fareast-font-family: 宋体"> <SPAN style="COLOR: black">status</SPAN> <SPAN style="COLOR: black">=</SPAN> <SPAN style="COLOR: black">client.executeMethod(filePost);</SPAN></SPAN><BR></SPAN></P>
<P>上面代码中，targetFilePath即为要上传的文件所在的路径。</P>
<P>7．&nbsp;访问启用认证的页面</P>
<P>我们经常会碰到这样的页面，当访问它的时候会弹出一个浏览器的对话框要求输入用户名和密码后方可，这种用户认证的方式不同于我们在前面介绍的基于表单的用户身份验证。这是HTTP的认证策略，httpclient支持三种认证方式包括：基本、摘要以及NTLM认证。其中基本认证最简单、通用但也最不安全；摘要认证是在HTTP 1.1中加入的认证方式，而NTLM则是微软公司定义的而不是通用的规范，最新版本的NTLM是比摘要认证还要安全的一种方式。</P>
<P>下面例子是从httpclient的CVS服务器中下载的，它简单演示如何访问一个认证保护的页面：<BR></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><B><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #7f0055; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white">import</SPAN></B><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"> <SPAN style="COLOR: black">org.apache.commons.httpclient.HttpClient;</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><B><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #7f0055; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white">import</SPAN></B><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"> <SPAN style="COLOR: black">org.apache.commons.httpclient.UsernamePasswordCredentials;</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><B><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #7f0055; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white">import</SPAN></B><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"> <SPAN style="COLOR: black">org.apache.commons.httpclient.methods.GetMethod;</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt">&nbsp;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><B><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #7f0055; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white">public</SPAN></B><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"> <B><SPAN style="COLOR: #7f0055">class</SPAN></B> <SPAN style="COLOR: black">BasicAuthenticationExample</SPAN> <SPAN style="COLOR: black">{</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN><B><SPAN style="COLOR: #7f0055">public</SPAN></B> <SPAN style="COLOR: black">BasicAuthenticationExample()</SPAN> <SPAN style="COLOR: black">{</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: black">}</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp; </SPAN><SPAN style="mso-spacerun: yes">&nbsp;</SPAN><B><SPAN style="COLOR: #7f0055">public</SPAN></B> <B><SPAN style="COLOR: #7f0055">static</SPAN></B> <B><SPAN style="COLOR: #7f0055">void</SPAN></B> <SPAN style="COLOR: black">main(String[]</SPAN> <SPAN style="COLOR: black">args)</SPAN> <B><SPAN style="COLOR: #7f0055">throws</SPAN></B> <SPAN style="COLOR: black">Exception</SPAN> <SPAN style="COLOR: black">{</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: black">HttpClient</SPAN> <SPAN style="COLOR: black">client</SPAN> <SPAN style="COLOR: black">=</SPAN> <B><SPAN style="COLOR: #7f0055">new</SPAN></B> <SPAN style="COLOR: black">HttpClient();</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: black">client.getState().setCredentials(</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: #2a00ff">"www.verisign.com"</SPAN><SPAN style="COLOR: black">,</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: #2a00ff">"realm"</SPAN><SPAN style="COLOR: black">,</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><B><SPAN style="COLOR: #7f0055">new</SPAN></B> <SPAN style="COLOR: black">UsernamePasswordCredentials(</SPAN><SPAN style="COLOR: #2a00ff">"username"</SPAN><SPAN style="COLOR: black">,</SPAN> <SPAN style="COLOR: #2a00ff">"password"</SPAN><SPAN style="COLOR: black">)</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: black">);</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: black">GetMethod</SPAN> <SPAN style="COLOR: black">get</SPAN> <SPAN style="COLOR: black">=</SPAN> <B><SPAN style="COLOR: #7f0055">new</SPAN></B> <SPAN style="COLOR: black">GetMethod(</SPAN><SPAN style="COLOR: #2a00ff">"https://www.verisign.com/products/index.html"</SPAN><SPAN style="COLOR: black">);</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: black">get.setDoAuthentication(</SPAN> <B><SPAN style="COLOR: #7f0055">true</SPAN></B> <SPAN style="COLOR: black">);</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><B><SPAN style="COLOR: #7f0055">int</SPAN></B> <SPAN style="COLOR: black">status</SPAN> <SPAN style="COLOR: black">=</SPAN> <SPAN style="COLOR: black">client.executeMethod(</SPAN> <SPAN style="COLOR: black">get</SPAN> <SPAN style="COLOR: black">);</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: black">System.out.println(status+</SPAN><SPAN style="COLOR: #2a00ff">"\n"</SPAN><SPAN style="COLOR: black">+</SPAN> <SPAN style="COLOR: black">get.getResponseBodyAsString());</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: black">get.releaseConnection();</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: black">}</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-highlight: white; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA; mso-fareast-font-family: 宋体">}</SPAN></P>
<P><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-highlight: white; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA; mso-fareast-font-family: 宋体">8．&nbsp;多线程模式下使用httpclient</SPAN></P>
<P><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-highlight: white; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA; mso-fareast-font-family: 宋体">多线程同时访问httpclient，例如同时从一个站点上下载多个文件。对于同一个HttpConnection同一个时间只能有一个线程访问，为了保证多线程工作环境下不产生冲突，httpclient使用了一个多线程连接管理器的类：MultiThreadedHttpConnectionManager，要使用这个类很简单，只需要在构造HttpClient实例的时候传入即可，代码如下：</SPAN></P><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-highlight: white; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA; mso-fareast-font-family: 宋体">
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white">MultiThreadedHttpConnectionManager</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"> <SPAN style="COLOR: black">connectionManager</SPAN> <SPAN style="COLOR: black">=</SPAN> </SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp; </SPAN><B><SPAN style="COLOR: #7f0055">new</SPAN></B> <SPAN style="COLOR: black">MultiThreadedHttpConnectionManager();</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-highlight: white; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA; mso-fareast-font-family: 宋体"><SPAN style="COLOR: black">HttpClient</SPAN> <SPAN style="COLOR: black">client</SPAN> <SPAN style="COLOR: black">=</SPAN> <B><SPAN style="COLOR: #7f0055">new</SPAN></B> <SPAN style="COLOR: black">HttpClient(connectionManager);</SPAN></SPAN></P>
<P><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-highlight: white; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA; mso-fareast-font-family: 宋体"><SPAN style="COLOR: black">以后尽管访问client实例即可。</SPAN></SPAN></P>
<P><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-highlight: white; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA; mso-fareast-font-family: 宋体"><SPAN style="COLOR: black">参考资料：</SPAN></SPAN></P>
<P><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-highlight: white; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA; mso-fareast-font-family: 宋体"><SPAN style="COLOR: black">httpclient首页：&nbsp;&nbsp;&nbsp;&nbsp;<A href="http://jakarta.apache.org/commons/httpclient/">http://jakarta.apache.org/commons/httpclient/</A><BR>关于NTLM是如何工作：&nbsp;&nbsp;<A href="http://davenport.sourceforge.net/ntlm.html">http://davenport.sourceforge.net/ntlm.html</A> </SPAN></SPAN><BR></SPAN><BR></P></TD></TR></TBODY></TABLE>	(<a href=http://www.blogcn.com/user28/reene/blog/22216776.html target=_blank>浏览全文</a>&nbsp; <a href=http://www.blogcn.com/blog/trackback.asp?mydiary=22216776 target=_blank>引用此文</a>)<br>文章来源:<a href='http://www.blogcn.com/user28/reene/blog/22216776.html'>http://www.blogcn.com/user28/reene/blog/22216776.html</a><img src ="http://www.cnitblog.com/reene/aggbug/3172.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/reene/" target="_blank">快乐着飞舞着</a> 2005-10-09 13:07 <a href="http://www.cnitblog.com/reene/archive/2005/10/09/3172.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>jfree中字体的调整</title><link>http://www.cnitblog.com/reene/archive/2005/10/09/3178.html</link><dc:creator>快乐着飞舞着</dc:creator><author>快乐着飞舞着</author><pubDate>Sun, 09 Oct 2005 05:07:00 GMT</pubDate><guid>http://www.cnitblog.com/reene/archive/2005/10/09/3178.html</guid><wfw:comment>http://www.cnitblog.com/reene/comments/3178.html</wfw:comment><comments>http://www.cnitblog.com/reene/archive/2005/10/09/3178.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/reene/comments/commentRss/3178.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/reene/services/trackbacks/3178.html</trackback:ping><description><![CDATA[<TABLE style="BORDER-RIGHT: #6595d6 1px dotted; TABLE-LAYOUT: fixed; BORDER-TOP: #6595d6 1px dotted; FONT-SIZE: 12px; BORDER-LEFT: #6595d6 1px dotted; BORDER-BOTTOM: #6595d6 1px dotted" cellSpacing=0 cellPadding=6 width="95%" align=center border=0>
<TBODY>
<TR>
<TD style="WORD-WRAP: break-word" bgColor=#e8f4ff>
<P>JFreeChart chart = ChartFactory.createVerticalBarChart3D(title, domain, range, dataset,true,true,false);</P>
<P>chart.setBackgroundPaint(new GradientPaint(0.0F, 0.0F, Color.white, 1000F, 0.0F, Color.red));<BR>chart.setTitle(new TextTitle(title, new Font("隶书", Font.ITALIC, 15)));</P>
<P>Font font=new Font("黑体",Font.TRUETYPE_FONT, 12);</P>
<P>StandardLegend legend = (StandardLegend) chart.getLegend();<BR>legend.setItemFont(font);</P>
<P>CategoryPlot plot = (CategoryPlot)chart.getPlot();<BR>plot.setForegroundAlpha(0.9F);</P>
<P>CategoryAxis domain_axis = plot.getDomainAxis();<BR>domain_axis.setTickLabelFont(font);</P>
<P>ValueAxis value_axis=plot.getRangeAxis();<BR>value_axis.setTickLabelFont(font);</P>
<P>&nbsp;</P></TD></TR></TBODY></TABLE>	(<a href=http://www.blogcn.com/user28/reene/blog/22155047.html target=_blank>浏览全文</a>&nbsp; <a href=http://www.blogcn.com/blog/trackback.asp?mydiary=22155047 target=_blank>引用此文</a>)<br>文章来源:<a href='http://www.blogcn.com/user28/reene/blog/22155047.html'>http://www.blogcn.com/user28/reene/blog/22155047.html</a><img src ="http://www.cnitblog.com/reene/aggbug/3178.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/reene/" target="_blank">快乐着飞舞着</a> 2005-10-09 13:07 <a href="http://www.cnitblog.com/reene/archive/2005/10/09/3178.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>使用JFreeChart创建图象</title><link>http://www.cnitblog.com/reene/archive/2005/10/09/3179.html</link><dc:creator>快乐着飞舞着</dc:creator><author>快乐着飞舞着</author><pubDate>Sun, 09 Oct 2005 05:07:00 GMT</pubDate><guid>http://www.cnitblog.com/reene/archive/2005/10/09/3179.html</guid><wfw:comment>http://www.cnitblog.com/reene/comments/3179.html</wfw:comment><comments>http://www.cnitblog.com/reene/archive/2005/10/09/3179.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/reene/comments/commentRss/3179.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/reene/services/trackbacks/3179.html</trackback:ping><description><![CDATA[<TABLE style="BORDER-RIGHT: #6595d6 1px dotted; TABLE-LAYOUT: fixed; BORDER-TOP: #6595d6 1px dotted; FONT-SIZE: 12px; BORDER-LEFT: #6595d6 1px dotted; BORDER-BOTTOM: #6595d6 1px dotted" cellSpacing=0 cellPadding=6 width="95%" align=center border=0>
<TBODY>
<TR>
<TD style="WORD-WRAP: break-word" bgColor=#e8f4ff>
<P>使用JFreeChart创建图象&nbsp; <BR>文档性质： 转载&nbsp; 发表时间：2003-5-21 快速返回 <BR>作者:孤魂一笑(<A href="mailto:bingo_ge@hotmail.com">bingo_ge@hotmail.com</A>) 日期：2003-05-08 <BR>一：jfreechart介绍<BR>jfreechart是一个免费创建图片的java工具.可以创建如下图形：<BR>饼图(pie charts;)<BR>曲线图(line charts )<BR>柱状图(horizontal/vertical bar charts)<BR>甘特图(Gantt charts; )<BR>XY plots and scatter plots; <BR>time series, high/low/open/close charts and candle stick charts; <BR>combination charts; <BR>Pareto charts; <BR>bubble charts; <BR>wind plots, meter charts and symbol charts; <BR>从以下地址可以看到jfreechart可以创建的图形类型<BR><A href="http://www.jfree.org/jfreechart/samples.html">http://www.jfree.org/jfreechart/samples.html</A><BR>sourceforge有一个基于jfreechart的项目Cewolf可以很方便的在jsp/servlet中创建图片<BR>jfreechart目前(2003-05-08)版本为0.98<BR>希望得到详细的信息或下载jfreechart请访问如下站点:<BR><A href="http://www.jfree.org/jfreechart/">http://www.jfree.org/jfreechart/</A></P>
<P>二：特别说明:<BR>jfreechart是一个开源项目，但是文档是需要40美金去购买的。<BR>还有一个很重要的问题,jfreechart如果使用中文，他使用的默认字体<BR>显示出来的中文会很模糊，你可能需要修改源代码。<BR>下面我就举几个简单的例子说明一下如何使用jfreechart创建图片<BR>在开发中有可能会导入以下的类<BR>import com.jrefinery.chart.ChartFactory;<BR>import com.jrefinery.chart.ChartUtilities;<BR>import com.jrefinery.chart.JFreeChart;<BR>import com.jrefinery.chart.TextTitle;<BR>import com.jrefinery.chart.axis.NumberAxis;<BR>import com.jrefinery.chart.plot.CategoryPlot;<BR>import com.jrefinery.chart.plot.PiePlot;<BR>import com.jrefinery.data.Day;<BR>import com.jrefinery.data.DefaultCategoryDataset;<BR>import com.jrefinery.data.DefaultPieDataset;<BR>import com.jrefinery.data.TimeSeries;<BR>import com.jrefinery.data.TimeSeriesCollection;<BR>import com.jrefinery.data.TimeSeriesDataPair;<BR>在0.98以后包由com.jrefinery.*改变为:org.jfree</P>
<P>三：创建饼图<BR>//图片标题<BR>String title = "空调2002年市场占有率";<BR>//设定数据源<BR>DefaultPieDataset piedata = new DefaultPieDataset();<BR>//第一个参数为名称，第二个参数是double数<BR>piedata.setValue("联想", 27.3);<BR>piedata.setValue("长城", 12.2);<BR>piedata.setValue("海尔", 5.5);<BR>piedata.setValue("美的", 17.1);<BR>piedata.setValue("松下", 9.0);<BR>piedata.setValue("科龙", 19.0);<BR>//创建JFreeChart，都使用ChartFactory来创建JFreeChart,很标准的工厂设计模式<BR>JFreeChart chart =<BR>ChartFactory.createPieChart(title, piedata, true, true, true);<BR>//设定图片标题<BR>chart.setTitle(new TextTitle(title, new Font("隶书", Font.ITALIC, 15)));<BR>//chart.addSubtitle(new TextTitle("2002财年分析", new Font("隶书", Font.ITALIC, 12)));<BR>//设定背景<BR>chart.setBackgroundPaint(Color.white);<BR>//chart.s<BR>//饼图使用一个PiePlot <BR>PiePlot pie = (PiePlot)chart.getPlot();<BR>//pie.setSectionLabelType(PiePlot.NAME_AND_PERCENT_LABELS);<BR>pie.setSectionLabelType(PiePlot.NAME_AND_VALUE_LABELS);<BR>//设定显示格式(名称加百分比或数值)<BR>pie.setPercentFormatString("#,###0.0#%");<BR>//设定百分比显示格式<BR>pie.setBackgroundPaint(Color.white);<BR>pie.setSectionLabelFont(new Font("黑体", Font.TRUETYPE_FONT, 12));<BR>//设定背景透明度（0-1.0之间）<BR>pie.setBackgroundAlpha(0.6f);<BR>//设定前景透明度（0-1.0之间）<BR>pie.setForegroundAlpha(0.90f);<BR>//输出文件到指定目录<BR>String rfname = MathUtil.getRoundCode(12) + ".jpeg";<BR>String fileName = "d:/test/" + rfname;<BR>try {<BR>//可以保存文件为jpg或png格式。<BR>ChartUtilities.saveChartAsJPEG(new File(fileName), 100, chart, 600, 600);<BR>//第一个参数为文件名<BR>//第二个参数质量<BR>//第三个参数为哪个chart创建图片<BR>//第四个宽度<BR>//第五个高度<BR>} catch (IOException exz) {<BR>System.out.print("....Cant’t Create image File");<BR>}</P>
<P>其实使用JFreeChart创建图片很简单，不同的的图片类型区别在于设置数据集</P>
<P>四：创建曲线图<BR>// create a default chart based on some sample data...<BR>//曲线图标题<BR>String title = "趋势分析";<BR>//曲线图X轴提示<BR>String domain = "月份走势";<BR>//曲线图Y轴提示<BR>String range = "应收余额";<BR>//曲线图自标题<BR>String subtitleStr = "2003财年分析";<BR>//创建时间数据源<BR>//每一个TimeSeries在图上是一条曲线<BR>TimeSeries ca = new TimeSeries("用友");<BR>for (int i = 1999; i &lt; 2005; i++) {<BR>for (int mon = 0; mon &lt; 12; mon++) {<BR>//ca.add(new Month(mon + 1, i), new Double(500 + Math.random() * 100));<BR>//TimeSeriesDataPair是一个时间点的数值体现<BR>ca.add(<BR>new TimeSeriesDataPair(<BR>new Day(1, mon + 1, i),<BR>new Double(500 + Math.random() * 100)));<BR>}<BR>}</P>
<P>TimeSeries ibm = new TimeSeries("金碟");<BR>for (int i = 1999; i &lt; 2005; i++) {<BR>for (int mon = 0; mon &lt; 12; mon++) {<BR>//ibm.add(new Month(mon+1,i),new Double(400-Math.random()*100));<BR>ibm.add(<BR>new TimeSeriesDataPair(<BR>new Day(1, mon + 1, i),<BR>new Double(400 - Math.random() * 100)));<BR>}<BR>}</P>
<P>TimeSeries king = new TimeSeries("东软");<BR>for (int i = 1999; i &lt; 2005; i++) {<BR>for (int mon = 0; mon &lt; 12; mon++) {<BR>//ibm.add(new Month(mon+1,i),new Double(400-Math.random()*100));<BR>king.add(<BR>new TimeSeriesDataPair(<BR>new Day(1, mon + 1, i),<BR>new Double(300 - Math.random() * 100)));<BR>}<BR>}<BR>//时间曲线数据集合<BR>TimeSeriesCollection dataset = new TimeSeriesCollection();<BR>dataset.addSeries(ca);<BR>dataset.addSeries(ibm);<BR>dataset.addSeries(king);<BR>//dataset.addSeries(jpy);<BR>//dataset.addSeries(mav);<BR>//时间曲线元素<BR>JFreeChart chart =<BR>ChartFactory.createTimeSeriesChart(<BR>title,<BR>domain,<BR>range,<BR>dataset,<BR>true,<BR>true,<BR>false);<BR>// then customise it a little...<BR>TextTitle subtitle =<BR>new TextTitle(subtitleStr, new Font("黑体", Font.BOLD, 12));<BR>chart.addSubtitle(subtitle);<BR>chart.setTitle(new TextTitle(title, new Font("隶书", Font.ITALIC, 15)));<BR>//pie.setSeriesLabelFont(new Font("黑体", Font.BOLD, 15));<BR>chart.setBackgroundPaint(<BR>new GradientPaint(0, 0, Color.white, 0, 1000, Color.blue));<BR>//sysout<BR>//输出文件到指定目录<BR>String rfname = MathUtil.getRoundCode(22) + ".jpeg";<BR>String fileName = "d:/test/" + rfname;<BR>try {<BR>//for<BR>//System.out.println();<BR>ChartUtilities.saveChartAsJPEG(new File(fileName), 100, chart, 600, 600);<BR>// log.info("....Create image File:" + fileName);<BR>} catch (IOException exz) {<BR>System.out.print("....Cant’t Create image File");<BR>}</P>
<P><BR>五：创建柱状图<BR>String title = "柱状图测试";<BR>String domain = "单位比较";<BR>String range = "数值";<BR>//CategoryDataset data = DemoDatasetFactory.createCategoryDataset();<BR>DefaultCategoryDataset data = new DefaultCategoryDataset();<BR>for (int r = 0; r &lt; 5; r++) {<BR>String rowKey = "单位 [" + (r + 1)+"]" ;<BR>//第一层循环：分析对象<BR>for (int c = 0; c &lt; 6; c++) {<BR>//第二层循环：分析对象在时间点上的数据<BR>String columnKey = "2001年" + (c + 1) + "月";<BR>data.addValue(new Double(r * c + 5), rowKey, columnKey);<BR>}<BR>}</P>
<P>JFreeChart chart =<BR>ChartFactory.createVerticalBarChart(<BR>title,<BR>domain,<BR>range,<BR>data,<BR>true,<BR>true,<BR>false);<BR>// then customise it a little...<BR>chart.setBackgroundPaint(<BR>new GradientPaint(0, 0, Color.white, 1000, 0, Color.red));<BR>chart.setTitle(new TextTitle(title, new Font("隶书", Font.ITALIC, 15)));<BR>CategoryPlot plot = (CategoryPlot)chart.getPlot();<BR>plot.setForegroundAlpha(0.9f);<BR>plot.setValueLabelFont(new Font("黑体", Font.TRUETYPE_FONT, 12));<BR>//plot.setSectionLabelFont(new Font("黑体", Font.TRUETYPE_FONT, 12));<BR>//注意以下代码<BR>NumberAxis verticalAxis = (NumberAxis)plot.getRangeAxis();<BR>verticalAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());<BR>// 输出文件到指定目录<BR>String rfname = MathUtil.getRoundCode(22) + "b.jpeg";<BR>String fileName = "d:/test/" + rfname;<BR>try {<BR>ChartUtilities.saveChartAsJPEG(new File(fileName), 100, chart, 600, 600);<BR>// log.info("....Create image File:" + fileName);<BR>} catch (IOException exz) {<BR>System.out.print("....Cant’t Create image File");<BR>}</P>
<P>六：结束语<BR>个人感觉JFreeChart可以满足大部分图片创建的需要，美中不足的是：对字体的设置做的<BR>不够好，特别是使用中文的时候字体很不清晰。因为这个原因建议你自己去修改他的源代码<BR>最好使用properties文件去设置字体.还有就是文档要钱所以要多花点时间去看源代码。或多<BR>上社区.因为时间等原因我只介绍了三种图片的创建，其他类型的图片可以参考jfreechart提<BR>供的例子。 <BR></P></TD></TR></TBODY></TABLE>	(<a href=http://www.blogcn.com/user28/reene/blog/22154859.html target=_blank>浏览全文</a>&nbsp; <a href=http://www.blogcn.com/blog/trackback.asp?mydiary=22154859 target=_blank>引用此文</a>)<br>文章来源:<a href='http://www.blogcn.com/user28/reene/blog/22154859.html'>http://www.blogcn.com/user28/reene/blog/22154859.html</a><img src ="http://www.cnitblog.com/reene/aggbug/3179.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/reene/" target="_blank">快乐着飞舞着</a> 2005-10-09 13:07 <a href="http://www.cnitblog.com/reene/archive/2005/10/09/3179.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>在eclipse+myeclipse+weblogic下EJB开发所要注意的问题 [转]</title><link>http://www.cnitblog.com/reene/archive/2005/10/09/3187.html</link><dc:creator>快乐着飞舞着</dc:creator><author>快乐着飞舞着</author><pubDate>Sun, 09 Oct 2005 05:07:00 GMT</pubDate><guid>http://www.cnitblog.com/reene/archive/2005/10/09/3187.html</guid><wfw:comment>http://www.cnitblog.com/reene/comments/3187.html</wfw:comment><comments>http://www.cnitblog.com/reene/archive/2005/10/09/3187.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/reene/comments/commentRss/3187.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/reene/services/trackbacks/3187.html</trackback:ping><description><![CDATA[<TABLE style="BORDER-RIGHT: #6595d6 1px dotted; TABLE-LAYOUT: fixed; BORDER-TOP: #6595d6 1px dotted; FONT-SIZE: 12px; BORDER-LEFT: #6595d6 1px dotted; BORDER-BOTTOM: #6595d6 1px dotted" cellSpacing=0 cellPadding=6 width="95%" align=center border=0>
<TBODY>
<TR>
<TD style="WORD-WRAP: break-word" bgColor=#e8f4ff>
<DIV class=postTitle><A name=0></A><A name=1></A>&nbsp;</DIV>
<DIV class=postText>一般在使用<B style="COLOR: black; BACKGROUND-COLOR: #a0ffff">ejb</B>时都是从创立J2EE项目的application项中依次创建application，<B style="COLOR: black; BACKGROUND-COLOR: #a0ffff">ejb</B>,web三项，其中：<BR>appliction是用来作为j2ee部署时使用的。<BR><B style="COLOR: black; BACKGROUND-COLOR: #a0ffff">ejb</B>就是创建<B style="COLOR: black; BACKGROUND-COLOR: #a0ffff">ejb</B>组件。<BR>web就是一个web服务的容器，包括hrml,jsp,servlet,struts等。<BR>在<B style="COLOR: black; BACKGROUND-COLOR: #ffff66">myeclipse</B>中使用<B style="COLOR: black; BACKGROUND-COLOR: #a0ffff">EJB</B>的两种方法：<BR>1）选择J2EE1.3则可以通过xDoclet来根据app_bean来生成app,home,session,apputil等。<BR>2）选择J2EE1.4的就需要自己写app,home,sesion等程序代码。<BR>对于整个程序以后的运行过程都是以JNDI为主线的展开，要注意：<BR>一般要以home中的jndi-name为标准，具体在使用xdoclet的j2ee1.3时参看相关的帮助。<BR>配置主要在：weblogic-<B style="COLOR: black; BACKGROUND-COLOR: #a0ffff">ejb</B>-jar.xml和<B style="COLOR: black; BACKGROUND-COLOR: #a0ffff">ejb</B>-jar.xml的jndi的设置。(这一般是针对于在同一服务器内部的jndi的调用，而对于其他外部情况，需要设置ervirament,权限等问题后才能进行lookup)<BR>由于在服务器等各方面的原因,在部署之前要注意几个配置文件的正确的位置:<BR>1) weblogic-<B style="COLOR: black; BACKGROUND-COLOR: #a0ffff">ejb</B>-jar.xml和<B style="COLOR: black; BACKGROUND-COLOR: #a0ffff">ejb</B>-jar.xml要在XXX<B style="COLOR: black; BACKGROUND-COLOR: #a0ffff">EJB</B>/src/meta-inf下，可以通过在不同的目录下导入。<BR>2) web.xml和weblogic.xml要在XXXWeb/webRoot/web-inf下，可以weblogic.xml直接用别的程序的，web.xml需要自己根据需要来配置。<BR>最后在部署之前，要对整个application server的三项进行项目清理，然后部署到服务器上。</DIV></TD></TR></TBODY></TABLE>	(<a href=http://www.blogcn.com/user28/reene/blog/21696707.html target=_blank>浏览全文</a>&nbsp; <a href=http://www.blogcn.com/blog/trackback.asp?mydiary=21696707 target=_blank>引用此文</a>)<br>文章来源:<a href='http://www.blogcn.com/user28/reene/blog/21696707.html'>http://www.blogcn.com/user28/reene/blog/21696707.html</a><img src ="http://www.cnitblog.com/reene/aggbug/3187.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/reene/" target="_blank">快乐着飞舞着</a> 2005-10-09 13:07 <a href="http://www.cnitblog.com/reene/archive/2005/10/09/3187.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>昨天做梦都在写代码</title><link>http://www.cnitblog.com/reene/archive/2005/10/09/3186.html</link><dc:creator>快乐着飞舞着</dc:creator><author>快乐着飞舞着</author><pubDate>Sun, 09 Oct 2005 05:07:00 GMT</pubDate><guid>http://www.cnitblog.com/reene/archive/2005/10/09/3186.html</guid><wfw:comment>http://www.cnitblog.com/reene/comments/3186.html</wfw:comment><comments>http://www.cnitblog.com/reene/archive/2005/10/09/3186.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cnitblog.com/reene/comments/commentRss/3186.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/reene/services/trackbacks/3186.html</trackback:ping><description><![CDATA[<TABLE style="BORDER-RIGHT: #6595d6 1px dotted; TABLE-LAYOUT: fixed; BORDER-TOP: #6595d6 1px dotted; FONT-SIZE: 12px; BORDER-LEFT: #6595d6 1px dotted; BORDER-BOTTOM: #6595d6 1px dotted" cellSpacing=0 cellPadding=6 width="95%" align=center border=0>
<TBODY>
<TR>
<TD style="WORD-WRAP: break-word" bgColor=#e8f4ff>昨天一天都在写xdoclet 结果今天早上起床时 就记得点了一夜的鼠标写了一夜的代码 </TD></TR></TBODY></TABLE>	(<a href=http://www.blogcn.com/user28/reene/blog/22038480.html target=_blank>浏览全文</a>&nbsp; <a href=http://www.blogcn.com/blog/trackback.asp?mydiary=22038480 target=_blank>引用此文</a>)<br>文章来源:<a href='http://www.blogcn.com/user28/reene/blog/22038480.html'>http://www.blogcn.com/user28/reene/blog/22038480.html</a><img src ="http://www.cnitblog.com/reene/aggbug/3186.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/reene/" target="_blank">快乐着飞舞着</a> 2005-10-09 13:07 <a href="http://www.cnitblog.com/reene/archive/2005/10/09/3186.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>使用 JFreeChart来创建基于web的图表（1）</title><link>http://www.cnitblog.com/reene/archive/2005/10/09/3184.html</link><dc:creator>快乐着飞舞着</dc:creator><author>快乐着飞舞着</author><pubDate>Sun, 09 Oct 2005 05:07:00 GMT</pubDate><guid>http://www.cnitblog.com/reene/archive/2005/10/09/3184.html</guid><wfw:comment>http://www.cnitblog.com/reene/comments/3184.html</wfw:comment><comments>http://www.cnitblog.com/reene/archive/2005/10/09/3184.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/reene/comments/commentRss/3184.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/reene/services/trackbacks/3184.html</trackback:ping><description><![CDATA[<TABLE style="BORDER-RIGHT: #6595d6 1px dotted; TABLE-LAYOUT: fixed; BORDER-TOP: #6595d6 1px dotted; FONT-SIZE: 12px; BORDER-LEFT: #6595d6 1px dotted; BORDER-BOTTOM: #6595d6 1px dotted" cellSpacing=0 cellPadding=6 width="95%" align=center border=0>
<TBODY>
<TR>
<TD style="WORD-WRAP: break-word" bgColor=#e8f4ff>
<P>关于jfreechart的介绍和配置部分就省去。</P>
<P>下面我们来介绍JFreeChart中几个核心的对象类： </P>
<P>
<TABLE cellSpacing=0 borderColorDark=#ffffff width=499 border=1>
<TBODY>
<TR>
<TD width="20%">类名 </TD>
<TD width="80%">类的作用以及简单描述 </TD></TR>
<TR>
<TD>JFreeChart </TD>
<TD>图表对象，任何类型的图表的最终表现形式都是在该对象进行一些属性的定制。JFreeChart引擎本身提供了一个工厂类用于创建不同类型的图表对象 </TD></TR>
<TR>
<TD>XXXXXDataset </TD>
<TD>数据集对象，用于提供显示图表所用的数据。根据不同类型的图表对应着很多类型的数据集对象类 </TD></TR>
<TR>
<TD>XXXXXPlot </TD>
<TD>图表区域对象，基本上这个对象决定着什么样式的图表，创建该对象的时候需要Axis、Renderer以及数据集对象的支持 </TD></TR>
<TR>
<TD>XXXXXAxis </TD>
<TD>用于处理图表的两个轴：纵轴和横轴 </TD></TR>
<TR>
<TD>XXXXXRenderer </TD>
<TD>负责如何显示一个图表对象 </TD></TR>
<TR>
<TD>XXXXXURLGenerator </TD>
<TD>用于生成Web图表中每个项目的鼠标点击链接 </TD></TR>
<TR>
<TD>XXXXXToolTipGenerator </TD>
<TD>用于生成图象的帮助提示，不同类型图表对应不同类型的工具提示类 </TD></TR></TBODY></TABLE></P>
<P>· 使用JFreeChart生成各种样式的图表<BR><BR>限于篇幅的问题我们在这里只实现两种常用的图表，其他类型图表读者可以触类旁通。我们先给出柱状图的实现，饼图的实现再来跟柱状图进行比较。 </P>
<P>· 1 柱状图</P>
<TABLE cellSpacing=0>
<TBODY>
<TR>
<TD bgColor=#cccccc>package lius.chart.demo; 
<P>import java.io.*;</P>
<P>import org.jfree.data.*;<BR>import org.jfree.chart.*;<BR>import org.jfree.chart.plot.*;<BR>/**<BR>* 该类用于演示最简单的柱状图生成<BR>* @author Winter Lau<BR>*/<BR>public class BarChartDemo {</P>
<P>public static void main(String[] args) throws IOException{</P>
<P>CategoryDataset dataset = getDataSet2();<BR>JFreeChart chart = ChartFactory.createBarChart3D(<BR>"水果产量图", // 图表标题<BR>"水果", // 目录轴的显示标签<BR>"产量", // 数值轴的显示标签<BR>dataset, // 数据集<BR>PlotOrientation.VERTICAL, // 图表方向：水平、垂直<BR>true, // 是否显示图例(对于简单的柱状图必须是false)<BR>false, // 是否生成工具<BR>false // 是否生成URL链接<BR>);<BR><BR>FileOutputStream fos_jpg = null;<BR>try {<BR>fos_jpg = new FileOutputStream("D:\\fruit.jpg");<BR>ChartUtilities.writeChartAsJPEG(fos_jpg,100,chart,400,300,null);<BR>} finally {<BR>try {<BR>fos_jpg.close();<BR>} catch (Exception e) {}<BR>}<BR>}<BR>/**<BR>* 获取一个演示用的简单数据集对象<BR>* @return<BR>*/<BR>private static CategoryDataset getDataSet() {<BR>DefaultCategoryDataset dataset = new DefaultCategoryDataset();<BR>dataset.addValue(100, null, "苹果");<BR>dataset.addValue(200, null, "梨子");<BR>dataset.addValue(300, null, "葡萄");<BR>dataset.addValue(400, null, "香蕉");<BR>dataset.addValue(500, null, "荔枝");<BR>return dataset;<BR>}<BR>/**<BR>* 获取一个演示用的组合数据集对象<BR>* @return<BR>*/<BR>private static CategoryDataset getDataSet2() {<BR>DefaultCategoryDataset dataset = new DefaultCategoryDataset();<BR>dataset.addValue(100, "北京", "苹果");<BR>dataset.addValue(100, "上海", "苹果");<BR>dataset.addValue(100, "广州", "苹果");<BR>dataset.addValue(200, "北京", "梨子");<BR>dataset.addValue(200, "上海", "梨子");<BR>dataset.addValue(200, "广州", "梨子");<BR>dataset.addValue(300, "北京", "葡萄");<BR>dataset.addValue(300, "上海", "葡萄");<BR>dataset.addValue(300, "广州", "葡萄");<BR>dataset.addValue(400, "北京", "香蕉");<BR>dataset.addValue(400, "上海", "香蕉");<BR>dataset.addValue(400, "广州", "香蕉");<BR>dataset.addValue(500, "北京", "荔枝");<BR>dataset.addValue(500, "上海", "荔枝");<BR>dataset.addValue(500, "广州", "荔枝");<BR>return dataset;<BR>}<BR>}</P></TD></TR></TBODY></TABLE>
<P>程序运行结束后生成的图片文件效果如下图所示： </P>
<P align=center>图4 <BR><IMG src="http://www.ddvip.net/program/java/index6/img_java_webservice/image004.gif"></P>
<P>如果是使用简单的数据即使用getDataSet方法获取数据集时产生的图片文件如下： </P>
<P align=center>图5 <BR><IMG src="http://www.ddvip.net/program/java/index6/img_java_webservice/image005.gif"></P></TD></TR></TBODY></TABLE>	(<a href=http://www.blogcn.com/user28/reene/blog/22124837.html target=_blank>浏览全文</a>&nbsp; <a href=http://www.blogcn.com/blog/trackback.asp?mydiary=22124837 target=_blank>引用此文</a>)<br>文章来源:<a href='http://www.blogcn.com/user28/reene/blog/22124837.html'>http://www.blogcn.com/user28/reene/blog/22124837.html</a><img src ="http://www.cnitblog.com/reene/aggbug/3184.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/reene/" target="_blank">快乐着飞舞着</a> 2005-10-09 13:07 <a href="http://www.cnitblog.com/reene/archive/2005/10/09/3184.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>使用 JFreeChart来创建基于web的图表（2）</title><link>http://www.cnitblog.com/reene/archive/2005/10/09/3183.html</link><dc:creator>快乐着飞舞着</dc:creator><author>快乐着飞舞着</author><pubDate>Sun, 09 Oct 2005 05:07:00 GMT</pubDate><guid>http://www.cnitblog.com/reene/archive/2005/10/09/3183.html</guid><wfw:comment>http://www.cnitblog.com/reene/comments/3183.html</wfw:comment><comments>http://www.cnitblog.com/reene/archive/2005/10/09/3183.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/reene/comments/commentRss/3183.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/reene/services/trackbacks/3183.html</trackback:ping><description><![CDATA[<TABLE style="BORDER-RIGHT: #6595d6 1px dotted; TABLE-LAYOUT: fixed; BORDER-TOP: #6595d6 1px dotted; FONT-SIZE: 12px; BORDER-LEFT: #6595d6 1px dotted; BORDER-BOTTOM: #6595d6 1px dotted" cellSpacing=0 cellPadding=6 width="95%" align=center border=0>
<TBODY>
<TR>
<TD style="WORD-WRAP: break-word" bgColor=#e8f4ff>
<P>· 2 饼图<BR></P>
<P>对于饼图而言，数据集的获取用的不是同一个数据集类，另外饼图不支持同一个类别的项目中还有子项目这样的数据。我们只给出创建饼图的代码，至于写图表到一个文件则与柱状图一致，无需重复。 </P>
<P>
<TABLE cellSpacing=0>
<TBODY>
<TR>
<TD bgColor=#cccccc>package lius.chart.demo; 
<P>import java.io.*;</P>
<P>import org.jfree.data.*;<BR>import org.jfree.chart.*;<BR>/**<BR>* 用于演示饼图的生成<BR>* @author Winter Lau<BR>*/<BR>public class PieChartDemo {</P>
<P>public static void main(String[] args) throws IOException{<BR>DefaultPieDataset data = getDataSet();<BR>JFreeChart chart = ChartFactory.createPie3DChart("水果产量图", // 图表标题<BR>data, <BR>true, // 是否显示图例<BR>false,<BR>false<BR>);<BR>//写图表对象到文件，参照柱状图生成源码<BR>}<BR>/**<BR>* 获取一个演示用的简单数据集对象<BR>* @return<BR>*/<BR>private static DefaultPieDataset getDataSet() {<BR>DefaultPieDataset dataset = new DefaultPieDataset();<BR>dataset.setValue("苹果",100);<BR>dataset.setValue("梨子",200);<BR>dataset.setValue("葡萄",300);<BR>dataset.setValue("香蕉",400);<BR>dataset.setValue("荔枝",500);<BR>return dataset;<BR>}<BR>}</P></TD></TR></TBODY></TABLE></P>
<P>生成的饼图文件效果如下： </P>
<P align=center>图6 <BR><IMG src="http://www.ddvip.net/program/java/index6/img_java_webservice/image006.gif"></P>
<P>将生成的图表移到浏览器上<BR>为了将生成的图表直接传给客户端浏览器，只需要将前面两个例子中的文件流换成是通过HttpServletResponse对象获取到的输出流，详细代码清单如下： </P>
<P>
<TABLE cellSpacing=0>
<TBODY>
<TR>
<TD bgColor=#cccccc>package lius.chart.demo; 
<P>import java.io.IOException;<BR>import javax.servlet.*;<BR>import javax.servlet.http.HttpServlet;</P>
<P>import org.jfree.data.*;<BR>import org.jfree.chart.*;<BR>/**<BR>* 演示通过servlet直接输出图表<BR>* @author Winter Lau<BR>*/<BR>public class ChartDemoServlet extends HttpServlet {</P>
<P>public void service(ServletRequest req, ServletResponse res)<BR>throws ServletException, IOException <BR>{<BR>res.setContentType("image/jpeg");<BR>DefaultPieDataset data = getDataSet();<BR>JFreeChart chart = ChartFactory.createPie3DChart("水果产量图",<BR>data,<BR>true,<BR>false,<BR>false<BR>);<BR><BR>ChartUtilities.writeChartAsJPEG(res.getOutputStream(),<BR>100,chart,400,300,null);<BR>}<BR>/**<BR>* 获取一个演示用的简单数据集对象<BR>* @return<BR>*/<BR>private static DefaultPieDataset getDataSet() {<BR>DefaultPieDataset dataset = new DefaultPieDataset();<BR>dataset.setValue("苹果",100);<BR>dataset.setValue("梨子",200);<BR>dataset.setValue("葡萄",300);<BR>dataset.setValue("香蕉",400);<BR>dataset.setValue("荔枝",500);<BR>return dataset;<BR>}<BR>}</P></TD></TR></TBODY></TABLE></P></TD></TR></TBODY></TABLE>	(<a href=http://www.blogcn.com/user28/reene/blog/22124929.html target=_blank>浏览全文</a>&nbsp; <a href=http://www.blogcn.com/blog/trackback.asp?mydiary=22124929 target=_blank>引用此文</a>)<br>文章来源:<a href='http://www.blogcn.com/user28/reene/blog/22124929.html'>http://www.blogcn.com/user28/reene/blog/22124929.html</a><img src ="http://www.cnitblog.com/reene/aggbug/3183.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/reene/" target="_blank">快乐着飞舞着</a> 2005-10-09 13:07 <a href="http://www.cnitblog.com/reene/archive/2005/10/09/3183.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>用Java Swing制作欢迎屏幕(2)</title><link>http://www.cnitblog.com/reene/archive/2005/10/09/3180.html</link><dc:creator>快乐着飞舞着</dc:creator><author>快乐着飞舞着</author><pubDate>Sun, 09 Oct 2005 05:07:00 GMT</pubDate><guid>http://www.cnitblog.com/reene/archive/2005/10/09/3180.html</guid><wfw:comment>http://www.cnitblog.com/reene/comments/3180.html</wfw:comment><comments>http://www.cnitblog.com/reene/archive/2005/10/09/3180.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/reene/comments/commentRss/3180.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/reene/services/trackbacks/3180.html</trackback:ping><description><![CDATA[<TABLE style="BORDER-RIGHT: #6595d6 1px dotted; TABLE-LAYOUT: fixed; BORDER-TOP: #6595d6 1px dotted; FONT-SIZE: 12px; BORDER-LEFT: #6595d6 1px dotted; BORDER-BOTTOM: #6595d6 1px dotted" cellSpacing=0 cellPadding=6 width="95%" align=center border=0>
<TBODY>
<TR>
<TD style="WORD-WRAP: break-word" bgColor=#e8f4ff>
<P><FONT size=3>现在我们有了一个很不错的欢迎屏幕，它可以通过点击的方法关闭，但它不会自己消失。接下来我们要加入代码，使得欢迎屏幕在显示一定的时间之后自动消失。这里我们要考虑到运用线程。<BR><BR></FONT>
<TABLE width="90%" bgColor=#ffffff border=0>
<TBODY>
<TR>
<TD>class SplashWindow3 extends JWindow<BR>{<BR>　public SplashWindow3(String filename, Frame f, int waitTime)<BR>　{<BR>　　super(f);<BR>　　JLabel l = new JLabel(new ImageIcon(filename));<BR>　　getContentPane().add(l, BorderLayout.CENTER);<BR>　　pack();<BR>　　Dimension screenSize =Toolkit.getDefaultToolkit().getScreenSize();<BR>　　Dimension labelSize = l.getPreferredSize();<BR>　　setLocation(screenSize.width/2 - (labelSize.width/2),<BR>　　screenSize.height/2 - (labelSize.height/2));<BR>　　addMouseListener(new MouseAdapter()<BR>　　{<BR>　　　public void mousePressed(MouseEvent e)<BR>　　　{<BR>　　　　setVisible(false);<BR>　　　　dispose();<BR>　　　}<BR>　　});<BR>　　final int pause = waitTime;<BR>　　final Runnable closerRunner = new Runnable()<BR>　　{<BR>　　　public void run()<BR>　　　{<BR>　　　　setVisible(false);<BR>　　　　dispose();<BR>　　　}<BR>　　};<BR>　　Runnable waitRunner = new Runnable()<BR>　　{<BR>　　　public void run()<BR>　　　{<BR>　　　　try<BR>　　　　{<BR>　　　　　Thread.sleep(pause);<BR>　　　　　SwingUtilities.invokeAndWait(closerRunner);<BR>　　　　}<BR>　　　　catch(Exception e)<BR>　　　　{<BR>　　　　　e.printStackTrace();<BR>　　　　　// 能够捕获InvocationTargetException<BR>　　　　　// 能够捕获InterruptedException<BR>　　　　}<BR>　　　}<BR>　　};<BR>　　setVisible(true);<BR>　　Thread splashThread = new Thread(waitRunner, "SplashThread");<BR>　　splashThread.start();<BR>　}<BR>}</TD></TR></TBODY></TABLE><BR>　　这里的基本思路是利用一个在一定时间内暂停等待的Thread对象。在上面的代码中，线程的暂停时间是4秒。当这个线程唤醒时，它将关闭欢迎屏幕。由于Swing是非线程安全的，除非代码在事件分派线程上执行，否则它就不应该影响任何UI组件的状态。所谓事件分派线程，就是Swing中负责绘图和事件处理的线程。<BR><BR>　　为了解决这个问题，Swing设计者赋予我们安全地把Runnable对象加入UI事件队列的能力。在本例中，我们用可运行对象closerRunner完成最关键的工作。我们把可运行对象传入SwingUtilities.invokeAndWait()静态方法，然后wingUtilities.invokeAndWait()进行所有未完成的UI操作，并执行传递给该方法的可运行对象closerRunner的run方法。通过运用一个独立的线程负责欢迎屏幕的关闭操作，应用担负起了显示和关闭欢迎屏幕之间的所有操作。<BR><BR>　　如果要让欢迎屏幕总是显示且用户不能关闭它，你必须删除那些隐藏欢迎屏幕的代码。如果要让欢迎屏幕只能由用户手工关闭，你可以象使用任何其他JWindow对象一样调用SplashWindow3对象上的setVisible(false)和dispose()方法。<BR></P></TD></TR></TBODY></TABLE>	(<a href=http://www.blogcn.com/user28/reene/blog/22125464.html target=_blank>浏览全文</a>&nbsp; <a href=http://www.blogcn.com/blog/trackback.asp?mydiary=22125464 target=_blank>引用此文</a>)<br>文章来源:<a href='http://www.blogcn.com/user28/reene/blog/22125464.html'>http://www.blogcn.com/user28/reene/blog/22125464.html</a><img src ="http://www.cnitblog.com/reene/aggbug/3180.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/reene/" target="_blank">快乐着飞舞着</a> 2005-10-09 13:07 <a href="http://www.cnitblog.com/reene/archive/2005/10/09/3180.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>用Java Swing制作欢迎屏幕(1) [转]</title><link>http://www.cnitblog.com/reene/archive/2005/10/09/3181.html</link><dc:creator>快乐着飞舞着</dc:creator><author>快乐着飞舞着</author><pubDate>Sun, 09 Oct 2005 05:07:00 GMT</pubDate><guid>http://www.cnitblog.com/reene/archive/2005/10/09/3181.html</guid><wfw:comment>http://www.cnitblog.com/reene/comments/3181.html</wfw:comment><comments>http://www.cnitblog.com/reene/archive/2005/10/09/3181.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/reene/comments/commentRss/3181.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/reene/services/trackbacks/3181.html</trackback:ping><description><![CDATA[<TABLE style="BORDER-RIGHT: #6595d6 1px dotted; TABLE-LAYOUT: fixed; BORDER-TOP: #6595d6 1px dotted; FONT-SIZE: 12px; BORDER-LEFT: #6595d6 1px dotted; BORDER-BOTTOM: #6595d6 1px dotted" cellSpacing=0 cellPadding=6 width="95%" align=center border=0>
<TBODY>
<TR>
<TD style="WORD-WRAP: break-word" bgColor=#e8f4ff>
<P><FONT size=3>几乎所有时髦的应用都有一个欢迎屏幕。欢迎屏幕既是宣传产品的方法之一，而且在长时间的应用启动过程中，欢迎屏幕还用来表示应用正在准备过程中。 <BR><BR></FONT><IFRAME align=center marginWidth=0 marginHeight=0 src="http://images.chinabyte.com/adjs/iframe-pip/y-software-pip.html" frameBorder=0 width=360 scrolling=no height=300></IFRAME><BR><BR><FONT size=3>　　下面是一个最简单的欢迎屏幕实现：<BR><BR></FONT>
<TABLE width="90%" bgColor=#ffffff border=0>
<TBODY>
<TR>
<TD>class SplashWindow1 extends JWindow<BR>{<BR>　public SplashWindow1(String filename, Frame f)<BR>　{<BR>　　super(f);<BR>　　JLabel l = new JLabel(new ImageIcon(filename));<BR>　　getContentPane().add(l, BorderLayout.CENTER);<BR>　　pack();<BR>　　Dimension screenSize =Toolkit.getDefaultToolkit().getScreenSize();<BR>　　Dimension labelSize = l.getPreferredSize();<BR>　　setLocation(screenSize.width/2 - (labelSize.width/2),<BR>　　screenSize.height/2 - (labelSize.height/2));<BR>　　setVisible(true);<BR>　　screenSize = null;<BR>　　labelSize = null;<BR>　}<BR>}</TD></TR></TBODY></TABLE><BR>　　SplashWindow1类从Swing的JWindow派生。JWindow是一个容器，它没有其他窗口所具有的各种窗口元素，如标题条、窗口管理按钮，甚至连突出显示的边框也没有。因此，JWindow对于制作欢迎屏幕来说是非常合适的。上面的代码假定图形文件在当前目录。图形通过ImageIcon装入内存，然后它就被放到了JWindow的中心。接着，窗口被pack()，这使得Swing把窗口调整到适当的大小，最后窗口被移到了屏幕的中心。<BR><BR>　　如果我们运行上面的程序，可以发现虽然欢迎画面确实出现在屏幕中央，但遗憾的，它却不会关闭！要关闭欢迎画面，我们需要加入更多的代码：<BR><BR>
<TABLE width="90%" bgColor=#ffffff border=0>
<TBODY>
<TR>
<TD>class SplashWindow2 extends JWindow<BR>{<BR>　public SplashWindow2(String filename, Frame f)<BR>　{<BR>　　super(f);<BR>　　JLabel l = new JLabel(new ImageIcon(filename));<BR>　　getContentPane().add(l, BorderLayout.CENTER);<BR>　　pack();<BR>　　Dimension screenSize =Toolkit.getDefaultToolkit().getScreenSize();<BR>　　Dimension labelSize = l.getPreferredSize();<BR>　　setLocation(screenSize.width/2 - (labelSize.width/2),<BR>　　screenSize.height/2 - (labelSize.height/2));<BR>　　addMouseListener(new MouseAdapter()<BR>　　{　　<BR>　　　public void mousePressed(MouseEvent e)<BR>　　　{<BR>　　　　setVisible(false);<BR>　　　　dispose();<BR>　　　}<BR>　　});<BR>　　setVisible(true);<BR>　}<BR>}</TD></TR></TBODY></TABLE><BR>　　和原先的SplashWindow1类相比，这个SplashWindow2类唯一的区别在于多出了一个安装到JWindow上的匿名MouseListener。经过这个改动之后，用户可以点击欢迎屏幕关闭它。</P></TD></TR></TBODY></TABLE>	(<a href=http://www.blogcn.com/user28/reene/blog/22125419.html target=_blank>浏览全文</a>&nbsp; <a href=http://www.blogcn.com/blog/trackback.asp?mydiary=22125419 target=_blank>引用此文</a>)<br>文章来源:<a href='http://www.blogcn.com/user28/reene/blog/22125419.html'>http://www.blogcn.com/user28/reene/blog/22125419.html</a><img src ="http://www.cnitblog.com/reene/aggbug/3181.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/reene/" target="_blank">快乐着飞舞着</a> 2005-10-09 13:07 <a href="http://www.cnitblog.com/reene/archive/2005/10/09/3181.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>使用 JFreeChart来创建基于web的图表（3）</title><link>http://www.cnitblog.com/reene/archive/2005/10/09/3182.html</link><dc:creator>快乐着飞舞着</dc:creator><author>快乐着飞舞着</author><pubDate>Sun, 09 Oct 2005 05:07:00 GMT</pubDate><guid>http://www.cnitblog.com/reene/archive/2005/10/09/3182.html</guid><wfw:comment>http://www.cnitblog.com/reene/comments/3182.html</wfw:comment><comments>http://www.cnitblog.com/reene/archive/2005/10/09/3182.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/reene/comments/commentRss/3182.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/reene/services/trackbacks/3182.html</trackback:ping><description><![CDATA[<TABLE style="BORDER-RIGHT: #6595d6 1px dotted; TABLE-LAYOUT: fixed; BORDER-TOP: #6595d6 1px dotted; FONT-SIZE: 12px; BORDER-LEFT: #6595d6 1px dotted; BORDER-BOTTOM: #6595d6 1px dotted" cellSpacing=0 cellPadding=6 width="95%" align=center border=0>
<TBODY>
<TR>
<TD style="WORD-WRAP: break-word" bgColor=#e8f4ff>
<P>很多情况我们不仅仅要求可以在浏览器上显示一个图表，我们更需要客户可以直接在图表上做一下交互的操作，例如获取信息提示，点击图表某个部分进行更详细信息的展示等等。例如前面生成的简单柱状图，用户需要在看到柱状图后点击某种水果例如是苹果即可看到各个地区苹果产量的情况。为此就要求该图形具有交互操作的功能。在HTML中为了让一个图像具有可交互的功能就必须给该图像定义一个Map对象。下表节选一段具有该功能的HTML代码 
<TABLE cellSpacing=0 width=500>
<TBODY>
<TR>
<TD bgColor=#cccccc>&lt;MAP NAME="chartMap"&gt;<BR>&lt;AREA SHAPE="RECT" COORDS="81,15,126,254" href="?series=0&amp;category=100" title="100 = 7,048" <BR>onclick="javascript:clickChart('100');return false;"&gt;<BR>&lt;AREA SHAPE="RECT" COORDS="143,27,188,255" href="?series=0&amp;category=200" title="200 = 6,721" <BR>onclick="javascript: clickChart ('200');return false;"&gt;<BR>&lt;AREA SHAPE="RECT" COORDS="205,54,250,255" href="?series=0&amp;category=300" title="300 = 5,929" <BR>onclick="javascript: clickChart ('300');return false;"&gt;<BR>&lt;AREA SHAPE="RECT" COORDS="267,85,312,255" href="?series=0&amp;category=400" title="400 = 5,005" <BR>onclick="javascript: clickChart ('400');return false;"&gt;<BR>&lt;AREA SHAPE="RECT" COORDS="329,17,374,255" href="?series=0&amp;category=Diet" title="Diet = 7,017" onclick="javascript: <BR>clickChart ('Diet');return false;"&gt;<BR>&lt;/MAP&gt;</TD></TR></TBODY></TABLE></P>
<P>由此就产生了一个问题：如果根据一个图像来生成对应的MAP对象。我们回头看看刚才的代码，在创建一个图表对象时候有两个参数，我们举柱状图的例子来讲这两个参数就是ChartFactory. createBarChart3D方法中的最后两个参数，这两个参数的类型都是布尔值。这两个参数意思分别是：是否创建工具提示(tooltip)以及是否生成URL。这两个参数分别对应着MAP中一个AREA的title属性以及href属性。 </P>
<P>可是我想知道的是怎么来产生这个MAP啊！哈哈，不要着急，JFreeChart已经帮我们做好生成MAP对象的功能。为了生成MAP对象就要引入另外一个对象：ChartRenderingInfo。因为JFreeChart没有直接的方法利用一个图表对象直接生成MAP数据，它需要一个中间对象来过渡，这个对象就是ChartRenderingInfo。下图是生成MAP数据的流程图： </P>
<P align=center>图7<BR><IMG src="http://www.ddvip.net/program/java/index6/img_java_webservice/image007.gif"></P>
<P>如上图所示，ChartUtilities类是整个流程的核心，它周围的对象都是一些例如数据对象或者是文件等。这个流程简单描述如下：首先创建一个ChartRenderingInfo对象并在调用ChartUtilities的writeChartAsJPEG时作为最后一个参数传递进去。调用该方法结束后将产生一个图像文件以及一个填充好MAP数据的ChartRenderingInfo对象，有了这个对象我们还是没有办法获取具体的MAP数据，我们还必须借助于ChartUtilities的writeImageMap方法来将ChartRenderingInfo对象读取出来，获取MAP数据的代码片断如下： </P>
<P>
<TABLE cellSpacing=0>
<TBODY>
<TR>
<TD bgColor=#cccccc>PrintWriter w = null;<BR>FileOutputStream fos_jpg = null;<BR>FileOutputStream fos_cri = null;<BR>try{<BR>//根据不同类型的图表使用不同类，以下是针对饼图的操作<BR>PiePlot plot = (PiePlot) chart.getPlot();<BR>plot.setURLGenerator(new StandardPieURLGenerator(url));<BR>//设置工具提示<BR>plot.setToolTipGenerator(new StandardPieToolTipGenerator());<BR>fos_jpg = new FileOutputStream(“d:\\fruit.jpg”); <BR>ChartUtilities.writeChartAsJPEG(<BR>fos_jpg,<BR>100,<BR>chart,<BR>400,<BR>300,<BR>info);<BR>fos_cri = new FileOutputStream(__d:\\fruit.map__);<BR>w = new PrintWriter(fos_cri);<BR>ChartUtilities.writeImageMap(w, __mapname__, info);<BR>w.flush();<BR>}finally{<BR>try{<BR>w.close();<BR>}catch(Exception e){} <BR>try{<BR>fos_cri.close();<BR>}catch(Exception e){} <BR>try{<BR>fos_jpg.close();<BR>}catch(Exception e){}<BR>}</TD></TR></TBODY></TABLE></P>
<P>打开文件D:\fruit.map，文件的内容就是要写到页面上的MAP数据。把生成的图像文件以及MAP数据文件写到页面上即可完成热点图表的功能。至于怎么结合两者之间的关系例如图像的useMap属性值必须与MAP对象的名称结合起来，必须根据实际的应用情况进行相应的处理。笔者建议把二者通过标签库封装起来，图像文件的名称以及MAP对象的名称由标签库统一进行控制，这样可以保证二者的一致性。 </P>
<P>关于作者<BR>刘冬，他已经不再叫凤梨罐头了。珠海市创我科技发展有限公司软件工程师，主要从事在J2EE平台上实现商业智能（BI）方面的开发。 <BR>联系电话：0756-3377435-383 <BR>电子邮件： winter.lau@163.com<BR></P></TD></TR></TBODY></TABLE>(<A href="http://www.blogcn.com/user28/reene/blog/22125000.html" target=_blank>浏览全文</A>&nbsp; <A href="http://www.blogcn.com/blog/trackback.asp?mydiary=22125000" target=_blank>引用此文</A>)<BR>文章来源:<A href="http://www.blogcn.com/user28/reene/blog/22125000.html">http://www.blogcn.com/user28/reene/blog/22125000.html</A><img src ="http://www.cnitblog.com/reene/aggbug/3182.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/reene/" target="_blank">快乐着飞舞着</a> 2005-10-09 13:07 <a href="http://www.cnitblog.com/reene/archive/2005/10/09/3182.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>JFreeChart实时曲线(代码) [zz]</title><link>http://www.cnitblog.com/reene/archive/2005/09/07/2552.html</link><dc:creator>快乐着飞舞着</dc:creator><author>快乐着飞舞着</author><pubDate>Wed, 07 Sep 2005 08:02:00 GMT</pubDate><guid>http://www.cnitblog.com/reene/archive/2005/09/07/2552.html</guid><wfw:comment>http://www.cnitblog.com/reene/comments/2552.html</wfw:comment><comments>http://www.cnitblog.com/reene/archive/2005/09/07/2552.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/reene/comments/commentRss/2552.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/reene/services/trackbacks/2552.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: &nbsp;&nbsp;1&nbsp;&nbsp;&nbsp;2&nbsp;package&nbsp;com.cityinforport.demo;&nbsp;&nbsp;3&nbsp;/**&nbsp;&nbsp;4&nbsp;&nbsp;*&nbsp;=============================================================&nbsp;&nbsp...&nbsp;&nbsp;<a href='http://www.cnitblog.com/reene/archive/2005/09/07/2552.html'>阅读全文</a><img src ="http://www.cnitblog.com/reene/aggbug/2552.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/reene/" target="_blank">快乐着飞舞着</a> 2005-09-07 16:02 <a href="http://www.cnitblog.com/reene/archive/2005/09/07/2552.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Java读取文件中含有中文的解决办法</title><link>http://www.cnitblog.com/reene/archive/2005/08/17/2149.html</link><dc:creator>快乐着飞舞着</dc:creator><author>快乐着飞舞着</author><pubDate>Wed, 17 Aug 2005 03:29:00 GMT</pubDate><guid>http://www.cnitblog.com/reene/archive/2005/08/17/2149.html</guid><wfw:comment>http://www.cnitblog.com/reene/comments/2149.html</wfw:comment><comments>http://www.cnitblog.com/reene/archive/2005/08/17/2149.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/reene/comments/commentRss/2149.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/reene/services/trackbacks/2149.html</trackback:ping><description><![CDATA[<DIV style="BORDER-RIGHT: windowtext 0.5pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 0.5pt solid; PADDING-LEFT: 5.4pt; BACKGROUND: #e6e6e6; PADDING-BOTTOM: 4px; BORDER-LEFT: windowtext 0.5pt solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: windowtext 0.5pt solid">
<DIV><FONT face="Courier New"><FONT size=2><SPAN style="COLOR: #008080">&nbsp;1</SPAN>&nbsp;</FONT></FONT><FONT face="Courier New"><FONT size=2><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;2</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;b</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">""</SPAN></FONT></FONT><FONT face="Courier New"><FONT size=2><SPAN style="COLOR: #000000">;<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;3</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;tmp</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #0000ff">null</SPAN></FONT></FONT><FONT face="Courier New"><FONT size=2><SPAN style="COLOR: #000000">;<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;4</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">try</SPAN></FONT></FONT><FONT face="Courier New"><FONT size=2><SPAN style="COLOR: #000000">&nbsp;{<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;5</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;File&nbsp;f</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;File(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">F:\\Eclipse\\MobileChart\\sms</SPAN><SPAN style="COLOR: #000000">"</SPAN></FONT></FONT><FONT face="Courier New"><FONT size=2><SPAN style="COLOR: #000000">);<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;6</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;InputStreamReader&nbsp;isr</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;InputStreamReader(</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;FileInputStream(f),</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">gb2312</SPAN><SPAN style="COLOR: #000000">"</SPAN></FONT></FONT><FONT face="Courier New"><FONT size=2><SPAN style="COLOR: #000000">);<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;7</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BufferedReader&nbsp;br</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #0000ff">new</SPAN></FONT></FONT><FONT face="Courier New"><FONT size=2><SPAN style="COLOR: #000000">&nbsp;BufferedReader(isr);<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;8</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">while</SPAN><SPAN style="COLOR: #000000">((tmp</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">br.readLine())</SPAN><SPAN style="COLOR: #000000">!=</SPAN><SPAN style="COLOR: #0000ff">null</SPAN></FONT></FONT><FONT face="Courier New"><FONT size=2><SPAN style="COLOR: #000000">)<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;9</SPAN>&nbsp;</FONT></FONT><FONT face="Courier New"><FONT size=2><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<BR></SPAN><SPAN style="COLOR: #008080">10</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b</SPAN><SPAN style="COLOR: #000000">+=</SPAN></FONT></FONT><FONT face="Courier New"><FONT size=2><SPAN style="COLOR: #000000">tmp;<BR></SPAN><SPAN style="COLOR: #008080">11</SPAN>&nbsp;</FONT></FONT><FONT face="Courier New"><FONT size=2><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR></SPAN><SPAN style="COLOR: #008080">12</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN><SPAN style="COLOR: #0000ff">catch</SPAN></FONT></FONT><FONT face="Courier New"><FONT size=2><SPAN style="COLOR: #000000">(Exception&nbsp;e){<BR></SPAN><SPAN style="COLOR: #008080">13</SPAN>&nbsp;</FONT></FONT><FONT face="Courier New"><FONT size=2><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.printStackTrace();<BR></SPAN><SPAN style="COLOR: #008080">14</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></FONT></FONT></DIV></DIV>如上面代码所示 只要指定 编码形式就行了<img src ="http://www.cnitblog.com/reene/aggbug/2149.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/reene/" target="_blank">快乐着飞舞着</a> 2005-08-17 11:29 <a href="http://www.cnitblog.com/reene/archive/2005/08/17/2149.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Java正则表达式详解[转载]</title><link>http://www.cnitblog.com/reene/archive/2005/08/16/2128.html</link><dc:creator>快乐着飞舞着</dc:creator><author>快乐着飞舞着</author><pubDate>Tue, 16 Aug 2005 02:23:00 GMT</pubDate><guid>http://www.cnitblog.com/reene/archive/2005/08/16/2128.html</guid><wfw:comment>http://www.cnitblog.com/reene/comments/2128.html</wfw:comment><comments>http://www.cnitblog.com/reene/archive/2005/08/16/2128.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/reene/comments/commentRss/2128.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/reene/services/trackbacks/2128.html</trackback:ping><description><![CDATA[<TABLE width=680 align=center>
<TBODY>
<TR>
<TD align=middle>
<H2><FONT color=#0f3ccd>Java正则表达式详解</FONT></H2><BR><B>仙人掌工作室</B> </TD></TR>
<TR>
<TD align=right>01-7-31 下午 04:13:03<BR>
<HR width=660 color=#f46240 SIZE=1>
</TD></TR></TBODY></TABLE><BR>
<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="http://www.ccw.com.cn/htm/app/aprog/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="http://www.ccw.com.cn/htm/app/aprog/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="http://www.ccw.com.cn/htm/app/aprog/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="http://www.ccw.com.cn/htm/app/aprog/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="http://www.ccw.com.cn/htm/app/aprog/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="http://www.ccw.com.cn/htm/app/aprog/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="http://www.ccw.com.cn/htm/app/aprog/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="http://www.ccw.com.cn/htm/app/aprog/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="http://www.ccw.com.cn/htm/app/aprog/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="http://www.ccw.com.cn/htm/app/aprog/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="http://www.ccw.com.cn/htm/app/aprog/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="http://www.ccw.com.cn/htm/app/aprog/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="http://www.ccw.com.cn/htm/app/aprog/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="http://www.ccw.com.cn/htm/app/aprog/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="http://www.ccw.com.cn/htm/app/aprog/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="http://www.ccw.com.cn/htm/app/aprog/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="http://www.ccw.com.cn/htm/app/aprog/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="http://www.ccw.com.cn/htm/app/aprog/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="http://www.ccw.com.cn/htm/app/aprog/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="http://www.ccw.com.cn/htm/app/aprog/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="http://www.ccw.com.cn/htm/app/aprog/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="http://www.ccw.com.cn/htm/app/aprog/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="http://www.ccw.com.cn/htm/app/aprog/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="http://www.ccw.com.cn/htm/app/aprog/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="http://www.ccw.com.cn/htm/app/aprog/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="http://www.ccw.com.cn/htm/app/aprog/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="http://www.ccw.com.cn/htm/app/aprog/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="http://www.ccw.com.cn/htm/app/aprog/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="http://www.ccw.com.cn/htm/app/aprog/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="http://www.ccw.com.cn/htm/app/aprog/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="http://www.ccw.com.cn/htm/app/aprog/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="http://www.ccw.com.cn/htm/app/aprog/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="http://www.ccw.com.cn/htm/app/aprog/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="http://www.ccw.com.cn/htm/app/aprog/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="http://www.ccw.com.cn/htm/app/aprog/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="http://www.ccw.com.cn/htm/app/aprog/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="http://www.ccw.com.cn/htm/app/aprog/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="http://www.ccw.com.cn/htm/app/aprog/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="http://www.ccw.com.cn/htm/app/aprog/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.cnitblog.com/reene/aggbug/2128.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/reene/" target="_blank">快乐着飞舞着</a> 2005-08-16 10:23 <a href="http://www.cnitblog.com/reene/archive/2005/08/16/2128.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>正则表达式和Java编程语言[转载 ]</title><link>http://www.cnitblog.com/reene/archive/2005/08/16/2126.html</link><dc:creator>快乐着飞舞着</dc:creator><author>快乐着飞舞着</author><pubDate>Tue, 16 Aug 2005 02:22:00 GMT</pubDate><guid>http://www.cnitblog.com/reene/archive/2005/08/16/2126.html</guid><wfw:comment>http://www.cnitblog.com/reene/comments/2126.html</wfw:comment><comments>http://www.cnitblog.com/reene/archive/2005/08/16/2126.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/reene/comments/commentRss/2126.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/reene/services/trackbacks/2126.html</trackback:ping><description><![CDATA[<A href="http://www.yesky.com/SoftChannel/72342371961929728/20030611/1706928.shtml">http://www.yesky.com/SoftChannel/72342371961929728/20030611/1706928.shtml</A><BR><BR>应用程序常常需要有文本处理功能，比如单词查找、电子邮件确认或XML文档 集成。这通常会涉及到模式匹配。Perl、sed或awk等语言通过使用正则表达式来 改善模式匹配，正则表达式是一串字符，它所定义的模式可用来查找匹配的文本。 为了使用Java<FONT size=-2><SUP>TM</SUP></FONT>编程语言进行模式匹配，需 要使用带有许多<CODE><FONT face=新宋体>charAt</FONT></CODE>子字串的<CODE><FONT face=新宋体>StringTokenizer</FONT></CODE> 类，读取字母或符号以便处理文本。这常常导致复杂或凌乱的代码。 
<P>　　现在不一样了。</P>
<P>　　2平台标准版（J2SE<FONT size=-2><SUP>TM</SUP></FONT>）1.4版包含一个名 为<CODE><FONT face=新宋体>java.util.regex</FONT></CODE>的新软件包，使得使用正则表达式成为可能。 目前的功能包括元字符的使用，它赋予正则表达式极大的灵活性</P>
<P>　　本文概括地介绍了正则表达式的使用，并详细解释如何利用 <CODE><FONT face=新宋体>java.util.regex</FONT></CODE>软件包来使用正则表达式，用以下常见情形作为 例子：</P>
<P>
<UL>
<LI>简单的单词替换 
<LI>电子邮件确认 
<LI>从文件中删除控制字符 
<LI>查找文件 </LI></UL>
<P></P>
<P>　　为了编译这些例子中的代码和在应用程序中使用正则表达式，需要安装 J2SE 1.4版。</P>
<P>　　<B>构造正则表达式</B> 
<P>　　正则表达式是一种字符模式，它描述的是一组字符串。你可以使用 <CODE><FONT face=新宋体>java.util.regex</FONT></CODE>软件包，查找、显示或修改输入序列中出现的 某个模式的一部分或全部。</P>
<P>　　正则表达式最简单的形式是一个精确的字符串，比如“Java”或 “programming”。正则表达式匹配还允许你检查一个字符串是否符合某个具体的 句法形式，比如是不是一个电子邮件地址。</P>
<P>　　为了编写正则表达式，普通字符和特殊字符都要使用：</P>
<P>
<TABLE cellPadding=4 width=300 align=center border=1>
<TBODY>
<TR>
<TD>\$</TD>
<TD>^</TD>
<TD>.</TD>
<TD>*</TD></TR>
<TR>
<TD>+</TD>
<TD>?</TD>
<TD>['</TD>
<TD>']</TD></TR>
<TR>
<TD>\.</TD>&nbsp; 
<TD>&nbsp;</TD>
<TD>&nbsp;</TD>
<TD>&nbsp;</TD></TR></TBODY></TABLE>
<P>　　正则表达式中出现的任何其他字符都是普通字符，除非它前面有个 <CODE><FONT face=新宋体>\</FONT></CODE>。</P>
<P>　　特殊字符有着特别的用处。例如，.可匹配除了换行符之外的任意字符。与 <CODE><FONT face=新宋体>s.n</FONT></CODE>这样的正则表达式匹配的是任何三个字符的、以<CODE><FONT face=新宋体>s</FONT></CODE> 开始以<CODE><FONT face=新宋体>n</FONT></CODE>结束的字符串，包括<CODE><FONT face=新宋体>sun</FONT></CODE>和<CODE><FONT face=新宋体>son</FONT></CODE> 。</P>
<P>　　在正则表达式中有许多特殊字符，可以查找一行开头的单词，忽略大小写或 大小写敏感的单词，还有特殊字符可以给出一个范围，比如<CODE><FONT face=新宋体>a-e</FONT></CODE>表 示从<CODE><FONT face=新宋体>a</FONT></CODE>到<CODE><FONT face=新宋体>e</FONT></CODE>的任何字母。</P>
<P>　　使用这个新软件包的正则表达式用法与Perl类似，所以如果你熟悉Perl中正则 表达式的使用，就可以在Java语言中使用同样的表达式语法。如果你不熟悉正则 表达式，下面是一些入门的例子：</P>
<P>
<TABLE cellSpacing=0 cellPadding=0 align=center border=0>
<TBODY>
<TR>
<TH height=30>构造</TH>
<TH height=30>匹配于</TH></TR>
<TR></TR>
<TR>
<TH align=left height=20>字符</TH>
<TH align=left height=20>&nbsp;</TH></TR>
<TR>
<TD vAlign=top height=20><I>x</I></TD>
<TD height=20>字符 <I>x</I></TD></TR>
<TR>
<TD vAlign=top height=20><CODE><FONT face=新宋体>\\</FONT></CODE></TD>
<TD height=20>反斜线字符</TD></TR>
<TR>
<TD vAlign=top height=20><CODE><FONT face=新宋体>\0</FONT></CODE><I>n</I></TD>
<TD height=20>八进制值的字符<CODE><FONT face=新宋体>0</FONT></CODE><I>n</I> (0&nbsp;<CODE><FONT face=新宋体>&lt;=</FONT></CODE>&nbsp;<I>n</I>&nbsp;<CODE><FONT face=新宋体>&lt;=</FONT></CODE>&nbsp;7)</TD></TR>
<TR>
<TD vAlign=top height=20><CODE><FONT face=新宋体>\0</FONT></CODE><I>nn</I></TD>
<TD height=20>八进制值的字符 <CODE><FONT face=新宋体>0</FONT></CODE><I>nn</I> (0&nbsp;<CODE><FONT face=新宋体>&lt;=</FONT></CODE>&nbsp;<I>n</I>&nbsp;<CODE><FONT face=新宋体>&lt;=</FONT></CODE>&nbsp;7)</TD></TR>
<TR>
<TD vAlign=top height=20><CODE><FONT face=新宋体>\0</FONT></CODE><I>mnn</I></TD>
<TD height=20>八进制值的字符0mnn <CODE><FONT face=新宋体>0</FONT></CODE><I>mnn</I> (0&nbsp;<CODE><FONT face=新宋体>&lt;=</FONT></CODE>&nbsp;<I>m</I>&nbsp;<CODE><FONT face=新宋体>&lt;=</FONT></CODE>&nbsp;3, 0&nbsp;<CODE><FONT face=新宋体>&lt;=</FONT></CODE>&nbsp;<I>n</I>&nbsp;<CODE><FONT face=新宋体>&lt;=</FONT></CODE>&nbsp;7)</TD></TR>
<TR>
<TD vAlign=top height=20><CODE><FONT face=新宋体>\x</FONT></CODE><I>hh</I></TD>
<TD height=20>十六进制值的字符<CODE><FONT face=新宋体>0x</FONT></CODE><I>hh</I></TD></TR>
<TR>
<TD vAlign=top height=20><CODE><FONT face=新宋体>\u</FONT></CODE><I>hhhh</I></TD>
<TD height=20>十六进制值的字符<CODE><FONT face=新宋体>0x</FONT></CODE><I>hhhh</I></TD></TR>
<TR>
<TD vAlign=top height=20><CODE><FONT face=新宋体>\t</FONT></CODE></TD>
<TD height=20>制表符(<CODE><FONT face=新宋体>'\u0009'</FONT></CODE>)</TD></TR>
<TR>
<TD vAlign=top height=20><CODE><FONT face=新宋体>\n</FONT></CODE></TD>
<TD height=20>换行符 (<CODE><FONT face=新宋体>'\u000A'</FONT></CODE>)</TD></TR>
<TR>
<TD vAlign=top height=20><CODE><FONT face=新宋体>\r</FONT></CODE></TD>
<TD height=20>回车符 (<CODE><FONT face=新宋体>'\u000D'</FONT></CODE>)</TD></TR>
<TR>
<TD vAlign=top height=20><CODE><FONT face=新宋体>\f</FONT></CODE></TD>
<TD height=20>换页符 (<CODE><FONT face=新宋体>'\u000C'</FONT></CODE>)</TD></TR>
<TR>
<TD vAlign=top height=20><CODE><FONT face=新宋体>\a</FONT></CODE></TD>
<TD height=20>响铃符 (<CODE><FONT face=新宋体>'\u0007'</FONT></CODE>)</TD></TR>
<TR>
<TD vAlign=top height=20><CODE><FONT face=新宋体>\e</FONT></CODE></TD>
<TD height=20>转义符 (<CODE><FONT face=新宋体>'\u001B'</FONT></CODE>)</TD></TR>
<TR>
<TD vAlign=top height=20><CODE><FONT face=新宋体>\c</FONT></CODE><I>x</I></TD>
<TD height=20>T对应于x的控制字符 <I>x</I></TD></TR>
<TR>
<TH height=20>&nbsp;</TH></TR>
<TR>
<TH align=left colSpan=2 height=25>字符类</TH></TR>
<TR>
<TD vAlign=top height=20><CODE><FONT face=新宋体>[abc]</FONT></CODE></TD>
<TD height=20><CODE><FONT face=新宋体>a</FONT></CODE>, <CODE><FONT face=新宋体>b</FONT></CODE>, or <CODE><FONT face=新宋体>c</FONT></CODE> (简单类)</TD></TR>
<TR>
<TD vAlign=top height=20><CODE><FONT face=新宋体>[^abc]</FONT></CODE></TD>
<TD height=20>除了<CODE><FONT face=新宋体>a</FONT></CODE>、<CODE><FONT face=新宋体>b</FONT></CODE>或<CODE><FONT face=新宋体>c</FONT></CODE>之外的任意 字符（求反）</TD></TR>
<TR>
<TD vAlign=top height=20><CODE><FONT face=新宋体>[a-zA-Z]</FONT></CODE></TD>
<TD height=20><CODE><FONT face=新宋体>a</FONT></CODE>到<CODE><FONT face=新宋体>z</FONT></CODE>或<CODE><FONT face=新宋体>A</FONT></CODE>到<CODE><FONT face=新宋体>Z</FONT></CODE> ，包含（范围)</TD></TR>
<TR>
<TD vAlign=top height=20><CODE><FONT face=新宋体>[a-z-[bc]]</FONT></CODE></TD>
<TD height=20><CODE><FONT face=新宋体>a</FONT></CODE>到<CODE><FONT face=新宋体>z</FONT></CODE>，除了<CODE><FONT face=新宋体>b</FONT></CODE>和<CODE><FONT face=新宋体>c</FONT></CODE> ： <CODE><FONT face=新宋体>[ad-z]</FONT></CODE>（减去）</TD></TR>
<TR>
<TD vAlign=top height=20><CODE><FONT face=新宋体>[a-z-[m-p]]</FONT></CODE></TD>
<TD height=20><CODE><FONT face=新宋体>a</FONT></CODE>到<CODE><FONT face=新宋体>z</FONT></CODE>，除了<CODE><FONT face=新宋体>m</FONT></CODE>到 <CODE><FONT face=新宋体>p</FONT></CODE>： <CODE><FONT face=新宋体>[a-lq-z]</FONT></CODE></TD></TR>
<TR>
<TD vAlign=top height=20><CODE><FONT face=新宋体>[a-z-[^def]]</FONT></CODE></TD>
<TD height=20><CODE><FONT face=新宋体>d</FONT></CODE>, <CODE><FONT face=新宋体>e</FONT></CODE>, 或 <CODE><FONT face=新宋体>f</FONT></CODE></TD>
<TR></TR>
<TR>
<TH align=left colSpan=2 height=30>预定义的字符类</TH></TR>
<TR>
<TD vAlign=top height=20><CODE><FONT face=新宋体>.</FONT></CODE></TD>
<TD height=20>任意字符（也许能与行终止符匹配，也许不能）</TD></TR>
<TR>
<TD vAlign=top height=20><CODE><FONT face=新宋体>\d</FONT></CODE></TD>
<TD height=20>数字: <CODE><FONT face=新宋体>[0-9]</FONT></CODE></TD></TR>
<TR>
<TD vAlign=top height=20><CODE><FONT face=新宋体>\D</FONT></CODE></TD>
<TD height=20>非数字: <CODE><FONT face=新宋体>[^0-9]</FONT></CODE></TD></TR>
<TR>
<TD vAlign=top height=20><CODE><FONT face=新宋体>\s</FONT></CODE></TD>
<TD height=20>空格符: <CODE><FONT face=新宋体>[ \t\n\x0B\f\r]</FONT></CODE></TD></TR>
<TR>
<TD vAlign=top height=20><CODE><FONT face=新宋体>\S</FONT></CODE></TD>
<TD height=20>非空格符: <CODE><FONT face=新宋体>[^\s]</FONT></CODE></TD></TR>
<TR>
<TD vAlign=top height=20><CODE><FONT face=新宋体>\w</FONT></CODE></TD>
<TD height=20>单词字符: <CODE><FONT face=新宋体>[a-zA-Z_0-9]</FONT></CODE></TD></TR>
<TR>
<TD vAlign=top height=20><CODE><FONT face=新宋体>\W</FONT></CODE></TD>
<TD height=20>非单词字符: <CODE><FONT face=新宋体>[^\w]</FONT></CODE></TD></TR></TBODY></TABLE></P><img src ="http://www.cnitblog.com/reene/aggbug/2126.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/reene/" target="_blank">快乐着飞舞着</a> 2005-08-16 10:22 <a href="http://www.cnitblog.com/reene/archive/2005/08/16/2126.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>java正则表达式; regular expression[转载]</title><link>http://www.cnitblog.com/reene/archive/2005/08/16/2127.html</link><dc:creator>快乐着飞舞着</dc:creator><author>快乐着飞舞着</author><pubDate>Tue, 16 Aug 2005 02:22:00 GMT</pubDate><guid>http://www.cnitblog.com/reene/archive/2005/08/16/2127.html</guid><wfw:comment>http://www.cnitblog.com/reene/comments/2127.html</wfw:comment><comments>http://www.cnitblog.com/reene/archive/2005/08/16/2127.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/reene/comments/commentRss/2127.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/reene/services/trackbacks/2127.html</trackback:ping><description><![CDATA[<TABLE class=showinfo style="TABLE-LAYOUT: fixed; WORD-BREAK: break-all" cellSpacing=0 cellPadding=3 width="100%" align=center border=0>
<TBODY>
<TR>
<TD class=showTitle align=middle>java正则表达式; regular expression</TD></TR>
<TR>
<TD>
<TABLE cellSpacing=0 cellPadding=0 align=left border=0>
<TBODY>
<TR>
<TD>
<SCRIPT language=javascript src="/ad/js/edu_left_300-300.js"></SCRIPT>
</TD></TR></TBODY></TABLE>概要 <BR><BR>文本处理经常涉及的根据一个pattern的匹配。尽管java的character和assorted 的String类提供了low-level的pattern-matching支持，这种支持一般带来了复杂的代码。为了帮助你书写简单的pattern-matching代码，java提供了regular expression。在介绍给你术语和java.util.regex包之后，Jeff Friesen explores 了许多那个包的Pattern类支持的正则表达式结构。然后他examines 了Pattern的方法和附加的java.util.regex 类。作为结束，他提供了一个正则表达式的实践应用。 <BR><BR>为察看术语列表，提示与警告，新的homework，上个月homework的回答，这篇文章的相关材料，请访问study guide. (6,000 words; February 7, 2003) <BR><BR>By Jeff Friesen ，Translated By humx <BR><BR>文本处理经常的要求依据特定pattern匹配的代码。它能让文本检索，email header验证，从普通文本的自定义文本的创建（例如，用"Dear Mr. Smith" 替代 "Dear Customer"），等等成为可能。Java通过character和assorted string类支持pattern matching。由于low-level的支持一般带来了复杂的pattern-matching代码，java同时提供了regular expression来简代码。 <BR><BR>Regular expressions经常让新手迷惑。然而, 这篇文章驱散了大部分混淆。在介绍了regular expression术语，java.util.regex 包中的类, 和一个regular expression constructs的示例程序之后, 我explore了许多Pattern类支持的regular expression constructs。我也examine了组成Pattern 和java.util.regex 包中其它类的方法。一个practical 的正则表达式的应用程序结束了我的讨论。 <BR><BR>Note <BR><BR>Regular expressions的漫长历史开始于计算机科学理论领域自动控制原理和formal 语言理论。它的历史延续到Unix和其它的操作系统，在那里正则表达式被经常用作在Unix和Unix-like的工具中：像awk（一个由其创作者，Aho, Weinberger, and Kernighan，命名，能够进行文本分析处理的编程语言）, emacs (一个开发工具)，和grep (一个在一个或多个文件中匹配正则表达式，为了全局地正则表达式打印的工具。 <BR><BR><BR>什么是正则表达式? <BR>A regular expression，也被known as regex or regexp，是一个描述了一个字符串集合的pattern(template)。这个pattern决定了什么样的字符串属于这个集合，它由文本字符和元字符（metacharacters，由有特殊的而不是字符含义的字符）组成。为了识别匹配的检索文本的过程—字符串满足一个正则表达式—称作模式匹配（pattern matching）。 <BR><BR>Java''s java.util.regex 包通过Pattern，Matcher类和PatternSyntaxException异常支持pattern matching： <BR><BR>Pattern 对象，被known as patterns，是编译的正则表达式。 <BR>Matcher 对象，或者matchers，在，实现了java.lang.CharSequence接口并作为文本source的字符序列中定位解释matchers的引擎。 <BR>PatternSyntaxException 对象描述非法的regex patterns。 <BR>Listing 1 介绍这些类： <BR><BR>Listing 1. RegexDemo.java <BR><BR>// RegexDemo.java <BR>import java.util.regex.*; <BR><BR><BR>class RegexDemo { <BR>public static void main (String [] args) { <BR>if (args.length != 2) <BR>System.err.println ("java RegexDemo regex text"); <BR>return; <BR>} <BR>Pattern p; <BR>try { <BR>p = Pattern.compile (args [0]); <BR>} <BR>catch (PatternSyntaxException e) { <BR>System.err.println ("Regex syntax error: " + e.getMessage ()); <BR>System.err.println ("Error description: " + e.getDescription ()); <BR>System.err.println ("Error index: " + e.getIndex ()); <BR>System.err.println ("Erroneous pattern: " + e.getPattern ()); <BR>return; <BR>} <BR><BR>String s = cvtLineTerminators (args [1]); <BR>Matcher m = p.matcher (s); <BR><BR>System.out.println ("Regex = " + args [0]); <BR>System.out.println ("Text = " + s); <BR>System.out.println (); <BR>while (m.find ()) { <BR>System.out.println ("Found " + m.group ()); <BR>System.out.println (" starting at index " + m.start () + <BR>" and ending at index " + m.end ()); <BR>System.out.println (); <BR>} <BR>} <BR><BR>// Convert \n and \r character sequences to their single character <BR>// equivalents <BR><BR>static String cvtLineTerminators (String s) { <BR>StringBuffer sb = new StringBuffer (80); <BR><BR>int oldindex = 0, newindex; <BR>while ((newindex = s.indexOf ("\\n", oldindex)) != -1) { <BR>sb.append (s.substring (oldindex, newindex)); <BR>oldindex = newindex + 2; <BR>sb.append (''\n''); <BR>} <BR>sb.append (s.substring (oldindex)); <BR><BR>s = sb.toString (); <BR><BR>sb = new StringBuffer (80); <BR><BR>oldindex = 0; <BR>while ((newindex = s.indexOf ("\\r", oldindex)) != -1) { <BR>sb.append (s.substring (oldindex, newindex)); <BR>oldindex = newindex + 2; <BR>sb.append (''\r''); <BR>} <BR>sb.append (s.substring (oldindex)); <BR><BR>return sb.toString (); <BR>} <BR>} <BR><BR>RegexDemo''s public static void main(String [] args) 方法validates 两个命令行参数：一个指出正则表达式，另外一个指出文本。在创建一个pattern之后，这个方法转换所有的文本参数，new-line and carriage-return line-terminator 字符序列为它们的实际meanings 。例如，一个new-line字符序列(由反斜杠后跟n表示)转换成一个new-line字符（用数字表示为10）。在输出了regex和被转换的命令行文本参数之后，main(String [] args) 方法从pattern创建了一个matcher，它随后查找了所有的matches 。对于每一个match，它所出现的字符和信息的位置被输出。 <BR><BR>为了完成模式匹配，RegexDemo 调用了java.util.regex包中类的不同的方法。不要使你自己现在就理解这些方法；我们将在后边的文章探讨它们。更重要的是，编译 Listing 1: 你需要RegexDemo.class来探索Pattern''s regex 结构。 <BR><BR>探索Pattern''s regex 构造 <BR><BR>Pattern''s SDK 文档提供了一部分正则表达式结构的文档。除非你是一个avid正则表达式使用者，一个最初的那段文档的阅读会让你迷惑。什么是quantifiers，greedy之间的不同是什么, reluctant, 和 possessive quantifiers? 什么是 character classes， boundary matchers， back references, 和 embedded flag expressions? 为了回答这些和其它的问题，我们探索了许多Patter认可的regex constructs或 regex pattern 种类。我们从最简单的regex construct 开始：literal strings。 <BR><BR>Caution <BR><BR>不要认为Pattern和Perl5的正则表达式结构是一样的。尽管他们有很多相同点，他们也有许多，它们支持的metacharacters结构的不同点。 (更多信息，察看在你的平台上的你的SDK Pattern类的文档。) <BR><BR><BR>Literal strings <BR><BR>当你在字处理软件的检索对话框输入一个你指定一个literal string 的时候，你就指定了一个regex expression construct 。执行以下的RegexDemo 命令行来察看一下这个regex construct 的动作： <BR><BR>java RegexDemo apple applet <BR><BR>上边的这个命令行确定了apple 作为一个包含了字符a, p, p, l, and e（依次）的字符regex construct。 这个命令行同时也确定了applet 作为pattern-matching的文本。执行命令行以后，看到以下输出： <BR><BR>Regex = apple <BR>Text = applet <BR><BR>Found apple <BR>starting at index 0 and ending at index 5 <BR><BR>输出的regex 和text 命令行，预示着在applet中一个applet的成功的匹配，并表示了匹配的开始和结束的索引：分别为0和5。开始索引指出了一个pattern match出现的第一个文本的开始位置，结束索引指明了这个match后的第一个text的位置。换句话说，匹配的text的范围包含在开始索引和去掉结束索引之间（不包含结束索引）。 <BR><BR>Metacharacters <BR><BR>尽管string regex constructs 是有用的，更强大的regex contsruct联合了文本字符和元字符。例如，在a.b，这个句点metacharacter (.) 代表在a个b之间出现的任何字符。 为了察看元字符的动作， 执行以下命令行： <BR><BR>java RegexDemo .ox "The quick brown fox jumps over the lazy ox." <BR><BR>以上命令指出.ox 作为regex ，和The quick brown fox jumps over the lazy ox.作为文本源text。RegexDemo 检索text来匹配以任意字符开始以ox结束的match，并产生如下输出： <BR><BR>Regex = .ox <BR>Text = The quick brown fox jumps over the lazy ox. <BR><BR>Found fox <BR>starting at index 16 and ending at index 19 <BR><BR>Found ox <BR>starting at index 39 and ending at index 42 <BR><BR>这个输出展示了两个matches:fox和ox。. metacharacter 在第一个match中匹配f ，在第二个match中匹配空格。 <BR><BR>假如我们用前述的metacharacter 替换.ox会怎么样呢？也就是，我们指定java RegexDemo . "The quick brown fox jumps over the lazy ox."会有什么样的输出，因为period metacharacter 匹配任何字符， RegexDemo 在命令行输出每一个匹配字符，包括结尾的period字符。 <BR><BR>Tip <BR><BR>为了指定.或者任何的元字符作为在一个regex construct 作为literal character，引用—转换meta状态到literal status—用以下两种方法之一： <BR><BR>在元字符之前放置反斜杠。 <BR>将元字符放在\Q和\E之间（例如：\Q.\E）。 <BR>在每种情形下，不要忘记在string literal（例如：String regex = \\.; <BR><BR>）中出现时（像 \\. or \\Q.\\E）的双倍的反斜杠。不要在当它在命令行参数中出现的时候用双倍的反斜杠。 <BR><BR><BR>Character classes <BR><BR>有时我们限定产生的matches到一个特定的字符集和。例如，我们可以检索元音a, e, i, o, and u ，任何一个元音字符的出现都以为着一个match。A character类， 通过在方括号之间的一个字符集和指定的regex construct ，帮我们完成这个任务。Pattern 支持以下的character classes： <BR><BR>简单字符: 支持被依次放置的字符串并仅匹配这些字符。例如：[abc] 匹配字符a, b, and c。以下的命令行提供了另外一个示例： <BR>java RegexDemo [csw] cave <BR><BR>java RegexDemo [csw] cave [csw]中c匹配在cave中的c。没有其它的匹配存在。 <BR><BR>否定: 以^ metacharacter 元字符开始且仅匹配没有在class中出现的字符。例如：[^abc]匹配所有除了a, b, 和c以外的字符，以下的命令行提供了另外一个示例： <BR>java RegexDemo [^csw] cave <BR><BR>java RegexDemo [^csw] cave 匹配在cave中遇到的a, v, 和e。没有其它的匹配存在。 <BR><BR>范围: 包含在元字符（-）左侧的字符开始，元字符（-）右侧字符结束的所有字符。仅匹配在范围内的字符。例如： [a-z] 匹配所有的小写字母。以下的命令行提供了另外一个示例： <BR>java RegexDemo [a-c] clown <BR><BR>java RegexDemo [a-c] clown 匹配在clown中的c。没有其它的匹配存在。 <BR><BR>Tip <BR><BR>通过将它们放置在一起来在一个range character class内联合多个范围。例如：[a-zA-Z] 匹配所有的大写和小写字母。 <BR><BR><BR>联合: 由多个嵌套的character classes组成，匹配属于联合结果的所有字符。例如：[a-d[m-p]] 匹配字符a到d和m到p。 characters a through d and m through p。以下的命令行提供了另外一个示例： <BR>java RegexDemo [ab[c-e]] abcdef <BR><BR>java RegexDemo [ab[c-e]] abcdef 匹配它们在abcdef中的副本 a, b, c, d, and e。没有其它的匹配存在。 <BR><BR>交集: 由所有嵌套的class的共同部分组成，且仅匹配字符的共同部分。例如：[a-z&amp;&amp;[d-f]] 匹配字符d, e, 和 f。以下的命令行提供了另外一个示例： <BR>java RegexDemo [aeiouy&amp;&amp;[y]] party <BR><BR>java RegexDemo [aeiouy&amp;&amp;[y]] party 匹配在party中的y。没有其它的匹配存在。 <BR><BR>差集: 由除了那些在否定嵌套character class中的字符外所有保留的字符组成。例如：[a-z&amp;&amp;[^m-p]] 匹配 字符a到l和q到z。以下的命令行提供了另外一个示例： <BR>java RegexDemo [a-f&amp;&amp;[^a-c]&amp;&amp;[^e]] abcdefg <BR><BR>java RegexDemo [a-f&amp;&amp;[^a-c]&amp;&amp;[^e]] abcdefg 匹配在abcdefg中的 d 和f。没有其它的匹配存在。 <BR><BR>预定义的character classes <BR><BR>一些在regexes 中出现足够次数的character classes 提供了shortcuts。Pattern用预定义的character class提供了这样的shortcuts，如Table 1所示。使用预定义的character classes来简化你的regexes和最小化regex语法错误。 <BR><BR>Table 1. 预定义的character classes <BR><BR>Predefined character class <BR>Description <BR><BR>\d <BR>A 数字。 相当于[0-9]。 <BR><BR>\D <BR>A 非数字。相当于[^0-9]。 <BR><BR>\s <BR>A whitespace character。相当于[ \t\n\x0B\f\r]。 <BR><BR>\S <BR>A 非空格字符。相当于[^\s]。 <BR><BR>\w <BR>A 一个字符。相当于[a-zA-Z_0-9]。 <BR><BR>\W <BR>A 非字符，相当于[^\w]。 <BR><BR><BR><BR><BR>随后的命令行示例使用了\w预定义character class来identify 命令行中的所有word characters。 <BR><BR>java RegexDemo \w "aZ.8 _" <BR><BR>上边的命令行产生了以下的输出，它展示了句点和space characters 不被考虑为word character： <BR><BR>Regex = \w <BR>Text = aZ.8 _ <BR><BR>Found a <BR>starting at index 0 and ending at index 1 <BR><BR>Found Z <BR>starting at index 1 and ending at index 2 <BR><BR>Found 8 <BR>starting at index 3 and ending at index 4 <BR><BR>Found _ <BR>starting at index 5 and ending at index 6 <BR><BR>Note <BR><BR>Pattern的SDK文档引用句点元字符作为匹配除了line terminator，一个或者两个标志一行结束的预定义的标志之外的任何字符，除非 dotall mode (随后讨论)有效。Pattern 识别以下line terminators： <BR><BR>The 回车符 (\r\) <BR>The 回行符 (\n) <BR>The 回车符紧跟回行符 (\r\n) <BR>The 回行字符 (\u0085) <BR>The 行分割字符 (\u2028) <BR>The 段落分割字符 (\u2029) <BR><BR><BR>捕获组 <BR><BR>Pattern支持在pattern匹配的过程中，一个regex construct 调用capturing group 来保存为了以后recall 的match的字符；此结构是由圆括号包围的字符序列。在捕获的group中的所有字符在匹配的过程中被作为一个单独的单元。例如，(Java) capturing group 结合了字符 J, a, v, 和a为一个单独单元。Capturing group依据在text中Java的出现来匹配Java pattern。每一个match用下一个匹配的Java字符替代了前一个match保存的Java字符。 <BR><BR>Capturing groups 在其它capturing groups内被嵌套。例如：在(Java( language))，( language) 在(Java)内嵌套。每一个嵌套或非嵌套的capturing group有它自己的数字，数字从1开始，Capturing 数字从左边至右。在这个例子中，(Java( language))是capturing group 1，( language)是capturing group 2。在(a)(b)，(a)是捕获组1，(b)是捕获组2。 <BR><BR>每一个capturing group随后通过a back reference来recall保存的match。指定跟随在一个反斜杠后的数字来指示一个capturing group，back reference来 recalls 一个capturing group捕获的文本字符。一个back reference 的出现导致了一个matcher 使用the back reference的 capturing group number来recall捕获组保存的match ，然后使用匹配的字符进行进一步的匹配操作。随后的示例示范了为了检查语法错误进行的text 搜索的用法： <BR><BR>java RegexDemo "(Java( language)\2)" "The Java language language" <BR><BR>这个例子使用(Java( language)\2) regex为了检查语法错误，来检索字符串The Java language language，那里Java直接地在两个连续出现的language之前。Regex 指定了两个capturing groups： number 1 is (Java( language)\2)， 它匹配Java language language，number 2 is ( language), 它匹配由language跟随的space characer。\2 back reference recalls number 2''s 保存的match，它允许matcher检索空格后跟随language的第二次出现，which 直接地跟随space character and language的第一次出现。随后的输出显示了RegexDemo''s matcher 找到了什么： <BR><BR>Regex = (Java( language)\2) <BR>Text = The Java language language <BR><BR>Found Java language language <BR>starting at index 4 and ending at index 26 <BR><BR>量词 <BR><BR>Quantifiers大概是理解起来最让人迷惑的regex 结构。一部分混淆来自于尽力的理解18个量词逻辑（六个基本逻辑被组织为三个主要逻辑）。其它的一个混淆来自于费尽的0长度匹配的理解。一旦你理解了这个概念和18 categories， 大部分(假如不是全部)混淆将消失。 <BR><BR>Note <BR><BR>简要地, 着一部分主要讨论18 个quantifier categories 和zero-length 匹配的概念。为了更详尽的讨论和更多示例，请学习The Java Tutoria 的"Quantifiers"部分。 <BR><BR><BR>一个quantifier 是一个隐式或显示的为一个pattern绑定一个数量值的正则表达式结构。这个数字值解决定了匹配一个pattern的次数。Pattern的六个基本的quantifiers匹配一个pattern一次或者根本不匹配，0次或者多次，一次或者多次，一个精确的数字次数，至少x次和 至少x次但不超过y次。 <BR><BR>六个基本的quantifier categories 在每一个三个主要的类别：greedy， reluctant和 possessive中复制。Greedy quantifiers 尝试找到最长的匹配。与之对照，reluctant quantifiers 尝试找到最短的匹配。Possessive quantifiers 也尝试找到最长的匹配。然而，他们和greedy quantifies在工作方式上不同。尽管greedy 和possessive quantifiers 迫使一个matcher 在进行第一次匹配之前读取整个的text，greedy quantifiers 常常导致为了找到一个match进行多次尝试，然而possessive quantifiers 让一个matcher 仅尝试一个match一次。 <BR><BR>随后的示例描述了六种基本的quantifiers 在greedy category类别下，单个fundamental quantifier 在每一个 reluctant 和 possessive categories类别下的行为。这些示例也描述了0匹配的概念： <BR><BR>1. java RegexDemo a? abaa: 使用一个greedy quantifier 来在abaa中匹配a 一次或者根本不匹配。以下是输出结果： <BR><BR>Regex = a? <BR>Text = abaa <BR><BR>Found a <BR>starting at index 0 and ending at index 1 <BR><BR>Found <BR>starting at index 1 and ending at index 1 <BR><BR>Found a <BR>starting at index 2 and ending at index 3 <BR><BR>Found a <BR>starting at index 3 and ending at index 4 <BR><BR>Found <BR>starting at index 4 and ending at index 4 <BR><BR>这个输出展示了五次匹配。尽管第一、三和四次匹配的出现展示了三次匹配中位置并不奇怪，第一、第五次的匹配大概有点奇怪。这个匹配好像指出a匹配b和文本的结束。然而，不是这种情况。a?并不查找b和文本结尾。相反， 它查找a的出现或者缺失。当a? 查找a失败的时候，它以零长度的匹配返回那个事实(a缺失)，在零长度那里起始和结束位置的索引相同。Zero-length matches 发生在空文本, 最后一个文本字符之后，或者任何量个字符之间。 <BR><BR>2. java RegexDemo a* abaa: 使用一个greedy quantifier在abaa 中匹配a零次或多次。以下是输出结果： <BR><BR>Regex = a* <BR>Text = abaa <BR><BR>Found a <BR>starting at index 0 and ending at index 1 <BR><BR>Found <BR>starting at index 1 and ending at index 1 <BR><BR>Found aa <BR>starting at index 2 and ending at index 4 <BR><BR>Found <BR>starting at index 4 and ending at index 4 <BR><BR>输出展示了四次匹配。像使用 a?，a* 产生了zero-length 匹配。第三个匹配，a* 匹配了aa, 很有趣。不像 a?，a* 匹配一个或者多个连续的a。 <BR><BR>3. java RegexDemo a+ abaa: 使用一个greedy quantifier在abaa 中匹配a一次或多次。以下是输出结果： <BR><BR>Regex = a+ <BR>Text = abaa <BR><BR>Found a <BR>starting at index 0 and ending at index 1 <BR><BR>Found aa <BR>starting at index 2 and ending at index 4 <BR><BR>输出展示了两个匹配 。不像 a? 和 a*，a+ 没有匹配a的却失。因而，没有零长度匹配产生。像 a*，a+匹配了连续的a。 <BR><BR>4. java RegexDemo a{2} aababbaaaab: 使用greedy quantifier 来匹配中的每一个 aababbaaaab中的 aa序列。以下是输出结果： <BR><BR>Regex = a{2} <BR>Text = aababbaaaab <BR><BR>Found aa <BR>starting at index 0 and ending at index 2 <BR><BR>Found aa <BR>starting at index 6 and ending at index 8 <BR><BR>Found aa <BR>starting at index 8 and ending at index 10 <BR><BR>5. java RegexDemo a{2,} aababbaaaab: 使用了greedy quantifier 来匹配在ababbaaaab中两个或更多的匹配，以下是输出结果： <BR><BR>Regex = a{2,} <BR>Text = aababbaaaab <BR><BR>Found aa <BR>starting at index 0 and ending at index 2 <BR><BR>Found aaaa <BR>starting at index 6 and ending at index 10 <BR><BR>6. java RegexDemo a{1,3} aababbaaaab: 使用greedy quantifier 来匹配在aababbaaaab中出现的a、aa或者aaa。以下是输出结果： <BR><BR>Regex = a{1,3} <BR>Text = aababbaaaab <BR><BR>Found aa <BR>starting at index 0 and ending at index 2 <BR><BR>Found a <BR>starting at index 3 and ending at index 4 <BR><BR>Found aaa <BR>starting at index 6 and ending at index 9 <BR><BR>Found a <BR>starting at index 9 and ending at index 10 <BR><BR>7. java RegexDemo a+? abaa: 使用一个reluctant quantifier 在abaa中一次或多次匹配a。以下是输出结果： <BR><BR>Regex = a+? <BR>Text = abaa <BR><BR>Found a <BR>starting at index 0 and ending at index 1 <BR><BR>Found a <BR>starting at index 2 and ending at index 3 <BR><BR>Found a <BR>starting at index 3 and ending at index 4 <BR><BR>不像在第三个例中的greedy变量，reluctant 示例产生了三个单独的匹配，因为reluctant quantifier尽力的查找最短的匹配。 <BR><BR>8. java RegexDemo .*+end "This is the end": 使用了possessive quantifier 来匹配在this is the end中的以end结尾的任意字符0次或者多次。以下是输出结果： <BR><BR>Regex = .*+end <BR>Text = This is the end <BR><BR>由于这个possessive quantifier consume了整个文本，没有留下任何东西来匹配end，它没有产生匹配。相比之下，在java RegexDemo .*end "This is the end" 的greedy quantifier，因为它每次backing off一个字符直到最右端的end匹配，产生了一个匹配。(这个quantifier与greedy的不同就在后者的匹配过程中一旦匹配的字符，在随后的匹配中就不再使用。因此.*这部分正则表达式就匹配了全部的字符串，就没有字符可以与end匹配了。) <BR><BR>Boundary matchers <BR><BR>我们有时想在一行的开始、在单词的边界、文本的结束等等匹配pattern。使用 boundary matcher，一个指定了匹配边界的正则表达式结构，完成这个任务。Table 2 表示了Pattern的边界匹配支持。 <BR><BR>Table 2. Boundary matchers <BR><BR>Boundary Matcher <BR>Description <BR><BR>^ <BR>一行的开始 <BR><BR>$ <BR>一行的结束 <BR><BR>\b <BR>单词的边界 <BR><BR>\B <BR>非单词边界 <BR><BR>\A <BR>文本的开始 <BR><BR>\G <BR>前一个匹配的结束 <BR><BR>\Z <BR>The end of the text (but for the final line terminator, if any) <BR><BR>\z <BR>文本结束 <BR><BR><BR><BR><BR>下边的命令行示例使用了^ 边界匹配元字符 ensure 由零个或者多个字符跟随的行开始。 <BR><BR>java RegexDemo ^The\w* Therefore <BR><BR>^ 指出了前三个字符必须匹配pattern后的T、h和e字符。可跟随任意数量的字符。以上的命令行产生以下输出： <BR><BR>Regex = ^The\w* <BR>Text = Therefore <BR><BR>Found Therefore <BR>starting at index 0 and ending at index 9 <BR><BR>把命令行改为java RegexDemo ^The\w* " Therefore"。发生了什么事?因为在therefore前的一个空格，没有匹配被发现。 <BR><BR>Embedded flag expressions <BR><BR>Matcher假设了确定的却省值，例如大小写敏感的匹配。一个程序可以使用an embedded flag expression 来覆盖缺省值，也就是，使用一个正则表达式结构，圆括号元字符包围一个问号元字符后跟小写字母。Pattern认可以下的embedded flag expressions ： <BR><BR>(?i): enables 大小写不敏感的pattern 匹配。例如：java RegexDemo (?i)tree Treehouse 来匹配tree和Tree。大小写敏感是缺省值。 <BR>(?x): 允许空格和注释用#元字符开始出现在Pattern中。一个matcher 忽略全部它们。例如：java RegexDemo ".at(?x)#match hat, cat, and so on" matter 匹配.at和mat。缺省地，空格和注释式不被允许的；一个matcher 将它们考虑为对match有贡献的字符。 <BR>(?s): 使dotall 方式有效。在这种模式中，句点除了其它字符外还匹配text结束。 例如：java RegexDemo (?s). \n ，. 匹配了 \n。Nondotall 方式是缺省的：不匹配行结尾。 <BR>(?m): 使多行方式有效。在多行模式下 ^ and $ 恰好分别的在一行的终结或末端之后或者之前。例如：java RegexDemo (?m)^.ake make\rlake\n\rtake 匹配 .ake 和 make、 lake与 take。非多行模式是缺省的： ^ and $ match仅在整个文本的开始和结束。 <BR>(?u): enables Unicode-aware case folding. This flag works with (?i) to perform case-insensitive matching in a manner consistent with the Unicode Standard. The default: case-insensitive matching that assumes only characters in the US-ASCII character set match。 <BR>(?d): enables Unix lines mode. In that mode, a matcher recognizes only the \n line terminator in the context of the ., ^, and $ metacharacters. Non-Unix lines mode is the default: a matcher recognizes all terminators in the context of the aforementioned metacharacters。 <BR>Embedded flag expressions 类似于 capturing groups因为两个regex constructs都用圆括号包围字符。不像capturing group，embedded flag expression 没有捕获匹配的字符。因而，一个embedded flag expression是noncapturing group的特例。也就是说，一个不捕获text字符的regex construct ；它指定了由元字符圆括号包围的字符序列。在Pattern''s SDK 文档中出现了一些noncapturing groups。 <BR><BR>Tip <BR><BR>为了在正则表达式中指定多个embedded flag 表达式。或者吧它们并排的放在一起 (e.g., (?m)(?i)) 或者 把它们的小写字母并排的放在一起 (e.g., (?mi))。 <BR><BR><BR>探索java.util.regex 类的方法 <BR><BR>java.util.regex包的三个类提供了为帮我书写更健壮的正则表达式代码和创建强大的text处理工具许多的方法。 我们从Pattern类开始探索这些方法。 <BR><BR>Note <BR><BR>你也可以explore CharSequence 接口的当你创建一个新的字符序列类要实现的方法。仅实现 CharSequence 接口的类是java.nio.CharBuffer， String， 和 StringBuffer。 <BR><BR><BR>Pattern 方法 <BR><BR>除非代码将一个string编译为Pattern对象一个regex表达式式没有用处的。用以下编辑方法中的一个完成这个任务： <BR><BR>public static Pattern compile(String regex): 将regex内容编译为在一个新的Pattern对象内存储的树状结构的对象表示。返回那个对象引用。例如：Pattern p = Pattern.compile ("(?m)^\\.")；创建了一个，存储了一个编译的表示了匹配以句点开始的行的表示。 <BR>public static Pattern compile(String regex, int flags): 完成前一个方法的相同任务。然而，它同时考虑包含了flag常量（flags指定的）。Flag常量在Pattern中（except the canonical equivalence flag, CANON_EQ）被作为二选一的embedded flag expressions被声明。例如：Pattern p = Pattern.compile ("^\\.", Pattern.MULTILINE)；和上一个例子等价，Pattern.MULTILINE 常量和the (?m) embedded flag 表达式完成相同的任务。(参考SDK''s Pattern 文档学习其它的常量。) 假如不是这些在Pattern中被定义的常量在flag中出现，方法将抛出IllegalArgumentException 异常。 <BR>假如需要，通过调用以下方法可以得到一个Pattern对象的flag和最初的被编译为对象的正则表达式： <BR><BR>public int flags(): 返回当正则表达式编译时指定的Pattern的flag。例如：System.out.println (p.flags ())； 输出p引用的的Pattern相关的flag。 <BR>public String pattern(): 返回最初的被编译进Pattern的正则表达式。例如：System.out.println (p.pattern ())； 输出对应p引用Pattern的正则表达式。（Matcher 类包含了类似的返回Matcher相关的Pattern对象的Pattern pattern() 方法。） <BR>在创建一个Pattern对象后，你一般的通过调用Pattern的公有方法matcher(CharSequence text)获得一个Matcher对象。这个方法需要一个简单的，实现了CharSequence接口的文本对象参数。获得的对象在pattern匹配的过程中扫描输入的文本对象。例如：Pattern p = Pattern.compile ("[^aeiouy]"); Matcher m = p.matcher ("This is a test."); 获得一个在text中匹配所有非元音字母的matcher。 <BR><BR>当你想检查一个pattern是否完全的匹配一个文本序列的时候创建Pattern和Matcher对象是令人烦恼的。幸运的是，Pattern提供了一个方便的方法完成这个任务；public static boolean matches(String regex, CharSequence text)。当且仅当整个字符序列匹配regex的pattern的场合下静态方法返回布尔值true。例如：System.out.println (Pattern.matches ("[a-z[\\s]]*", "all lowercase letters and whitespace only"));返回布尔值true， 指出仅空格字符和小写字符在all lowercase letters and whitespace only中出现。 <BR><BR>书写代码将text分成它的组成部分(例如雇员记录文件到一个字段的set) 是许多开发者发现乏味的任务。Pattern 通过提供一对字符分割方法来减轻那种tedium。 <BR><BR>public String [] split(CharSequence text, int limit): 分割在当前的Pattern对象的pattern匹配周围的text。这个方法返回一个数组，它的每一个条目指定了一个从下一个由pattern匹配(或者文本结束)分开的字符序列；且所有条目以它们在text中出现相同的顺序存储。书组条目的数量依赖于limit，它同时也控制了匹配发生次数。一个正数意味着，至多，limit-1 个匹配被考虑且数组的长度不大于限定的条目数。一个负值以为着所有匹配的可能都被考虑且数组可以任意长。一个0值以为着所有可能匹配的条目都被考虑，数组可以有任意的长度，且尾部的空字符串被丢弃。 <BR>public String [] split(CharSequence text): 用0作为限制调用前边方法，返回方法调用的结果。 <BR>假如你想一个拆分雇员记录，包含了姓名，年龄，街道和工资，为它的组成部分。以下的代码用split(CharSequence text)方法完成了这个任务： <BR><BR>Pattern p = Pattern.compile (",\\s"); <BR>String [] fields = p.split ("John Doe, 47, Hillsboro Road, 32000"); <BR>for (int i = 0; i &lt; fields.length; i++) <BR>System.out.println (fields [i]); <BR><BR>The code fragment above specifies a regex that matches a comma character immediately followed by a single-space character and produces the following output: <BR><BR>John Doe <BR>47 <BR>Hillsboro Road <BR>32000 <BR><BR>Note <BR><BR>String合并了三个方便的方法调用它们等价的Pattern方法： public boolean matches(String regex), public String [] split(String regex), and public String [] split(String regex, int limit)。 <BR><BR><BR>Matcher 方法 <BR><BR>Matcher对象支持不同类型的pattern匹配操作，例如扫描text查找下一个match；尝试根据一个pattern来匹配整个文本；根据一个pattern尝试匹配部分text。用以下的方法完成这些任务： <BR><BR>public boolean find(): 扫描text查找下一个match。此方法，或者在text的开始扫描，假如上一次的方法调用返回true且这个matcher没有被reset，在前一个match后的第一个字符开始扫描。假如一个match被找到的话返回布尔值true。Listing 1 展示了一个例子。 <BR>public boolean find(int start): 重新安排matcher扫描下一个match。扫描从start指定的index开始。假如一个match被找到的话返回布尔值true。例如：m.find (1); 从index1开始扫描。(索引0被忽略。)假如start包含了一个负数或者一个超出了matcfher的text长度的值，这个方法抛出IndexOutOfBoundsException异常。 <BR>public boolean matches(): 尝试根据pattern匹配整个text。在这个text匹配的情形下返回true。例如： Pattern p = Pattern.compile ("\\w*"); Matcher m = p.matcher ("abc!"); System.out.println (m.matches ()); 输出false因为整个abc! text 包含了非字母word characters。 <BR>public boolean lookingAt(): 尝试根据pattern匹配text。假如一个match被找到的话返回布尔值true。 不像 matches()， 整个text不需要被匹配。例如：Pattern p = Pattern.compile ("\\w*"); Matcher m = p.matcher ("abc!"); System.out.println (p.lookingAt ()); 输出true因为text abc!的开始部分仅包含word 字符。 <BR>不像Pattern对象，Matcher 包含了状态信息。有时，你在一个pattern 匹配后想reset一个matcher清除那些信息。下边的方法reset了一个matcher： <BR><BR>public Matcher reset(): 重置了一个matcher的状态，包括matcher的append position(被清除为0)。下一个pattern的匹配操作从matcher新文本的起始开始。返回当前的matcher对象引用。例如：m.reset (); 通过引用m重置了matcher。 <BR>public Matcher reset(CharSequence text): 重新设置一个matcher的状态且设置了matcher的文本内同。下一个pattern的匹配操作在matcher新的文本的起始位置开始。返回当前的matcher对象引用。例如：m.reset ("new text"); 重置m引用的对象并制定 新的text作为matcher的新text。 <BR>一个matcher的append position 决定了matcher的text的追加到一个StringBuffer对象中的开始位置。以下的方法使用了append position： <BR><BR>public Matcher appendReplacement(StringBuffer sb, String replacement): 读取matcher的text并将它们追加到sb引用的StringBuffer对象。这个方法在前一个pattern match的最后一个字符之后停止读取。此method 然后添加replacement引用的中的characters 到StringBuffer 对象。(替换字符串可以包含上一个匹配捕获的文本的引用，通过dollar-sign characters ($) 和 capturing group 数) 最终，这个方法设置了matcher的append position为最后一个匹配字符的位置加上1。一个当前的matcher对象的引用返回。假如这个matcher对象还没有执行match或者上次的match尝试失败此方法将抛出一个IllegalStateException 异常。假如replacement指定了一个pattern中不存在的capturing group 一个IndexOutOfBoundsException异常将被抛出。 <BR>public StringBuffer appendTail(StringBuffer sb): 追加所有的text 到StringBuffer对象并返回对象引用。在最后一次调用appendReplacement(StringBuffer sb, String replacement) 方法之后，调用appendTail(StringBuffer sb) copy剩余的text到StringBuffer对象。 <BR>随后的例子调用appendReplacement(StringBuffer sb, String replacement) 和 appendTail(StringBuffer sb) 方法来替换所有在one cat, two cats, or three cats on a fence 中出现的cat为caterpillar。一个capturing group 和 在replacement中的capturing group的引用允许在每一个cat匹配后插入erpillar： <BR><BR>Pattern p = Pattern.compile ("(cat)"); <BR>Matcher m = p.matcher ("one cat, two cats, or three cats on a fence"); <BR>StringBuffer sb = new StringBuffer (); <BR><BR>while (m.find ()) <BR>m.appendReplacement (sb, "$1erpillar"); <BR><BR>m.appendTail (sb); <BR>System.out.println (sb); <BR><BR>此示例产生如下输出： <BR><BR>one caterpillar, two caterpillars, or three caterpillars on a fence <BR><BR>其它的两个替换方法使用替换的文本替换第一个match和所有的match成为可能： <BR><BR>public String replaceFirst(String replacement): 重置matcher，创建一个新的String对象，拷贝所有匹配的文本字符（直到第一个match）到String，追加替换字符到String，拷贝剩余的字符到Strring，并返回对象引用。（替换字符串可以包含上一个匹配捕获的文本的引用，通过dollar-sign characters ($) 和 capturing group 数。） <BR>public String replaceAll(String replacement): 操作和上一个方法类似。然而，replaceAll(String replacement) 用替换字符替换所有匹配。 <BR>正则表达式\s+ 探测在文本中出现的一次或多次出现的空格。随后的例子使用了这个regex 并调用了replaceAll(String replacement) 方法来从text删除duplicate whitespace ： <BR><BR>Pattern p = Pattern.compile ("\\s+"); <BR>Matcher m = p.matcher ("Remove the \t\t duplicate whitespace. "); <BR>System.out.println (m.replaceAll (" ")); <BR><BR>此示例产生如下输出： <BR><BR>Remove the duplicate whitespace. <BR><BR>Listing 1 包含了System.out.println ("Found " + m.group ());. 注意方法调用group()。此方法是capturing group-oriented 的Matcher方法： <BR><BR>public int groupCount(): 返回在matcher的pattern中capturing groups 的个数。这个计数没有包含特定的capturing group 数字 0，它捕获前一个match（不管一个pattern包含capturing groups与否。） <BR>public String group(): 通过capturing group 数字 0记录返回上一个match的字符。此方法可以根据一个空的字符串返回一个空字符串。假如match还没有被尝试或者上次的match操作失败将抛出IllegalStateException异常。 <BR>public String group(int group): 像上一个方法，除了通过group指定的capturing group number返回以前的match字符外。假如没有group number 指定的capturing group在pattern中存在，此方法抛出 一个IndexOutOfBoundsException 异常。 <BR>以下代码示范了the capturing group 方法： <BR><BR>Pattern p = Pattern.compile ("(.(.(.)))"); <BR>Matcher m = p.matcher ("abc"); <BR>m.find (); <BR><BR>System.out.println (m.groupCount ()); <BR>for (int i = 0; i &lt;= m.groupCount (); i++) <BR>System.out.println (i + ": " + m.group (i)); <BR><BR>The example produces the following output: <BR><BR>3 <BR>0: abc <BR>1: abc <BR>2: bc <BR>3: c <BR><BR>Capturing group 数字0 保存了previous match 且与has nothing to do with whether 一个capturing group在一个pattern中出现与否没有任何关系。也就是 is (.(.(.)))。其它的三个capturing groups捕获了previous match属于这个capturing groups的字符。例如，number 2, (.(.)), 捕获 bc; and number 3, (.), 捕获 c. <BR><BR>在我们离开讨论Matcher的方法之前，我们将examine四个match位置方法： <BR><BR>public int start(): 返回previous match的开始位置。假如match还没有被执行或者上次的match失败，此方法抛出一个IllegalStateException异常。 <BR>public int start(int group): 类似上一个方法，除了返回group指定的capturing group 的相关的previous match 的开始索引外，假如在pattern中没有指定的capturing group number 存在，start(int group) 抛出IndexOutOfBoundsException 异常。 <BR>public int end(): 返回上次match中匹配的字符的索引位置加上1。假如match还没有被尝试或者上次的match操作失败将抛出IllegalStateException异常。 <BR>public int end(int group): 类似上一个方法，除了返回group指定的capturing group 的相关的previous match 的end索引外。假如在pattern中没有指定的capturing group number 存在，end(int group) 抛出IndexOutOfBoundsException 异常。 <BR>下边的示例示范了两个match position 方法，为capturing group number 2报告起始/结束match 位置： <BR><BR>Pattern p = Pattern.compile ("(.(.(.)))"); <BR>Matcher m = p.matcher ("abcabcabc"); <BR><BR>while (m.find ()) <BR>{ <BR>System.out.println ("Found " + m.group (2)); <BR>System.out.println (" starting at index " + m.start (2) + <BR>" and ending at index " + m.end (2)); <BR>System.out.println (); <BR>} <BR><BR>The example produces the following output: <BR><BR>Found bc <BR>starting at index 1 and ending at index 3 <BR><BR>Found bc <BR>starting at index 4 and ending at index 6 <BR><BR>Found bc <BR>starting at index 7 and ending at index 9 <BR><BR>输出show我们仅仅对与capturing group number 2相关的matcher感兴趣，也就是这些匹配的起始结束位置。 <BR><BR>Note <BR><BR>String 引入了两个方便的和调用Matcher等价的方法：public String replaceFirst(String regex, String replacement) 和 public String replaceAll(String regex, String replacement)。 <BR><BR><BR>PatternSyntaxException methods <BR><BR>Pattern的方法当它们发现非法的正则表达式语法错误的时候抛出PatternSyntaxException 异常。一个异常处理器可以调用PatternSyntaxException 的方法来获得抛出的关于语法错误的PatternSyntaxException 对象的信息。 <BR><BR>public String getDescription(): 返回语法错误描述。 <BR>public int getIndex(): 返回语法错误发生位置的近似索引或-1，假如index是未知的。 <BR>public String getMessage(): 建立一个多行的，包含了其它三个方法返回的信息的综合，以可视的方式指出在pattern中错误的位置字符串。 <BR>public String getPattern(): 返回不正确的正则表达式。 <BR>因为PatternSyntaxException 从java.lang.RuntimeException继承而来，代码不需要指定错误handler。This proves appropriate when regexes are known to have correct patterns。但当有潜在的pattern语法错误存在的时候，一个异常handler是必需的。因而，RegexDemo的源代码(参看 Listing 1) 包含了try { ... } catch (ParseSyntaxException e) { ... }，它们调用了PatternSyntaxException四个异常方法中的每一个来获得非法pattern的信息。 <BR><BR>什么组成了非法的pattern?在embedded flag expression 中没有指定结束的元字符结束符号就是一个例。假如你执行java RegexDemo (?itree Treehouse。此命令的非法正则表达式(?tree pattern 导致 p = Pattern.compile (args [0]); 抛出PatternSyntaxException 异常。你将看到如下输出： <BR><BR>Regex syntax error: Unknown inline modifier near index 3 <BR>(?itree <BR>^ <BR>Error description: Unknown inline modifier <BR>Error index: 3 <BR>Erroneous pattern: (?itree <BR><BR>Note <BR><BR>public PatternSyntaxException(String desc, String regex, int index) 构造函数让你创建你自己的PatternSyntaxException对象， That constructor comes in handy should you ever create your own preprocessing compilation method that recognizes your own pattern syntax, translates that syntax to syntax recognized by Pattern''s compilation methods, and calls one of those compilation methods. If your method''s caller violates your custom pattern syntax, you can throw an appropriate PatternSyntaxException object from that method。 <BR><BR><BR>一个正则表达式应用实践 <BR><BR>Regexes let you create powerful text-processing applications. One application you might find helpful extracts comments from a Java, C, or C++ source file, and records those comments in another file. Listing 2 presents that application''s source code: <BR><BR>Listing 2. ExtCmnt.java <BR><BR>// ExtCmnt.java <BR><BR>import java.io.*; <BR>import java.util.regex.*; <BR><BR>class ExtCmnt <BR>{ <BR>public static void main (String [] args) <BR>{ <BR>if (args.length != 2) <BR>{ <BR>System.err.println ("usage: java ExtCmnt infile outfile"); <BR>return; <BR>} <BR><BR>Pattern p; <BR>try <BR>{ <BR>// The following pattern lets this extract multiline comments that <BR>// appear on a single line (e.g., /* same line */) and single-line <BR>// comments (e.g., // some line). Furthermore, the comment may <BR>// appear anywhere on the line. <BR><BR>p = Pattern.compile (".*/\\*.*\\*/|.*//.*$"); <BR>} <BR>catch (PatternSyntaxException e) <BR>{ <BR>System.err.println ("Regex syntax error: " + e.getMessage ()); <BR>System.err.println ("Error description: " + e.getDescription ()); <BR>System.err.println ("Error index: " + e.getIndex ()); <BR>System.err.println ("Erroneous pattern: " + e.getPattern ()); <BR>return; <BR>} <BR><BR>BufferedReader br = null; <BR>BufferedWriter bw = null; <BR><BR>try <BR>{ <BR>FileReader fr = new FileReader (args [0]); <BR>br = new BufferedReader (fr); <BR><BR>FileWriter fw = new FileWriter (args [1]); <BR>bw = new BufferedWriter (fw); <BR><BR>Matcher m = p.matcher (""); <BR>String line; <BR>while ((line = br.readLine ()) != null) <BR>{ <BR>m.reset (line); <BR>if (m.matches ()) /* entire line must match */ <BR>{ <BR>bw.write (line); <BR>bw.newLine (); <BR>} <BR>} <BR>} <BR>catch (IOException e) <BR>{ <BR>System.err.println (e.getMessage ()); <BR>return; <BR>} <BR>finally // Close file. <BR>{ <BR>try <BR>{ <BR>if (br != null) <BR>br.close (); <BR><BR>if (bw != null) <BR>bw.close (); <BR>} <BR>catch (IOException e) <BR>{ <BR>} <BR>} <BR>} <BR>} <BR><BR>在创建Pattern 和Matcher 对象之后，ExtCmnt 逐行的读取一个文本文件的内容。对于每一行，matcher尝试匹配pattern的行，鉴别是一个单行的注释或者多行的注释在一行中出现。假如一行匹配pattern，ExtCmnt 将此行写入另外一个文本文件中。例如，java ExtCmnt ExtCmnt.java out 读取ExtCmnt.java 文件的每一行，根据pattern来尝试着一行，将匹配的行输出到名叫out的文件。 (不要担心理解文件的读写逻辑。我将在将来的文章中explore这些代码。) 在ExtCmnt执行完成，out 文件包含了以下行： <BR><BR>// ExtCmnt.java <BR>// The following pattern lets this extract multiline comments that <BR>// appear on a single line (e.g., /* same line */) and single-line <BR>// comments (e.g., // some line). Furthermore, the comment may <BR>// appear anywhere on the line. <BR>p = Pattern.compile (".*/\\*.*\\*/|.*//.*$"); <BR>if (m.matches ()) /* entire line must match */ <BR>finally // Close file. <BR><BR>这个输出显示ExtCmnt 并不完美：p = Pattern.compile (".*/\\*.*\\*/|.*//.*$"); 没有描绘一个注释。出现在out中的行因为ExtCmnt的matcher匹配了//字符。 <BR><BR>关于pattern ".*/\\*.*\\*/|.*//.*$"由一些有趣的事，竖线元字符metacharacter (|)。依照SDK documentation，圆括号元字符在capturing group和 竖线元字符是逻辑操作符号。vertical bar 描述了一个matcher，它使用操作符左侧的正则表达式结构来在matcher的文本中定为一个match。假如没有match存在，matcher使用操作符号右侧的正则表达式进行再次的匹配尝试。 <BR><BR>温习 <BR><BR>尽管正则表达式简化了在text处理程序中pattern匹配的代码，除非你理解它们，否则你不能有效的在你的程序中使用正则表达式。这篇文章通过介绍给你regex terminology，the java.util.regex 包和示范regex constructs的程序来让你对正则表达式有一个基本的理解。既然你对regexes有了一个基本的理解，建立在通过阅读additional articles (see Resources)和学习java.util.regex''s SDK 文档，那里你可以学习更多的regex constructs ，例如POSIX (Portable Operating System Interface for Unix) 字符类。 <BR><BR>我鼓励你用这篇文章中或者其它以前文章中资料中问题email me。(请保持问题和这个栏目讨论的文章相关性。)你的问题和我的回答将出现在相关的学习guides中。) <BR><BR>After writing Java 101 articles for 28 consecutive months, I''m taking a two-month break. I''ll return in May and introduce a series on data structures and algorithms. <BR>About the author <BR><BR>Jeff Friesen has been involved with computers for the past 23 years. He holds a degree in computer science and has worked with many computer languages. Jeff has also taught introductory Java programming at the college level. In addition to writing for JavaWorld, he has written his own Java book for beginners—Java 2 by Example, Second Edition (Que Publishing, 2001; ISBN: 0789725932)—and helped write Using Java 2 Platform, Special Edition (Que Publishing, 2001; ISBN: 0789724685). Jeff goes by the nickname Java Jeff (or JavaJeff). To see what he''s working on, check out his Website at http://www.javajeff.com. <BR><BR>Resources <BR><BR>Download this article''s source code and resource files: <BR>http://www.javaworld.com/javaworld/jw-02-2003/java101/jw-0207-java101.zip <BR>For a glossary specific to this article, homework, and more, see the Java 101 study guide that accompanies this article: <BR>http://www.javaworld.com/javaworld/jw-02-2003/jw-0207-java101guide.html <BR>"Magic with Merlin: Parse Sequences of Characters with the New regex Library," John Zukowski (IBM developerWorks, August 2002) explores java.util.regex''s support for pattern matching and presents a complete example that finds the longest word in a text file: <BR>http://www-106.ibm.com/developerworks/java/library/j-mer0827/ <BR>"Matchmaking with Regular Expressions," Benedict Chng (JavaWorld, July 2001) explores regexes in the context of Apache''s Jakarta ORO library: <BR>http://www.javaworld.com/javaworld/jw-07-2001/jw-0713-regex.html <BR>"Regular Expressions and the Java Programming Language," Dana Nourie and Mike McCloskey (Sun Microsystems, August 2002) presents a brief overview of java.util.regex, including five illustrative regex-based applications: <BR>http://developer.java.sun.com/developer/technicalArticles/releases/1.4regex/ <BR>In "The Java Platform" (onJava.com), an excerpt from Chapter 4 of O''Reilly''s Java in a Nutshell, 4th Edition, David Flanagan presents short examples of CharSequence and java.util.regex methods: <BR>http://www.onjava.com/pub/a/onjava/excerpt/javanut4_ch04 <BR>The Java Tutorial''s "Regular Expressions" lesson teaches the basics of Sun''s java.util.regex package: <BR>http://java.sun.com/docs/books/tutorial/extra/regex/index.html <BR>Wikipedia defines some regex terminology, presents a brief history of regexes, and explores various regex syntaxes: <BR>http://www.wikipedia.org/wiki/Regular_expression <BR>Read Jeff''s previous Java 101 column: "Tools of the Trade, Part 3" (JavaWorld, January 2003): <BR>http://www.javaworld.com/javaworld/jw-01-2003/jw-0103-java101.html? <BR>Check out past Java 101 articles: <BR>http://www.javaworld.com/javaworld/topicalindex/jw-ti-java101.html <BR>Browse the Core Java section of JavaWorld''s Topical Index: <BR>http://www.javaworld.com/channel_content/jw-core-index.shtml <BR>Need some Java help? Visit our Java Beginner discussion: <BR>http://forums.devworld.com/webx?50@@.ee6b804 <BR>Java experts answer your toughest Java questions in JavaWorld''s Java Q&amp;A column: <BR>http://www.javaworld.com/javaworld/javaqa/javaqa-index.html <BR>For Tips ''N Tricks, see: <BR>http://www.javaworld.com/javaworld/javatips/jw-javatips.index.html <BR>Sign up for JavaWorld''s free weekly Core Java email newsletter: <BR>http://www.javaworld.com/subscribe <BR>You''ll find a wealth of IT-related articles from our sister publications at IDG.net <BR></TD></TR></TBODY></TABLE><img src ="http://www.cnitblog.com/reene/aggbug/2127.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/reene/" target="_blank">快乐着飞舞着</a> 2005-08-16 10:22 <a href="http://www.cnitblog.com/reene/archive/2005/08/16/2127.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>skin L&amp;F</title><link>http://www.cnitblog.com/reene/archive/2005/07/03/703.html</link><dc:creator>快乐着飞舞着</dc:creator><author>快乐着飞舞着</author><pubDate>Sun, 03 Jul 2005 10:49:00 GMT</pubDate><guid>http://www.cnitblog.com/reene/archive/2005/07/03/703.html</guid><wfw:comment>http://www.cnitblog.com/reene/comments/703.html</wfw:comment><comments>http://www.cnitblog.com/reene/archive/2005/07/03/703.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/reene/comments/commentRss/703.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/reene/services/trackbacks/703.html</trackback:ping><description><![CDATA[<A href="http://dev.l2fprod.com/tutorial-firstaid.html">http://dev.l2fprod.com/tutorial-firstaid.html</A><img src ="http://www.cnitblog.com/reene/aggbug/703.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/reene/" target="_blank">快乐着飞舞着</a> 2005-07-03 18:49 <a href="http://www.cnitblog.com/reene/archive/2005/07/03/703.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>一个关于 CMP entity bean 的诡异问题</title><link>http://www.cnitblog.com/reene/archive/2005/07/01/672.html</link><dc:creator>快乐着飞舞着</dc:creator><author>快乐着飞舞着</author><pubDate>Fri, 01 Jul 2005 10:38:00 GMT</pubDate><guid>http://www.cnitblog.com/reene/archive/2005/07/01/672.html</guid><wfw:comment>http://www.cnitblog.com/reene/comments/672.html</wfw:comment><comments>http://www.cnitblog.com/reene/archive/2005/07/01/672.html#Feedback</comments><slash:comments>3</slash:comments><wfw:commentRss>http://www.cnitblog.com/reene/comments/commentRss/672.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/reene/services/trackbacks/672.html</trackback:ping><description><![CDATA[<font face="Courier New" size=2>
<DIV style="BORDER-RIGHT: windowtext 0.5pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 0.5pt solid; PADDING-LEFT: 5.4pt; BACKGROUND: #e6e6e6; PADDING-BOTTOM: 4px; BORDER-LEFT: windowtext 0.5pt solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: windowtext 0.5pt solid">
<DIV><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #0000ff">create</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">table</SPAN><SPAN style="COLOR: #000000">&nbsp;results(sno&nbsp;</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #000000">number</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #800000">8</SPAN><SPAN style="COLOR: #000000">)&nbsp;</SPAN><SPAN style="COLOR: #0000ff">references</SPAN><SPAN style="COLOR: #000000">&nbsp;students&nbsp;</SPAN><SPAN style="COLOR: #0000ff">on</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">delete</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">cascade</SPAN><SPAN style="COLOR: #000000">,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cno&nbsp;</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #000000">number</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="FONT-WEIGHT: bold; COLOR: #800000">5</SPAN><SPAN style="COLOR: #000000">)&nbsp;</SPAN><SPAN style="COLOR: #0000ff">references</SPAN><SPAN style="COLOR: #000000">&nbsp;courses&nbsp;</SPAN><SPAN style="COLOR: #0000ff">on</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">delete</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">cascade</SPAN><SPAN style="COLOR: #000000">,<BR></SPAN><SPAN style="COLOR: #0000ff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;primary</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">key</SPAN><SPAN style="COLOR: #000000">(sno,cno));<BR><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top></SPAN></DIV></DIV>
<P>&nbsp;</P>
<P><FONT color=#000000>建立一个CMP Entity Bean<BR>在这个bean 中 有如下的方法：</P>
<P>&nbsp;</P>
<DIV style="BORDER-RIGHT: windowtext 0.5pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 0.5pt solid; PADDING-LEFT: 5.4pt; BACKGROUND: #e6e6e6; PADDING-BOTTOM: 4px; BORDER-LEFT: windowtext 0.5pt solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: windowtext 0.5pt solid">
<DIV><SPAN style="COLOR: #008080">&nbsp;1</SPAN><IMG id=Codehighlighter1_2_223_Open_Image onclick="this.style.display='none'; Codehighlighter1_2_223_Open_Text.style.display='none'; Codehighlighter1_2_223_Closed_Image.style.display='inline'; Codehighlighter1_2_223_Closed_Text.style.display='inline';" src="http://www.cnitblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_2_223_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_2_223_Closed_Text.style.display='none'; Codehighlighter1_2_223_Open_Image.style.display='inline'; Codehighlighter1_2_223_Open_Text.style.display='inline';" src="http://www.cnitblog.com/images/OutliningIndicators/ContractedBlock.gif" align=top><SPAN style="COLOR: #000000">&nbsp;&nbsp;</SPAN><SPAN id=Codehighlighter1_2_223_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">/**/</SPAN><SPAN id=Codehighlighter1_2_223_Open_Text><SPAN style="COLOR: #008000">/*</SPAN><SPAN style="COLOR: #008000">*<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;2</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;*<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;3</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;*&nbsp;&lt;!--&nbsp;begin-user-doc&nbsp;--&gt;<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;4</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;*&nbsp;The&nbsp;&nbsp;ejbCreate&nbsp;method.<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;5</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;*&nbsp;&lt;!--&nbsp;end-user-doc&nbsp;--&gt;<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;6</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;*<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;7</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;*&nbsp;&lt;!--&nbsp;begin-xdoclet-definition&nbsp;--&gt;&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;8</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;*&nbsp;@ejb.create-method&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;9</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;*&nbsp;&lt;!--&nbsp;end-xdoclet-definition&nbsp;--&gt;&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">10</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;*&nbsp;@generated<BR></SPAN><SPAN style="COLOR: #008080">11</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">*/</SPAN></SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">12</SPAN><SPAN style="COLOR: #000000"><IMG id=Codehighlighter1_311_596_Open_Image onclick="this.style.display='none'; Codehighlighter1_311_596_Open_Text.style.display='none'; Codehighlighter1_311_596_Closed_Image.style.display='inline'; Codehighlighter1_311_596_Closed_Text.style.display='inline';" src="http://www.cnitblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_311_596_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_311_596_Closed_Text.style.display='none'; Codehighlighter1_311_596_Open_Image.style.display='inline'; Codehighlighter1_311_596_Open_Text.style.display='inline';" src="http://www.cnitblog.com/images/OutliningIndicators/ContractedBlock.gif" align=top>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;resultPK&nbsp;ejbCreate(Integer&nbsp;sno,Integer&nbsp;cno)&nbsp;throws&nbsp;javax.ejb.CreateException&nbsp;</SPAN><SPAN id=Codehighlighter1_311_596_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.cnitblog.com/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_311_596_Open_Text><SPAN style="COLOR: #000000">{<BR></SPAN><SPAN style="COLOR: #008080">13</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;EJB&nbsp;2.0&nbsp;spec&nbsp;says&nbsp;return&nbsp;null&nbsp;for&nbsp;CMP&nbsp;ejbCreate&nbsp;methods.<BR></SPAN><SPAN style="COLOR: #008080">14</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;TODO:&nbsp;YOU&nbsp;MUST&nbsp;INITIALIZE&nbsp;THE&nbsp;FIELDS&nbsp;FOR&nbsp;THE&nbsp;BEAN&nbsp;HERE.&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">15</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;setMyField("Something");&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">16</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;begin-user-code</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #008080">17</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#008000>//&nbsp;</FONT></SPAN><SPAN style="COLOR: #0000ff"><FONT color=#008000>this</FONT></SPAN><SPAN style="COLOR: #000000"><FONT color=#008000>.setCno(cno);<BR></FONT></SPAN><SPAN style="COLOR: #008080">18</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#008000>//&nbsp;</FONT></SPAN><SPAN style="COLOR: #0000ff"><FONT color=#008000>this</FONT></SPAN><SPAN style="COLOR: #000000"><FONT color=#008000>.setSno(sno);</FONT><BR></SPAN><SPAN style="COLOR: #008080">19</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;resultPK(sno,cno);<BR></SPAN><SPAN style="COLOR: #008080">20</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;end-user-code</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #008080">21</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">22</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top><BR></SPAN><SPAN style="COLOR: #008080">23</SPAN><SPAN style="COLOR: #000000"><IMG id=Codehighlighter1_601_764_Open_Image onclick="this.style.display='none'; Codehighlighter1_601_764_Open_Text.style.display='none'; Codehighlighter1_601_764_Closed_Image.style.display='inline'; Codehighlighter1_601_764_Closed_Text.style.display='inline';" src="http://www.cnitblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_601_764_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_601_764_Closed_Text.style.display='none'; Codehighlighter1_601_764_Open_Image.style.display='inline'; Codehighlighter1_601_764_Open_Text.style.display='inline';" src="http://www.cnitblog.com/images/OutliningIndicators/ContractedBlock.gif" align=top>&nbsp;&nbsp;</SPAN><SPAN id=Codehighlighter1_601_764_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">/**/</SPAN><SPAN id=Codehighlighter1_601_764_Open_Text><SPAN style="COLOR: #008000">/*</SPAN><SPAN style="COLOR: #008000">*<BR></SPAN><SPAN style="COLOR: #008080">24</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;*&nbsp;&lt;!--&nbsp;begin-user-doc&nbsp;--&gt;<BR></SPAN><SPAN style="COLOR: #008080">25</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;*&nbsp;The&nbsp;container&nbsp;invokes&nbsp;this&nbsp;method&nbsp;immediately&nbsp;after&nbsp;it&nbsp;calls&nbsp;ejbCreate.<BR></SPAN><SPAN style="COLOR: #008080">26</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;*&nbsp;&lt;!--&nbsp;end-user-doc&nbsp;--&gt;<BR></SPAN><SPAN style="COLOR: #008080">27</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;*&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">28</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;*&nbsp;@generated<BR></SPAN><SPAN style="COLOR: #008080">29</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">*/</SPAN></SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">30</SPAN><SPAN style="COLOR: #000000"><IMG id=Codehighlighter1_852_1607_Open_Image onclick="this.style.display='none'; Codehighlighter1_852_1607_Open_Text.style.display='none'; Codehighlighter1_852_1607_Closed_Image.style.display='inline'; Codehighlighter1_852_1607_Closed_Text.style.display='inline';" src="http://www.cnitblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_852_1607_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_852_1607_Closed_Text.style.display='none'; Codehighlighter1_852_1607_Open_Image.style.display='inline'; Codehighlighter1_852_1607_Open_Text.style.display='inline';" src="http://www.cnitblog.com/images/OutliningIndicators/ContractedBlock.gif" align=top>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;ejbPostCreate(Integer&nbsp;sno,Integer&nbsp;cno)&nbsp;throws&nbsp;javax.ejb.CreateException&nbsp;</SPAN><SPAN id=Codehighlighter1_852_1607_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.cnitblog.com/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_852_1607_Open_Text><SPAN style="COLOR: #000000">{<BR></SPAN><SPAN style="COLOR: #008080">31</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;begin-user-code</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #008080">32</SPAN><SPAN style="COLOR: #008000"><IMG id=Codehighlighter1_883_1494_Open_Image onclick="this.style.display='none'; Codehighlighter1_883_1494_Open_Text.style.display='none'; Codehighlighter1_883_1494_Closed_Image.style.display='inline'; Codehighlighter1_883_1494_Closed_Text.style.display='inline';" src="http://www.cnitblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_883_1494_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_883_1494_Closed_Text.style.display='none'; Codehighlighter1_883_1494_Open_Image.style.display='inline'; Codehighlighter1_883_1494_Open_Text.style.display='inline';" src="http://www.cnitblog.com/images/OutliningIndicators/ContractedSubBlock.gif" align=top></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">try</SPAN><SPAN id=Codehighlighter1_883_1494_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.cnitblog.com/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_883_1494_Open_Text><SPAN style="COLOR: #000000">{<BR></SPAN><SPAN style="COLOR: #008080">33</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Hashtable&nbsp;props&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;Hashtable();<BR></SPAN><SPAN style="COLOR: #008080">34</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top><BR></SPAN><SPAN style="COLOR: #008080">35</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;props.put(InitialContext.INITIAL_CONTEXT_FACTORY,<BR></SPAN><SPAN style="COLOR: #008080">36</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">weblogic.jndi.WLInitialContextFactory</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR></SPAN><SPAN style="COLOR: #008080">37</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;props.put(InitialContext.PROVIDER_URL,&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">t3://127.0.0.1:7001</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR></SPAN><SPAN style="COLOR: #008080">38</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top><BR></SPAN><SPAN style="COLOR: #008080">39</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;InitialContext&nbsp;initialContext&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;InitialContext(props);<BR></SPAN><SPAN style="COLOR: #008080">40</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;csc.studentLocalHome&nbsp;studentlocalhome</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">(studentLocalHome)initialContext.lookup(csc.studentLocalHome.JNDI_NAME);<BR></SPAN><SPAN style="COLOR: #008080">41</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">this</SPAN><SPAN style="COLOR: #000000">.setStudent(studentlocalhome.findByPrimaryKey(sno));<BR></SPAN><SPAN style="COLOR: #008080">42</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;csc.courseLocalHome&nbsp;courselocalhome</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">(courseLocalHome)initialContext.lookup(csc.courseLocalHome.JNDI_NAME);<BR></SPAN><SPAN style="COLOR: #008080">43</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">this</SPAN><SPAN style="COLOR: #000000">.setCourse(courselocalhome.findByPrimaryKey(cno));<BR></SPAN><SPAN style="COLOR: #008080">44</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top><BR></SPAN><SPAN style="COLOR: #008080">45</SPAN><SPAN style="COLOR: #000000"><IMG id=Codehighlighter1_1513_1545_Open_Image onclick="this.style.display='none'; Codehighlighter1_1513_1545_Open_Text.style.display='none'; Codehighlighter1_1513_1545_Closed_Image.style.display='inline'; Codehighlighter1_1513_1545_Closed_Text.style.display='inline';" src="http://www.cnitblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_1513_1545_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_1513_1545_Closed_Text.style.display='none'; Codehighlighter1_1513_1545_Open_Image.style.display='inline'; Codehighlighter1_1513_1545_Open_Text.style.display='inline';" src="http://www.cnitblog.com/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #0000ff">catch</SPAN><SPAN style="COLOR: #000000">(Exception&nbsp;e)</SPAN><SPAN id=Codehighlighter1_1513_1545_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.cnitblog.com/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_1513_1545_Open_Text><SPAN style="COLOR: #000000">{<BR></SPAN><SPAN style="COLOR: #008080">46</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.printStackTrace();<BR></SPAN><SPAN style="COLOR: #008080">47</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000">System.</SPAN><SPAN style="COLOR: #0000ff">out</SPAN><SPAN style="COLOR: #000000">.println(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">ejb&nbsp;postCreate</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR></SPAN><SPAN style="COLOR: #008080">48</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;end-user-code</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #008080">49</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">50</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top></SPAN></DIV></DIV>
<P>&nbsp;</P>
<P></FONT>&nbsp;</P>
<P>&nbsp;</P>
<DIV style="BORDER-RIGHT: windowtext 0.5pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 0.5pt solid; PADDING-LEFT: 5.4pt; BACKGROUND: #e6e6e6; PADDING-BOTTOM: 4px; BORDER-LEFT: windowtext 0.5pt solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: windowtext 0.5pt solid">
<DIV><SPAN style="COLOR: #008080">&nbsp;1</SPAN><IMG id=Codehighlighter1_2_293_Open_Image onclick="this.style.display='none'; Codehighlighter1_2_293_Open_Text.style.display='none'; Codehighlighter1_2_293_Closed_Image.style.display='inline'; Codehighlighter1_2_293_Closed_Text.style.display='inline';" src="http://www.cnitblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_2_293_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_2_293_Closed_Text.style.display='none'; Codehighlighter1_2_293_Open_Image.style.display='inline'; Codehighlighter1_2_293_Open_Text.style.display='inline';" src="http://www.cnitblog.com/images/OutliningIndicators/ContractedBlock.gif" align=top><SPAN style="COLOR: #000000">&nbsp;&nbsp;</SPAN><SPAN id=Codehighlighter1_2_293_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">/**/</SPAN><SPAN id=Codehighlighter1_2_293_Open_Text><SPAN style="COLOR: #008000">/*</SPAN><SPAN style="COLOR: #008000">*<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;2</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;*&nbsp;@ejb.relation&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;3</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;*&nbsp;name&nbsp;=&nbsp;"resultTOstudent"<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;4</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;*&nbsp;role-name&nbsp;=&nbsp;"result_student"<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;5</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;*&nbsp;target-ejb&nbsp;=&nbsp;"student"<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;6</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;*&nbsp;target-multiple&nbsp;=&nbsp;"yes"<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;7</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;*&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;8</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;*&nbsp;@weblogic.column-map&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;9</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;*&nbsp;foreign-key-column&nbsp;=&nbsp;"sno"<BR></SPAN><SPAN style="COLOR: #008080">10</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;*&nbsp;key-column&nbsp;=&nbsp;"sno"<BR></SPAN><SPAN style="COLOR: #008080">11</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;*&nbsp;@ejb.interface-method&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">12</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;*&nbsp;view-type&nbsp;=&nbsp;"local"<BR></SPAN><SPAN style="COLOR: #008080">13</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">*/</SPAN></SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">14</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top>&nbsp;</SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">abstract</SPAN><SPAN style="COLOR: #000000">&nbsp;studentLocal&nbsp;getStudent();<BR></SPAN><SPAN style="COLOR: #008080">15</SPAN><SPAN style="COLOR: #000000"><IMG id=Codehighlighter1_340_616_Open_Image onclick="this.style.display='none'; Codehighlighter1_340_616_Open_Text.style.display='none'; Codehighlighter1_340_616_Closed_Image.style.display='inline'; Codehighlighter1_340_616_Closed_Text.style.display='inline';" src="http://www.cnitblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_340_616_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_340_616_Closed_Text.style.display='none'; Codehighlighter1_340_616_Open_Image.style.display='inline'; Codehighlighter1_340_616_Open_Text.style.display='inline';" src="http://www.cnitblog.com/images/OutliningIndicators/ContractedBlock.gif" align=top>&nbsp;</SPAN><SPAN id=Codehighlighter1_340_616_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">/**/</SPAN><SPAN id=Codehighlighter1_340_616_Open_Text><SPAN style="COLOR: #008000">/*</SPAN><SPAN style="COLOR: #008000">*<BR></SPAN><SPAN style="COLOR: #008080">16</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;*&nbsp;@ejb.relation&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">17</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;*&nbsp;name&nbsp;=&nbsp;"resultTOCourse"<BR></SPAN><SPAN style="COLOR: #008080">18</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;*&nbsp;role-name&nbsp;=&nbsp;"result_course"<BR></SPAN><SPAN style="COLOR: #008080">19</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;*&nbsp;target-ejb&nbsp;=&nbsp;"course"<BR></SPAN><SPAN style="COLOR: #008080">20</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;*&nbsp;target-multiple&nbsp;=&nbsp;"yes"<BR></SPAN><SPAN style="COLOR: #008080">21</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;*&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">22</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;*&nbsp;@weblogic.column-map&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">23</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;*&nbsp;foreign-key-column&nbsp;=&nbsp;"cno"<BR></SPAN><SPAN style="COLOR: #008080">24</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;*&nbsp;key-column&nbsp;=&nbsp;"cno"<BR></SPAN><SPAN style="COLOR: #008080">25</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;*&nbsp;@ejb.interface-method&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">26</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;*&nbsp;view-type&nbsp;=&nbsp;"local"<BR></SPAN><SPAN style="COLOR: #008080">27</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">*/</SPAN></SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">28</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top>&nbsp;</SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">abstract</SPAN><SPAN style="COLOR: #000000">&nbsp;courseLocal&nbsp;getCourse();</SPAN></DIV></DIV>
<P>&nbsp;</P>
<P><FONT color=#000000></FONT><FONT color=#000000>在客户程序中调用时 </FONT></P>
<P>&nbsp;</P>
<P>&nbsp;</P>
<DIV style="BORDER-RIGHT: windowtext 0.5pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 0.5pt solid; PADDING-LEFT: 5.4pt; BACKGROUND: #e6e6e6; PADDING-BOTTOM: 4px; BORDER-LEFT: windowtext 0.5pt solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: windowtext 0.5pt solid">
<DIV><IMG src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #000000">csc.result&nbsp;myBean&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;getHome().create(</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;Integer(Integer.parseInt(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">02012001</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">)),</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;Integer(</SPAN><SPAN style="COLOR: #000000">18</SPAN><SPAN style="COLOR: #000000">));</SPAN></DIV></DIV>
<P>&nbsp;</P>
<P>有如下的错误：<BR><BR>javax.ejb.CreateException: [EJB:010148]In EJB 'result', the primary key field 'cno' was not set during ejbCreate. All primary key fields must be initialized during ejbCreate.<BR><BR>如果把 ejbCreate改成<BR></P>
<P>&nbsp;</P>
<DIV style="BORDER-RIGHT: windowtext 0.5pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 0.5pt solid; PADDING-LEFT: 5.4pt; BACKGROUND: #e6e6e6; PADDING-BOTTOM: 4px; BORDER-LEFT: windowtext 0.5pt solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: windowtext 0.5pt solid">
<DIV><SPAN style="COLOR: #008080">&nbsp;1</SPAN><IMG id=Codehighlighter1_86_371_Open_Image onclick="this.style.display='none'; Codehighlighter1_86_371_Open_Text.style.display='none'; Codehighlighter1_86_371_Closed_Image.style.display='inline'; Codehighlighter1_86_371_Closed_Text.style.display='inline';" src="http://www.cnitblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_86_371_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_86_371_Closed_Text.style.display='none'; Codehighlighter1_86_371_Open_Image.style.display='inline'; Codehighlighter1_86_371_Open_Text.style.display='inline';" src="http://www.cnitblog.com/images/OutliningIndicators/ContractedBlock.gif" align=top><SPAN style="COLOR: #000000">&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;resultPK&nbsp;ejbCreate(Integer&nbsp;sno,Integer&nbsp;cno)&nbsp;throws&nbsp;javax.ejb.CreateException&nbsp;</SPAN><SPAN id=Codehighlighter1_86_371_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.cnitblog.com/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_86_371_Open_Text><SPAN style="COLOR: #000000">{<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;2</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;EJB&nbsp;2.0&nbsp;spec&nbsp;says&nbsp;return&nbsp;null&nbsp;for&nbsp;CMP&nbsp;ejbCreate&nbsp;methods.<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;3</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;TODO:&nbsp;YOU&nbsp;MUST&nbsp;INITIALIZE&nbsp;THE&nbsp;FIELDS&nbsp;FOR&nbsp;THE&nbsp;BEAN&nbsp;HERE.&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;4</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;setMyField("Something");&nbsp;<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;5</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;begin-user-code</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #008080">&nbsp;6</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">this</SPAN><SPAN style="COLOR: #000000">.setCno(cno);<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;7</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">this</SPAN><SPAN style="COLOR: #000000">.setSno(sno);<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;8</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;resultPK(sno,cno);<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;9</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;end-user-code</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #008080">10</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.cnitblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;}</SPAN></SPAN></DIV></DIV>
<P>&nbsp;</P>
<P>有下面的错误：<BR><BR>The setXXX method for a cmr-field that is mapped to a primary key may not be called. The cmr-field is read-only.<BR><BR><FONT color=#000000>特别的是 在第二种情况下&nbsp;create 方法虽然有错误 但是数据仍然插入了数据库，第一种情况下程序自然终止。<BR></FONT></FONT></P><img src ="http://www.cnitblog.com/reene/aggbug/672.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/reene/" target="_blank">快乐着飞舞着</a> 2005-07-01 18:38 <a href="http://www.cnitblog.com/reene/archive/2005/07/01/672.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>