??xml version="1.0" encoding="utf-8" standalone="yes"?>免费精品视频一区二区三区,亚洲黄色成人网,日韩—二三区免费观看avhttp://www.aygfsteel.com/Jkallen/category/6933.html开h?/description>zh-cnTue, 27 Feb 2007 09:49:48 GMTTue, 27 Feb 2007 09:49:48 GMT60prototype.js之ajax.request学习W记(一)http://www.aygfsteel.com/Jkallen/archive/2006/07/21/59445.htmlJkallenJkallenFri, 21 Jul 2006 08:47:00 GMThttp://www.aygfsteel.com/Jkallen/archive/2006/07/21/59445.htmlhttp://www.aygfsteel.com/Jkallen/comments/59445.htmlhttp://www.aygfsteel.com/Jkallen/archive/2006/07/21/59445.html#Feedback17http://www.aygfsteel.com/Jkallen/comments/commentRss/59445.htmlhttp://www.aygfsteel.com/Jkallen/services/trackbacks/59445.html阅读全文

Jkallen 2006-07-21 16:47 发表评论
]]>
DWR让AJAX如此??http://www.aygfsteel.com/Jkallen/archive/2006/07/11/57611.htmlJkallenJkallenTue, 11 Jul 2006 02:53:00 GMThttp://www.aygfsteel.com/Jkallen/archive/2006/07/11/57611.htmlhttp://www.aygfsteel.com/Jkallen/comments/57611.htmlhttp://www.aygfsteel.com/Jkallen/archive/2006/07/11/57611.html#Feedback0http://www.aygfsteel.com/Jkallen/comments/commentRss/57611.htmlhttp://www.aygfsteel.com/Jkallen/services/trackbacks/57611.html作?Cloves Carneiro; simmone
原文地址: http://www.javaworld.com/javaworld/jw-06-2005/jw-0620-dwr.html
中文地址: http://www.matrix.org.cn/resource/article/43/43926_DWR_AJAX.html
关键词: DWR AJAX


概述

q篇文章阐述了用开源项目DWR(直接Webq程控制)和AJAX(异步JavaScript和XML)的概忉|提高Web应用的可用性。作者一步步来展CDWR如何使得AJAX的应用既单又快捷。(1600?2005q??0日)

AJAXQ或者说是异步JavaScript和XMLQ描qC一U用؜合了HTML(或XHTML)和层叠样式表作ؓ表达信息,来创Z互式的Web应用的开发技?文档对象模型QDOMQ,JavaScript,动态地昄和与表达信息q行交互;q且QXMLHttpRequest对象与Web服务器异步地交换和处理数据?br />
因特|上许多例子展示了在一个HTML文g内部使用XMLHttpRequest与服务器端进行交互的必要的步骤。当手工地编写和l护XMLHttpRequest代码Ӟ开发者必d理许多潜在的问题Q特别是cM于跨览器的DOM实现的兼Ҏ这L问题。这会D在编码和调试Javascript代码上面pC清的旉Q这昄对开发者来说很不友好?br />
DWR(直接Webq程控制)目是在Apache许可下的一个开源的解决ҎQ它供给那些惌以一U简单的方式使用AJAX和XMLHttpRequest的开发者。它h一套Javascript功能集,它们把从HTML面调用应用服务器上的Java对象的方法简化了。它操控不同cd的参敎ͼq同时保持了HTML代码的可L?br />
DWR不是对一个设计的插入Q也不强q对象用Q何种cȝl承l构。它和servlet框架内的应用配合的很好。对~少DHTML~程l验的开发者来_DWR也提供了一个JavaScript库包含了l常使用的DHTMLdQ如l装表,用item填充select下拉框,改变HTML元素的内容,?lt;div>?lt;span>
DWR|站是详的q且有大量的文档Q这也是q篇文章的基。一些例子用来展CDWR如何使用和用它的库可以完成什么样的工作?br />
q篇文章让读者看C一个用了DWR的Web应用是如何一步步建立的。我会展C创个简单的CZ应用的必要的l节Q这个应用是可下载的q且可以在你的环境中布v来看看DWR如何工作?br />注意Q找到有关AJAX的信息ƈ不困?|页上有几篇文章和博客的条目늛了这个主题,每一个都试图指出和评个概늚不同的方面。在资源部分Q你会找C些有的指向CZ和文章的链接Q来学习AJAX的更多的内容?br />
CZ应用
q篇文章使用的示例应用模拟了多u多的一个公寓出U搜索引擎。用户可以在搜烦前选择一l搜索标准。ؓ了提高交互性,AJAX中以下两U情况下使用Q?br />·应用通告用户配合他的选择会返回多搜索结果。这个数字是实时更新?使用AJAX-当用户选择的卧室和室的数量,或者hD围变化时。当W合标准的搜索结果没有或太多Ӟ用户没有必要点L索按U?br />·数据库查询ƈ取回l果是由AJAX完成的。当用户按下昄l果按钮Ӟ数据库执行搜索。这P应用看v来更具响应了Q而整个页面不需要重载来昄l果?br />
数据?/span>
我们使用的数据库是HSQLQ它是一U占用资源很的Java SQL数据库引擎,可以不需要安装和配置的与Web应用捆绑在一赗一个SQL文g被用来在Web应用的上下文启动时创Z个内存中的表q添加一些记录?br />
Javac?/span>
应用包含了两个主要的cdApartment和ApartmentDAO。Apartment.javacL一个有着属性和getter/setterҎ的简单的JavacRApartmentDAO.java是数据访问类Q用来查询数据库q基于用L搜烦标准来返回信息。ApartmentDAOcȝ实现的直接了当的;它直接用了Java数据库联接调用来得到公寓的L和符合用戯求的可用公寓的列表?br />
DWR配置和?/span>
讄DWR的用是单的Q将DWR的jar文g拷入Web应用的WEB-INF/lib目录中,在web.xml中增加一个servlet声明Qƈ创徏DWR的配|文件。DWR的分发中需要用一个单独的jar文g。你必须DWR servlet加到应用的WEB-INF/web.xml中布|描q段中去?br />
				    <servlet>
        <servlet-name>dwr-invoker</servlet-name>
        <display-name>DWR Servlet</display-name>
        <description>Direct Web Remoter Servlet</description>
        <servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
        <init-param>
            <param-name>debug</param-name>
            <param-value>true</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>dwr-invoker</servlet-name>
        <url-pattern>/dwr/*</url-pattern>
    </servlet-mapping>


一个可选的步骤是设|DWR试模式—象上面的例子那样—在servlet描述D中debug参数设ؓtrue。当DWR在调试模式时Q你可以从HTMl|页中看到所有的可访问的Java对象。包含了可用对象列表的网会出现?WEBAPP/dwrq个url上,它显CZ对象的公共方法。所列方法可以从面中调用,允许你,W一ơ,q行服务器上的对象的Ҏ。下图显CZ调试늚样子:

image
调试?br />
现在你必让DWR知道通过XMLHttpRequest对象Q什么对象将会接收请求。这个Q务由叫做dwr.xml的配|文件来完成。在配置文g中,定义了DWR允许你从|页中调用的对象。从设计上讲QDWR允许讉K所有公布类的公共方法,但在我们的例子中Q我们只允许讉K几个Ҏ。下面是我们CZ的配|文?
				<dwr>
    <allow>
        <convert converter="bean" match="dwr.sample.Apartment"/>
        <create creator="new" javascript="ApartmentDAO" class="dwr.sample.ApartmentDAO">
            <include method="findApartments"/>
            <include method="countApartments"/>
        </create>
    </allow>
</dwr>


上面的文件实C我们例子中的两个目标。首先,<convert>标记告诉DWRdwr.sample.Apartment对象的类型{换ؓ联合数组Q因为,Z安全的原因,DWR默认的不会{换普通bean。第二,<create>标记让DWR暴露出dwr.sample.ApartmentDAOcȝJavaScript调用;我们在页面中使用JavaScript文g被javascript属性定义。我们必L?lt;include>标记Q它指明了dwr.sample.ApartmentDAOcȝ哪些Ҏ可用?br />
HTML/JSP代码
配置完成后,你就可以启动你的Web应用了,q时DWR会ؓ从你的HTML或Java服务器端面(JSP)上调用所需Ҏ作好准备Qƈ不需要你创徏JavaScript文g。在search.jsp文g中, 我们必须增加由DWR提供的JavaScript接口Q还有DWR引擎Q加入以下三行到我们的代码中Q?br />
				  <script src='dwr/interface/ApartmentDAO.js'></script>
  <script src='dwr/engine.js'></script>
  <script src='dwr/util.js'></script>


我们注意到当用户改变搜烦标准Ӟq是AJAX在示例程序中的首ơ应?正如他所看到的,当标准改变时Q可用的公寓数量被更C。我创徏了两个JavaScript函数Q当某一个选择下拉框中的值变化时被调用。ApartmentDAO.countApartments()函数是最重要的部分。最有趣的是W一个参? loadTotal()函数Q它指明了当接收到服务端的返回时DWR会调用的JavaScriptҎ。loadTotal于是被调用来在HTML面?lt;div>中显C结果。下面是在这个交互场景中所使用到的JavaScript函数:

				function updateTotal() {
    $("resultTable").style.display = 'none';
    var bedrooms = document.getElementById("bedrooms").value;
    var bathrooms = document.getElementById("bathrooms").value;
    var price = document.getElementById("price").value;
    ApartmentDAO.countApartments(loadTotal, bedrooms, bathrooms, price);
}

function loadTotal(data) {
    document.getElementById("totalRecords").innerHTML = data;
}


很明显,用户想看到符合他的搜索条件的公寓列表。那么,当用户对他的搜烦标准感到满意Qƈ且L也是有效的话Q他会按下显C结果的按纽Q这会调用updateResults() JavaScriptҎQ?br />
				function updateResults() {
    
    DWRUtil.removeAllRows("apartmentsbody");
    var bedrooms = document.getElementById("bedrooms").value;
    var bathrooms = document.getElementById("bathrooms").value;
    var price = document.getElementById("price").value;
    ApartmentDAO.findApartments(fillTable, bedrooms, bathrooms, price);
    $("resultTable").style.display = '';
}

function fillTable(apartment) {
    DWRUtil.addRows("apartmentsbody", apartment, [ getId, getAddress, getBedrooms, getBathrooms, getPrice ]);
}


updateResults()Ҏ清空了存放搜索返回结果的表域Q从用户界面上获取所需参数Qƈ且将q些参数传给DWR创徏的ApartmentDAO对象。然后数据库查询被执行QfillTable()会被调用,它解析了DWRq回的对?apartment)Q然后将其显C到面?apartmentsbody)?br />
安全因素
Z保持CZ的简要,ApartmentDAOcd可能的保持简单,但这L一个类通常有一l设|方法来操作数据Q如insert(), update()和delete()。DWR暴露了所有公共方法给所有的HTML面调用。出于安全的原因Q像q样暴露你的数据讉K层是不明智的。开发者可以创Z个门面来集中所有JavaScript函数与底层业务组件之间的通信Q这样就限制了过多暴露的功能?br />
l论
q篇文章仅仅让你在你的项目中使用由DWR支持的AJAX开了个头。DWR让你集中注意力在如何提高你的应用的交互模型上面,消除了编写和调试JavaScript代码的负担。用AJAX最有趣的挑战是定义在哪里和如何提高可用性。DWR负责了操作Web面与你的Java对象之间的通信Q这样就帮助你完全集中注意力在如何让你的应用的用L面更加友好,
我想感谢Mircea Oancea和Marcos Pereira,他们阅读了这文章ƈl予了非常有价值的q匮?br />
资源
·javaworld.com:
javaworld.com
·Matrix-Java开发者社? http://www.matrix.org.cn/
·onjava.com: onjava.com
·下蝲CZE序的全部源? http://www.javaworld.com/javaworld/jw-06-2005/dwr/jw-0620-dwr.war
·DWR: http://www.getahead.ltd.uk/dwr/index.html
·HSQL:http://hsqldb.sourceforge.net/
·AJAX的定?http://en.wikipedia.org/wiki/AJAX
· “AJAX:通向Web应用的新途径": Jesse James Garrett (Adaptive Path, 2005.2): http://www.adaptivepath.com/publications/essays/archives/000385.php
· “非常动态的Web界面?Drew McLellan (xml.com, 2005.2): http://www.xml.com/pub/a/2005/02/09/xml-http-request.html
·XMLHttpRequest & AJAX 工作范例: http://www.fiftyfoureleven.com/resources/programming/xmlhttprequest/examples
· “可用的XMLHttpRequest实践?Thomas Baekdal (Baekdal.com, 2005.3): http://www.baekdal.com/articles/Usability/usable-XMLHttpRequest/
·"XMLHttpRequest使用导引" Thomas Baekdal (Baekdal.com,  2005.2):http://www.baekdal.com/articles/Usability/XMLHttpRequest-guidelines/
·AJAX实质:http://www.ajaxmatters.com/

(看完后个人感觉:有了DWRJAVA开发而言Q完全可以与AJAXҎ啦,省了在JS上对XMLHTTP以及对DOM的处理,不可以避免地在后台对应的IO处理Q然后就DWR来说Q它增加了对XML中对应的配置--在开源框架中g一直不曑ց止过。还有对一些DWR自有用法如DWRUtil.addRows得参考其相关文---当然q样的功能我们自׃可以用JS来解冻Iq且它显然很实用。add by jkallen)


Jkallen 2006-07-11 10:53 发表评论
]]>
Ajax---与服务器通信Q请求参C为XML发送)http://www.aygfsteel.com/Jkallen/archive/2006/03/29/38049.htmlJkallenJkallenWed, 29 Mar 2006 07:04:00 GMThttp://www.aygfsteel.com/Jkallen/archive/2006/03/29/38049.htmlhttp://www.aygfsteel.com/Jkallen/comments/38049.htmlhttp://www.aygfsteel.com/Jkallen/archive/2006/03/29/38049.html#Feedback0http://www.aygfsteel.com/Jkallen/comments/commentRss/38049.htmlhttp://www.aygfsteel.com/Jkallen/services/trackbacks/38049.html阅读全文

Jkallen 2006-03-29 15:04 发表评论
]]>
Ajax---通过JSON与服务器通信Q发送请求和处理响应Q?/title><link>http://www.aygfsteel.com/Jkallen/archive/2006/03/28/37905.html</link><dc:creator>Jkallen</dc:creator><author>Jkallen</author><pubDate>Tue, 28 Mar 2006 14:28:00 GMT</pubDate><guid>http://www.aygfsteel.com/Jkallen/archive/2006/03/28/37905.html</guid><wfw:comment>http://www.aygfsteel.com/Jkallen/comments/37905.html</wfw:comment><comments>http://www.aygfsteel.com/Jkallen/archive/2006/03/28/37905.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.aygfsteel.com/Jkallen/comments/commentRss/37905.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/Jkallen/services/trackbacks/37905.html</trackback:ping><description><![CDATA[ <font color="#008000" size="2">好久没有做过关于AJAX的东西了Q^时做的也无非是通过XHP要么用GETQ请求部分是q加到URL中的q且览器对光度有会有限制Q方法要么通过POSTQ请求参数内容可以放到请求体中ƈ且无量的限制Q方法。传递的无非是些参数|以名=值的方式出现as : name=jkallenQ要么就是DOM对象了。再发到到服务端后做些处理再q回到客L昄对应l果?br /><br />今天看到了一个好东东Q就是通过<span style="FONT-WEIGHT: bold">JSON</span>(JavaScript Object Notation)向服务器发送数据。它是一U轻量的数据交换格式,使用了类gC语言家族的习惯?br /><br /><br /></font> <p align="left"> <font color="#008000"> <font size="2">JSONh以下q些形式Q?/font> <br /> </font> </p> <p> <font color="#006400" size="2">a :  对象是一个无序的“‘名U?值’对”集合。一个对象以“{”(左括P开始,“}”(xPl束。每个“名U”后跟一个?”(冒号Q;“‘名U?值?对”之间用?”(逗号Q分隔?<br /></font> </p> <p> <font color="#006400" size="2">b:   数组是|valueQ的有序集合。一个数l以“[”(左中括号Q开始,“]”(右中括号Q结束。g间用?”(逗号Q分隔?<br /></font> </p> <p> <font color="#006400" size="2">c:   |<i>value</i>Q可以是双引hh的字W串Q?i>string</i>Q、数?number)?ture、false?null、对象(objectQ或者数l(arrayQ。这些结构可以嵌套?<br /></font> </p> <p> <font color="#000000" size="2"> <font color="#006400">d:   字符Ԍ<i>string</i>Q是由双引号包围的Q意数量Unicode字符的集合,使用反斜U{义。一个字W(characterQ即一个单独的字符Ԍcharacter stringQ?/font> <br /> </font> </p> <p> <font color="#008000" size="2">不多说了Q关于JSON其实?/font> <a >www.json.org</a> <font color="#008000" size="2">上讲得太好了Q上面不但有每个JAVA的源文gQ而且每个文g都有与之对应的JAVA DOCQ还有不实例!<br /><br />我就选了json in java  and  json in javascript 二个链接 ,从这二个地方可以下到JSON的javascript包和Java包?br /><br />Javascript 包的应用如下Q?br /><br /><strong>首先创徏一个符合json的对象,q过XHP发送到sever</strong><br /></font> </p> <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"> <img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /> <span style="COLOR: #000000"><</span> <span style="COLOR: #000000">script type</span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">text/javascript</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000"> src</span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">json.js</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">></</span> <span style="COLOR: #000000">script</span> <span style="COLOR: #000000">></span> <span style="COLOR: #000000"> <br /> <img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /> </span> <span style="COLOR: #000000"><</span> <span style="COLOR: #000000">script type</span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">text/javascript</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">></span> <span style="COLOR: #000000"> <br /> <img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /> <br /> <img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /> </span> <span style="COLOR: #0000ff">var</span> <span style="COLOR: #000000"> xmlHttp;<br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /><br /><img id="Codehighlighter1_134_317_Open_Image" onclick="this.style.display='none'; Codehighlighter1_134_317_Open_Text.style.display='none'; Codehighlighter1_134_317_Closed_Image.style.display='inline'; Codehighlighter1_134_317_Closed_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" /><img id="Codehighlighter1_134_317_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_134_317_Closed_Text.style.display='none'; Codehighlighter1_134_317_Open_Image.style.display='inline'; Codehighlighter1_134_317_Open_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ContractedBlock.gif" align="top" /></span> <span style="COLOR: #0000ff">function</span> <span style="COLOR: #000000"> createXMLHttpRequest() </span> <span id="Codehighlighter1_134_317_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.aygfsteel.com/images/dot.gif" /> </span> <span id="Codehighlighter1_134_317_Open_Text"> <span style="COLOR: #000000">{<br /><img id="Codehighlighter1_166_230_Open_Image" onclick="this.style.display='none'; Codehighlighter1_166_230_Open_Text.style.display='none'; Codehighlighter1_166_230_Closed_Image.style.display='inline'; Codehighlighter1_166_230_Closed_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /><img id="Codehighlighter1_166_230_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_166_230_Closed_Text.style.display='none'; Codehighlighter1_166_230_Open_Image.style.display='inline'; Codehighlighter1_166_230_Open_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ContractedSubBlock.gif" align="top" />    </span> <span style="COLOR: #0000ff">if</span> <span style="COLOR: #000000"> (window.ActiveXObject) </span> <span id="Codehighlighter1_166_230_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.aygfsteel.com/images/dot.gif" /> </span> <span id="Codehighlighter1_166_230_Open_Text"> <span style="COLOR: #000000">{<br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />        xmlHttp </span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000"> </span> <span style="COLOR: #0000ff">new</span> <span style="COLOR: #000000"> ActiveXObject(</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">Microsoft.XMLHTTP</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">);<br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />    }</span> </span> <span style="COLOR: #000000"> <br /><img id="Codehighlighter1_269_315_Open_Image" onclick="this.style.display='none'; Codehighlighter1_269_315_Open_Text.style.display='none'; Codehighlighter1_269_315_Closed_Image.style.display='inline'; Codehighlighter1_269_315_Closed_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /><img id="Codehighlighter1_269_315_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_269_315_Closed_Text.style.display='none'; Codehighlighter1_269_315_Open_Image.style.display='inline'; Codehighlighter1_269_315_Open_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ContractedSubBlock.gif" align="top" />    </span> <span style="COLOR: #0000ff">else</span> <span style="COLOR: #000000"> </span> <span style="COLOR: #0000ff">if</span> <span style="COLOR: #000000"> (window.XMLHttpRequest) </span> <span id="Codehighlighter1_269_315_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.aygfsteel.com/images/dot.gif" /> </span> <span id="Codehighlighter1_269_315_Open_Text"> <span style="COLOR: #000000">{<br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />        xmlHttp </span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000"> </span> <span style="COLOR: #0000ff">new</span> <span style="COLOR: #000000"> XMLHttpRequest();<br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />    }</span> </span> <span style="COLOR: #000000"> <br /> <img src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</span> </span> <span style="COLOR: #000000"> <br /> <img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" />    <br /><img id="Codehighlighter1_342_842_Open_Image" onclick="this.style.display='none'; Codehighlighter1_342_842_Open_Text.style.display='none'; Codehighlighter1_342_842_Closed_Image.style.display='inline'; Codehighlighter1_342_842_Closed_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" /><img id="Codehighlighter1_342_842_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_342_842_Closed_Text.style.display='none'; Codehighlighter1_342_842_Open_Image.style.display='inline'; Codehighlighter1_342_842_Open_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ContractedBlock.gif" align="top" /></span> <span style="COLOR: #0000ff">function</span> <span style="COLOR: #000000"> doJSON() </span> <span id="Codehighlighter1_342_842_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.aygfsteel.com/images/dot.gif" /> </span> <span id="Codehighlighter1_342_842_Open_Text"> <span style="COLOR: #000000">{<br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />    </span> <span style="COLOR: #0000ff">var</span> <span style="COLOR: #000000"> car </span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000"> getCarObject();<br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />    <br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />    </span> <span style="COLOR: #008000">//</span> <span style="COLOR: #008000">Use the JSON JavaScript library to stringify the Car object</span> <span style="COLOR: #008000"> <br /> <img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" /> </span> <span style="COLOR: #000000">    </span> <span style="COLOR: #0000ff">var</span> <span style="COLOR: #000000"> carAsJSON </span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000"> JSON.stringify(car);<br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />    alert(</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">Car object as JSON:\n </span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000"> </span> <span style="COLOR: #000000">+</span> <span style="COLOR: #000000"> carAsJSON);<br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />    <br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />    </span> <span style="COLOR: #0000ff">var</span> <span style="COLOR: #000000"> url </span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000"> </span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">JSONExample?timeStamp=</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000"> </span> <span style="COLOR: #000000">+</span> <span style="COLOR: #000000"> </span> <span style="COLOR: #0000ff">new</span> <span style="COLOR: #000000"> Date().getTime();<br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />    <br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />    createXMLHttpRequest();<br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />    xmlHttp.open(</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">POST</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">, url, </span> <span style="COLOR: #0000ff">true</span> <span style="COLOR: #000000">);<br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />    xmlHttp.onreadystatechange </span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000"> handleStateChange;<br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />    xmlHttp.setRequestHeader(</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">Content-Type</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">, </span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">application/x-www-form-urlencoded</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">);    <br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />    xmlHttp.send(carAsJSON);<br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</span> </span> <span style="COLOR: #000000"> <br /> <img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" />    <br /><img id="Codehighlighter1_878_994_Open_Image" onclick="this.style.display='none'; Codehighlighter1_878_994_Open_Text.style.display='none'; Codehighlighter1_878_994_Closed_Image.style.display='inline'; Codehighlighter1_878_994_Closed_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" /><img id="Codehighlighter1_878_994_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_878_994_Closed_Text.style.display='none'; Codehighlighter1_878_994_Open_Image.style.display='inline'; Codehighlighter1_878_994_Open_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ContractedBlock.gif" align="top" /></span> <span style="COLOR: #0000ff">function</span> <span style="COLOR: #000000"> handleStateChange() </span> <span id="Codehighlighter1_878_994_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.aygfsteel.com/images/dot.gif" /> </span> <span id="Codehighlighter1_878_994_Open_Text"> <span style="COLOR: #000000">{<br /><img id="Codehighlighter1_912_992_Open_Image" onclick="this.style.display='none'; Codehighlighter1_912_992_Open_Text.style.display='none'; Codehighlighter1_912_992_Closed_Image.style.display='inline'; Codehighlighter1_912_992_Closed_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /><img id="Codehighlighter1_912_992_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_912_992_Closed_Text.style.display='none'; Codehighlighter1_912_992_Open_Image.style.display='inline'; Codehighlighter1_912_992_Open_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ContractedSubBlock.gif" align="top" />    </span> <span style="COLOR: #0000ff">if</span> <span style="COLOR: #000000">(xmlHttp.readyState </span> <span style="COLOR: #000000">==</span> <span style="COLOR: #000000"> </span> <span style="COLOR: #000000">4</span> <span style="COLOR: #000000">) </span> <span id="Codehighlighter1_912_992_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.aygfsteel.com/images/dot.gif" /> </span> <span id="Codehighlighter1_912_992_Open_Text"> <span style="COLOR: #000000">{<br /><img id="Codehighlighter1_948_986_Open_Image" onclick="this.style.display='none'; Codehighlighter1_948_986_Open_Text.style.display='none'; Codehighlighter1_948_986_Closed_Image.style.display='inline'; Codehighlighter1_948_986_Closed_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /><img id="Codehighlighter1_948_986_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_948_986_Closed_Text.style.display='none'; Codehighlighter1_948_986_Open_Image.style.display='inline'; Codehighlighter1_948_986_Open_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ContractedSubBlock.gif" align="top" />        </span> <span style="COLOR: #0000ff">if</span> <span style="COLOR: #000000">(xmlHttp.status </span> <span style="COLOR: #000000">==</span> <span style="COLOR: #000000"> </span> <span style="COLOR: #000000">200</span> <span style="COLOR: #000000">) </span> <span id="Codehighlighter1_948_986_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.aygfsteel.com/images/dot.gif" /> </span> <span id="Codehighlighter1_948_986_Open_Text"> <span style="COLOR: #000000">{<br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />            parseResults();<br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />        }</span> </span> <span style="COLOR: #000000"> <br /> <img src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />    }</span> </span> <span style="COLOR: #000000"> <br /> <img src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</span> </span> <span style="COLOR: #000000"> <br /> <img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /> <br /> <img id="Codehighlighter1_1021_1310_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1021_1310_Open_Text.style.display='none'; Codehighlighter1_1021_1310_Closed_Image.style.display='inline'; Codehighlighter1_1021_1310_Closed_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" /> <img id="Codehighlighter1_1021_1310_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_1021_1310_Closed_Text.style.display='none'; Codehighlighter1_1021_1310_Open_Image.style.display='inline'; Codehighlighter1_1021_1310_Open_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ContractedBlock.gif" align="top" /> </span> <span style="COLOR: #0000ff">function</span> <span style="COLOR: #000000"> parseResults() </span> <span id="Codehighlighter1_1021_1310_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.aygfsteel.com/images/dot.gif" /> </span> <span id="Codehighlighter1_1021_1310_Open_Text"> <span style="COLOR: #000000">{<br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />    </span> <span style="COLOR: #0000ff">var</span> <span style="COLOR: #000000"> responseDiv </span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000"> document.getElementById(</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">serverResponse</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">);<br /><img id="Codehighlighter1_1124_1190_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1124_1190_Open_Text.style.display='none'; Codehighlighter1_1124_1190_Closed_Image.style.display='inline'; Codehighlighter1_1124_1190_Closed_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /><img id="Codehighlighter1_1124_1190_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_1124_1190_Closed_Text.style.display='none'; Codehighlighter1_1124_1190_Open_Image.style.display='inline'; Codehighlighter1_1124_1190_Open_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ContractedSubBlock.gif" align="top" />    </span> <span style="COLOR: #0000ff">if</span> <span style="COLOR: #000000">(responseDiv.hasChildNodes()) </span> <span id="Codehighlighter1_1124_1190_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.aygfsteel.com/images/dot.gif" /> </span> <span id="Codehighlighter1_1124_1190_Open_Text"> <span style="COLOR: #000000">{<br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />        responseDiv.removeChild(responseDiv.childNodes[</span> <span style="COLOR: #000000">0</span> <span style="COLOR: #000000">]);<br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />    }</span> </span> <span style="COLOR: #000000"> <br /> <img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />    <br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />    </span> <span style="COLOR: #0000ff">var</span> <span style="COLOR: #000000"> responseText </span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000"> document.createTextNode(xmlHttp.responseText);<br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />    responseDiv.appendChild(responseText);<br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</span> </span> <span style="COLOR: #000000"> <br /> <img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /> <br /> <img id="Codehighlighter1_1337_1399_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1337_1399_Open_Text.style.display='none'; Codehighlighter1_1337_1399_Closed_Image.style.display='inline'; Codehighlighter1_1337_1399_Closed_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" /> <img id="Codehighlighter1_1337_1399_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_1337_1399_Closed_Text.style.display='none'; Codehighlighter1_1337_1399_Open_Image.style.display='inline'; Codehighlighter1_1337_1399_Open_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ContractedBlock.gif" align="top" /> </span> <span style="COLOR: #0000ff">function</span> <span style="COLOR: #000000"> getCarObject() </span> <span id="Codehighlighter1_1337_1399_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.aygfsteel.com/images/dot.gif" /> </span> <span id="Codehighlighter1_1337_1399_Open_Text"> <span style="COLOR: #000000">{<br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />    </span> <span style="COLOR: #0000ff">return</span> <span style="COLOR: #000000"> </span> <span style="COLOR: #0000ff">new</span> <span style="COLOR: #000000"> Car(</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">Dodge</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">, </span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">Coronet R/T</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">, </span> <span style="COLOR: #000000">1968</span> <span style="COLOR: #000000">, </span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">yellow</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">);<br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</span> </span> <span style="COLOR: #000000"> <br /> <img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /> <br /> <img id="Codehighlighter1_1441_1535_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1441_1535_Open_Text.style.display='none'; Codehighlighter1_1441_1535_Closed_Image.style.display='inline'; Codehighlighter1_1441_1535_Closed_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" /> <img id="Codehighlighter1_1441_1535_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_1441_1535_Closed_Text.style.display='none'; Codehighlighter1_1441_1535_Open_Image.style.display='inline'; Codehighlighter1_1441_1535_Open_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ContractedBlock.gif" align="top" /> </span> <span style="COLOR: #0000ff">function</span> <span style="COLOR: #000000"> Car(make, model, year, color) </span> <span id="Codehighlighter1_1441_1535_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.aygfsteel.com/images/dot.gif" /> </span> <span id="Codehighlighter1_1441_1535_Open_Text"> <span style="COLOR: #000000">{<br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />    </span> <span style="COLOR: #0000ff">this</span> <span style="COLOR: #000000">.make </span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000"> make;<br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />    </span> <span style="COLOR: #0000ff">this</span> <span style="COLOR: #000000">.model </span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000"> model;<br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />    </span> <span style="COLOR: #0000ff">this</span> <span style="COLOR: #000000">.year </span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000"> year;<br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />    </span> <span style="COLOR: #0000ff">this</span> <span style="COLOR: #000000">.color </span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000"> color;<br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</span> </span> <span style="COLOR: #000000"> <br /> <img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /> <br /> <img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /> </span> <span style="COLOR: #000000"></</span> <span style="COLOR: #000000">script</span> <span style="COLOR: #000000">></span> <span style="COLOR: #000000"> <br /> <img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /> </span> </div> <br /> <font style="BACKGROUND-COLOR: #ffffff"> <font color="#008000"> <font size="2">那个json.js是我们下下来的包啦Q?br /><br />我们再看看在<strong>server端的处理</strong></font> <br /> </font> </font> <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"> <img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /> <span style="COLOR: #0000ff">package</span> <span style="COLOR: #000000"> ajaxbook.chap3;<br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /><br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /></span> <span style="COLOR: #0000ff">import</span> <span style="COLOR: #000000"> java.io.</span> <span style="COLOR: #000000">*</span> <span style="COLOR: #000000">;<br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /></span> <span style="COLOR: #0000ff">import</span> <span style="COLOR: #000000"> java.net.</span> <span style="COLOR: #000000">*</span> <span style="COLOR: #000000">;<br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /></span> <span style="COLOR: #0000ff">import</span> <span style="COLOR: #000000"> java.text.ParseException;<br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /></span> <span style="COLOR: #0000ff">import</span> <span style="COLOR: #000000"> javax.servlet.</span> <span style="COLOR: #000000">*</span> <span style="COLOR: #000000">;<br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /></span> <span style="COLOR: #0000ff">import</span> <span style="COLOR: #000000"> javax.servlet.http.</span> <span style="COLOR: #000000">*</span> <span style="COLOR: #000000">;<br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /></span> <span style="COLOR: #0000ff">import</span> <span style="COLOR: #000000"> org.json.JSONObject;<br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /><br /><img id="Codehighlighter1_222_1579_Open_Image" onclick="this.style.display='none'; Codehighlighter1_222_1579_Open_Text.style.display='none'; Codehighlighter1_222_1579_Closed_Image.style.display='inline'; Codehighlighter1_222_1579_Closed_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" /><img id="Codehighlighter1_222_1579_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_222_1579_Closed_Text.style.display='none'; Codehighlighter1_222_1579_Open_Image.style.display='inline'; Codehighlighter1_222_1579_Open_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ContractedBlock.gif" align="top" /></span> <span style="COLOR: #0000ff">public</span> <span style="COLOR: #000000"> </span> <span style="COLOR: #0000ff">class</span> <span style="COLOR: #000000"> JSONExample </span> <span style="COLOR: #0000ff">extends</span> <span style="COLOR: #000000"> HttpServlet </span> <span id="Codehighlighter1_222_1579_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.aygfsteel.com/images/dot.gif" /> </span> <span id="Codehighlighter1_222_1579_Open_Text"> <span style="COLOR: #000000">{<br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />    <br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />    </span> <span style="COLOR: #0000ff">protected</span> <span style="COLOR: #000000"> </span> <span style="COLOR: #0000ff">void</span> <span style="COLOR: #000000"> doPost(HttpServletRequest request, HttpServletResponse response)<br /><img id="Codehighlighter1_354_1081_Open_Image" onclick="this.style.display='none'; Codehighlighter1_354_1081_Open_Text.style.display='none'; Codehighlighter1_354_1081_Closed_Image.style.display='inline'; Codehighlighter1_354_1081_Closed_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /><img id="Codehighlighter1_354_1081_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_354_1081_Closed_Text.style.display='none'; Codehighlighter1_354_1081_Open_Image.style.display='inline'; Codehighlighter1_354_1081_Open_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ContractedSubBlock.gif" align="top" />    </span> <span style="COLOR: #0000ff">throws</span> <span style="COLOR: #000000"> ServletException, IOException </span> <span id="Codehighlighter1_354_1081_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.aygfsteel.com/images/dot.gif" /> </span> <span id="Codehighlighter1_354_1081_Open_Text"> <span style="COLOR: #000000">{<br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />        String json </span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000"> readJSONStringFromRequestBody(request);<br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />        <br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />        </span> <span style="COLOR: #008000">//</span> <span style="COLOR: #008000">Use the JSON-Java binding library to create a JSON object in Java</span> <span style="COLOR: #008000"> <br /> <img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" /> </span> <span style="COLOR: #000000">        JSONObject jsonObject </span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000"> </span> <span style="COLOR: #0000ff">null</span> <span style="COLOR: #000000">;<br /><img id="Codehighlighter1_553_610_Open_Image" onclick="this.style.display='none'; Codehighlighter1_553_610_Open_Text.style.display='none'; Codehighlighter1_553_610_Closed_Image.style.display='inline'; Codehighlighter1_553_610_Closed_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /><img id="Codehighlighter1_553_610_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_553_610_Closed_Text.style.display='none'; Codehighlighter1_553_610_Open_Image.style.display='inline'; Codehighlighter1_553_610_Open_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ContractedSubBlock.gif" align="top" />        </span> <span style="COLOR: #0000ff">try</span> <span style="COLOR: #000000"> </span> <span id="Codehighlighter1_553_610_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.aygfsteel.com/images/dot.gif" /> </span> <span id="Codehighlighter1_553_610_Open_Text"> <span style="COLOR: #000000">{<br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />            jsonObject </span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000"> </span> <span style="COLOR: #0000ff">new</span> <span style="COLOR: #000000"> JSONObject(json);<br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />        }</span> </span> <span style="COLOR: #000000"> <br /> <img id="Codehighlighter1_645_723_Open_Image" onclick="this.style.display='none'; Codehighlighter1_645_723_Open_Text.style.display='none'; Codehighlighter1_645_723_Closed_Image.style.display='inline'; Codehighlighter1_645_723_Closed_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /> <img id="Codehighlighter1_645_723_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_645_723_Closed_Text.style.display='none'; Codehighlighter1_645_723_Open_Image.style.display='inline'; Codehighlighter1_645_723_Open_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ContractedSubBlock.gif" align="top" />        </span> <span style="COLOR: #0000ff">catch</span> <span style="COLOR: #000000">(ParseException pe) </span> <span id="Codehighlighter1_645_723_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.aygfsteel.com/images/dot.gif" /> </span> <span id="Codehighlighter1_645_723_Open_Text"> <span style="COLOR: #000000">{<br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />            System.out.println(</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">ParseException: </span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000"> </span> <span style="COLOR: #000000">+</span> <span style="COLOR: #000000"> pe.toString());<br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />        }</span> </span> <span style="COLOR: #000000"> <br /> <img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />        <br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />        String responseText </span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000"> </span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">You have a </span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000"> </span> <span style="COLOR: #000000">+</span> <span style="COLOR: #000000"> jsonObject.getInt(</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">year</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">) </span> <span style="COLOR: #000000">+</span> <span style="COLOR: #000000"> </span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000"> </span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000"> <br /> <img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />            </span> <span style="COLOR: #000000">+</span> <span style="COLOR: #000000"> jsonObject.getString(</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">make</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">) </span> <span style="COLOR: #000000">+</span> <span style="COLOR: #000000"> </span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000"> </span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000"> </span> <span style="COLOR: #000000">+</span> <span style="COLOR: #000000"> jsonObject.getString(</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">model</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">)<br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />            </span> <span style="COLOR: #000000">+</span> <span style="COLOR: #000000"> </span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000"> </span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000"> </span> <span style="COLOR: #000000">+</span> <span style="COLOR: #000000"> </span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000"> that is </span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000"> </span> <span style="COLOR: #000000">+</span> <span style="COLOR: #000000"> jsonObject.getString(</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">color</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">) </span> <span style="COLOR: #000000">+</span> <span style="COLOR: #000000"> </span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000"> in color.</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">;<br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />        <br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />        response.setContentType(</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">text/xml</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">);<br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />        response.getWriter().print(responseText);<br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />    }</span> </span> <span style="COLOR: #000000"> <br /> <img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" /> <br /> <img id="Codehighlighter1_1160_1577_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1160_1577_Open_Text.style.display='none'; Codehighlighter1_1160_1577_Closed_Image.style.display='inline'; Codehighlighter1_1160_1577_Closed_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /> <img id="Codehighlighter1_1160_1577_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_1160_1577_Closed_Text.style.display='none'; Codehighlighter1_1160_1577_Open_Image.style.display='inline'; Codehighlighter1_1160_1577_Open_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ContractedSubBlock.gif" align="top" />    </span> <span style="COLOR: #0000ff">private</span> <span style="COLOR: #000000"> String readJSONStringFromRequestBody(HttpServletRequest request)</span> <span id="Codehighlighter1_1160_1577_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.aygfsteel.com/images/dot.gif" /> </span> <span id="Codehighlighter1_1160_1577_Open_Text"> <span style="COLOR: #000000">{<br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />        StringBuffer json </span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000"> </span> <span style="COLOR: #0000ff">new</span> <span style="COLOR: #000000"> StringBuffer();<br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />        String line </span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000"> </span> <span style="COLOR: #0000ff">null</span> <span style="COLOR: #000000">;<br /><img id="Codehighlighter1_1250_1422_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1250_1422_Open_Text.style.display='none'; Codehighlighter1_1250_1422_Closed_Image.style.display='inline'; Codehighlighter1_1250_1422_Closed_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /><img id="Codehighlighter1_1250_1422_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_1250_1422_Closed_Text.style.display='none'; Codehighlighter1_1250_1422_Open_Image.style.display='inline'; Codehighlighter1_1250_1422_Open_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ContractedSubBlock.gif" align="top" />        </span> <span style="COLOR: #0000ff">try</span> <span style="COLOR: #000000"> </span> <span id="Codehighlighter1_1250_1422_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.aygfsteel.com/images/dot.gif" /> </span> <span id="Codehighlighter1_1250_1422_Open_Text"> <span style="COLOR: #000000">{<br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />            BufferedReader reader </span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000"> request.getReader();<br /><img id="Codehighlighter1_1363_1412_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1363_1412_Open_Text.style.display='none'; Codehighlighter1_1363_1412_Closed_Image.style.display='inline'; Codehighlighter1_1363_1412_Closed_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /><img id="Codehighlighter1_1363_1412_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_1363_1412_Closed_Text.style.display='none'; Codehighlighter1_1363_1412_Open_Image.style.display='inline'; Codehighlighter1_1363_1412_Open_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ContractedSubBlock.gif" align="top" />            </span> <span style="COLOR: #0000ff">while</span> <span style="COLOR: #000000">((line </span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000"> reader.readLine()) </span> <span style="COLOR: #000000">!=</span> <span style="COLOR: #000000"> </span> <span style="COLOR: #0000ff">null</span> <span style="COLOR: #000000">) </span> <span id="Codehighlighter1_1363_1412_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.aygfsteel.com/images/dot.gif" /> </span> <span id="Codehighlighter1_1363_1412_Open_Text"> <span style="COLOR: #000000">{<br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />                json.append(line);<br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />            }</span> </span> <span style="COLOR: #000000"> <br /> <img src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />        }</span> </span> <span style="COLOR: #000000"> <br /> <img id="Codehighlighter1_1451_1539_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1451_1539_Open_Text.style.display='none'; Codehighlighter1_1451_1539_Closed_Image.style.display='inline'; Codehighlighter1_1451_1539_Closed_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /> <img id="Codehighlighter1_1451_1539_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_1451_1539_Closed_Text.style.display='none'; Codehighlighter1_1451_1539_Open_Image.style.display='inline'; Codehighlighter1_1451_1539_Open_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ContractedSubBlock.gif" align="top" />        </span> <span style="COLOR: #0000ff">catch</span> <span style="COLOR: #000000">(Exception e) </span> <span id="Codehighlighter1_1451_1539_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.aygfsteel.com/images/dot.gif" /> </span> <span id="Codehighlighter1_1451_1539_Open_Text"> <span style="COLOR: #000000">{<br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />            System.out.println(</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">Error reading JSON string: </span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000"> </span> <span style="COLOR: #000000">+</span> <span style="COLOR: #000000"> e.toString());<br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />        }</span> </span> <span style="COLOR: #000000"> <br /> <img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />        </span> <span style="COLOR: #0000ff">return</span> <span style="COLOR: #000000"> json.toString();<br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />    }</span> </span> <span style="COLOR: #000000"> <br /> <img src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</span> </span> <span style="COLOR: #000000"> <br /> <img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /> </span> </div> <br /> <font color="#008000"> <font size="2">那个不v眼的org.json.JSONObject是我们从JSON.ORG上下下来的JAVA包!它上面也有JSONObject的JAVA DOC所以不用担心不知道用啦Q?br /><br />OK到此全部搞定啦!<br /><br />可以看到q样比我们^旉过XHP传递一个DOM对象Q通过q接串来创徏XMLԌ要方便些Q再说通过q接串来处理也不是生成XML数据l构的健壮技术!<br /><br />参考资?/font>Q?/font> <font size="2"> <font color="#800080">foundations-of-ajax<br /></font>                    </font> <a > <font size="2">www.json.org</font> </a> <br /> <br />相关链接:  <a class="postTitle2" id="viewpost1_TitleUrl" href="/Jkallen/archive/2006/03/29/38049.html"><font color="#646464">Ajax---与服务器通信Q请求参C为XML发送)</font></a><img src ="http://www.aygfsteel.com/Jkallen/aggbug/37905.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/Jkallen/" target="_blank">Jkallen</a> 2006-03-28 22:28 <a href="http://www.aygfsteel.com/Jkallen/archive/2006/03/28/37905.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>打造完的JS树Ş菜单http://www.aygfsteel.com/Jkallen/archive/2006/03/28/37848.htmlJkallenJkallenTue, 28 Mar 2006 10:15:00 GMThttp://www.aygfsteel.com/Jkallen/archive/2006/03/28/37848.htmlhttp://www.aygfsteel.com/Jkallen/comments/37848.htmlhttp://www.aygfsteel.com/Jkallen/archive/2006/03/28/37848.html#Feedback4http://www.aygfsteel.com/Jkallen/comments/commentRss/37848.htmlhttp://www.aygfsteel.com/Jkallen/services/trackbacks/37848.html阅读全文

Jkallen 2006-03-28 18:15 发表评论
]]>
格式化JSP面http://www.aygfsteel.com/Jkallen/archive/2006/03/15/35389.htmlJkallenJkallenWed, 15 Mar 2006 04:50:00 GMThttp://www.aygfsteel.com/Jkallen/archive/2006/03/15/35389.htmlhttp://www.aygfsteel.com/Jkallen/comments/35389.htmlhttp://www.aygfsteel.com/Jkallen/archive/2006/03/15/35389.html#Feedback5http://www.aygfsteel.com/Jkallen/comments/commentRss/35389.htmlhttp://www.aygfsteel.com/Jkallen/services/trackbacks/35389.htmlJSP面里面的内容往往是动态显C?当然也一般是从DB中按需取出来的?
在显CE也怼到q样的问?

我们通过后台操作一些文?通知什么的)传到DB?其中隑օ会分D?分行什么的.可是我们从DB中取出来直接在JSP面昄时出来的却是从头到尾的一片文?!!

解决Ҏ:
首先应将在DB中的相关昄文字取出来的时候进行一个{?比如说把里面的回?换行,I格什么的转换成HTML能识别的标记.也许以下代码正是我们需要的:


public class CheckData extends Object {


    
/** *//** *//** *//**
     * CheckData 构造器
     *
     * 
@param
     * 
@return

     
*/


    
public CheckData() {}


    
/** *//** *//** *//**
     * 字符串替换,?nbsp;source 中的 oldString 全部换成 newString
     *
     * 
@param source 源字W串
     * 
@param oldString 老的字符?BR>     * @param newString 新的字符?BR>     * @return 替换后的字符?BR>     */

    
public static String Replace(String source, String oldString, String newString) {
        StringBuffer output 
= new StringBuffer();

        
int lengthOfSource = source.length();   // 源字W串长度
        int lengthOfOld = oldString.length();   // 老字W串长度

        
int posStart = 0;   // 开始搜索位|?/SPAN>
        int pos;            // 搜烦到老字W串的位|?/SPAN>

        
while ((pos = source.indexOf(oldString, posStart)) >= 0{
            output.append(source.substring(posStart, pos));

            output.append(newString);
            posStart 
= pos + lengthOfOld;
        }


        
if (posStart < lengthOfSource) {
            output.append(source.substring(posStart));
        }


        
return output.toString();
    }


    
/** *//**//*
    public static String ReplaceIgnoreCase(String source, String oldString, String newString) {
    }
    
*/


    
/** *//** *//** *//**
     * 字W串格式化成 HTML 代码输出
     * 只{换特D字W,适合?nbsp;HTML 中的表单区域
     *
     * 
@param str 要格式化的字W串
     * 
@return 格式化后的字W串
     
*/

    
public static String toHtmlInput(String str) {
        
if (str == null)    return null;

        String html 
= new String(str);

        html 
= Replace(html, "&""&amp;");
        html 
= Replace(html, "<""&lt;");
        html 
= Replace(html, ">""&gt;");

        
return html;
    }


    
/** *//** *//** *//**
     * 字W串格式化成 HTML 代码输出
     * 除普通特D字W外Q还对空根{制表符和换行进行{换,
     * 以将内容格式化输出,
     * 适合?nbsp;HTML 中的昄输出
     *
     * 
@param str 要格式化的字W串
     * 
@return 格式化后的字W串
     
*/

    
public static String toHtml(String str) {
        
if (str == null)    return null;

        String html 
= new String(str);

        html 
= toHtmlInput(html);
        html 
= Replace(html, "\r\n""\n");
        html 
= Replace(html, "\n""<br>\n");
        html 
= Replace(html, "\t""    ");
        html 
= Replace(html, "  "" &nbsp;");

        
return html;
    }


}



好啦,现在你可以直接通过c调用相应的Ҏ(比如说toHtml)?成了HTML能识别的格式?如你在DB中的内容可能如下:

"lilin and you do it "
也许转换后成?BR>"lilin and you do it<br> "

接下来也许我们会很自然地惛_用JS(innerTHML,document.write(){等来处?来将其输出到JSP?然而结果却往往不是我们惌?原因是你在DB中的数据不符合JS的语?因ؓ在JS中要求所有的输出语句均在同一行中,否则׃出现"未结束的字符串常?的错?而我们{换后的仅仅是把DB中数据用HTML标识W来替代,比如说若有一行空?那么很可能对应的是一?<BR>"W号,但是q个W号依然会占一?..(大概是这个意?

我在JSP面?直接它们out出来?想一下servlet是怎么直接昄布面内容的^_^,虽然我一向对servletq样的行为比较反?!不管我们转换后的字符串中有多行,全部把它攑ֈout中就可以?惟一的缺h又要在JSP中多一?<%=%>"W号.

不知道各位有什么别的好Ҏ没有?



Jkallen 2006-03-15 12:50 发表评论
]]>
妙用javascripthttp://www.aygfsteel.com/Jkallen/archive/2006/03/06/33886.htmlJkallenJkallenMon, 06 Mar 2006 09:05:00 GMThttp://www.aygfsteel.com/Jkallen/archive/2006/03/06/33886.htmlhttp://www.aygfsteel.com/Jkallen/comments/33886.htmlhttp://www.aygfsteel.com/Jkallen/archive/2006/03/06/33886.html#Feedback3http://www.aygfsteel.com/Jkallen/comments/commentRss/33886.htmlhttp://www.aygfsteel.com/Jkallen/services/trackbacks/33886.html阅读全文

Jkallen 2006-03-06 17:05 发表评论
]]>
վ֩ģ壺 | | | ĵ| | ȫ| | Ƽ| | ָɽ| ˼é| ԭ| | ͷ| ũ| ̨| | ˶| | | ϴ| ƽ| | | | | | ʡ| »| | | | ӳ| ¤| | ̨| | | | ֣| |