小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

AJAX應(yīng)用在JavaRSS.com

 非常主體 2006-09-28

像其他人一樣,當(dāng)我看到一下RIA應(yīng)用,例如Google Maps和Google Suggest的時候我都非常驚訝。我希望知道是如何實(shí)現(xiàn)的。現(xiàn)在,謎底揭開了,那就是AJAX。這是在我花了一段時間研究AJAX之后才知曉的。這里有一個很好的例子讓我們知道AJAX是如何很好的應(yīng)用在 JavaRSS.com 里面的。

什么是AJAX: AJAX 是一個架構(gòu)(architecture)并不是一種技術(shù)。AJAX代表異步的JavaScript和XML。

妙語(Punch Line): 延遲加載

問題: 當(dāng)JavaRSS.com首頁加載時,他同時加載了所有條目的介紹(如果你在設(shè)置中激活了)。這些介紹只有當(dāng)你鼠標(biāo)移動到該條目的上面的時候才顯示。

現(xiàn)在的問題是用戶不可能是鼠標(biāo)移過所有的條目,所以預(yù)先加載所有的介紹不是個好主意。

解決方案: 使用AJAX,當(dāng)鼠標(biāo)移過的時候從服務(wù)器動態(tài)加載條目的介紹。

這么做可以使初始頁的加載大小減小一半甚至更多,這樣一來頁面加載就更快,就內(nèi)能得到一個更好的用戶體驗(yàn)。

時序圖:

AJAX Sequence Diagram

我們首先會在onmouseover事件中調(diào)用JavaScript函數(shù)getDescription。下面是html代碼:

<a href="/" onmouseover="getDescription(3,1)">Java & J2EE News<a>
下面是 getDescription 函數(shù)的代碼:
    var url = ‘http://localhost:8080/getDescription.jsp?channelId=‘ + channelId + ‘&itemId=‘ + itemId;
if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
} else if (window.ActiveXObject) {
req = new ActiveXObject("Microsoft.XMLHTTP");
}
req.onreadystatechange = processRequest;
req.open("GET", url, true);
req.send(null);

XMLHttpRequest 對象將用來進(jìn)行http連接并取回xml文檔。我們需要檢測一下是否是IE并且創(chuàng)建 XMLHttpRequest 對象。

    if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
} else if (window.ActiveXObject) {
req = new ActiveXObject("Microsoft.XMLHTTP");
}

設(shè)置回調(diào)函數(shù),并且發(fā)送"GET"請求至服務(wù)器接收xml文檔:

    req.onreadystatechange = processRequest;
req.open("GET", url, true);
req.send(null);

JSP將根據(jù)適當(dāng)?shù)臈l目編號創(chuàng)建具有相應(yīng)介紹的xml文檔。

<%
String channelId = request.getParameter("channelId");
String itemId = request.getParameter("itemId");
//String description = new Channel(channelId).getItemDescription(itemId);
String description = "This is the description for the channelId: " + channelId + "and itemId: " + itemId;
if (description != null) {
response.setContentType("text/xml");
response.setHeader("Cache-Control", "no-cache");
response.getWriter().write("" + description.toString() + "");
} else {
//nothing to show
response.setStatus(HttpServletResponse.SC_NO_CONTENT);
}
%>

檢測HTTP請求返回狀態(tài)碼,狀態(tài)為200,即OK。

function processRequest() {
if (req.readyState == 4) {
if (req.status == 200) {
parseMessages();
} else {
alert ( "Not able to retrieve description" );
}
}
}

readyState = 4 的情況下文檔被加載。

readyState Status Codes:
0 = uninitialized
1 = loading
2 = loaded
3 = interactive
4 = complete

最后,我們解析XML文檔并顯示介紹。

問題: 唯一的問題就是我遭遇到的 "&" 字符。 "&" 在XML文檔里面不是一個有效字符。所以我需要將他轉(zhuǎn)換成 "&"。

function parseMessages() {
response  = req.responseXML.documentElement;
itemDescription = response.getElementsByTagName(‘description‘)[0].firstChild.data;
alert(itemDescription);
}

下面是所有的代碼:

HTML Code:
<a href="/" onmouseover="getDescription(3,1)">Java & J2EE News<a>
JavaScript Code:
function getDescription(channelId,itemId) {
var url = ‘http://localhost:8080/getDescription.jsp?channelId=‘ + channelId + ‘&itemId=‘ + itemId;
if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
} else if (window.ActiveXObject) {
req = new ActiveXObject("Microsoft.XMLHTTP");
}
req.onreadystatechange = processRequest;
req.open("GET", url, true);
req.send(null);
}
function processRequest() {
if (req.readyState == 4) {
if (req.status == 200) {
parseMessages();
} else {
alert ( "Not able to retrieve description" );
}
}
}
function parseMessages() {
response  = req.responseXML.documentElement;
itemDescription = response.getElementsByTagName(‘description‘)[0].firstChild.data;
alert ( itemDescription );
}
JSP Code:
<%
String channelId = request.getParameter("channelId");
String itemId = request.getParameter("itemId");
description = "This is the description for the channelId: " + channelId + "and itemId: " + itemId;
if (description != null) {
response.setContentType("text/xml");
response.setHeader("Cache-Control", "no-cache");
response.getWriter().write("" + description.toString() + "");
} else {
//nothing to show
response.setStatus(HttpServletResponse.SC_NO_CONTENT);
}
%>

資源:
AJAX Java BluePrints Solutions Catalog
AJAX in Wikipedia
W3C HTTP Status Codes

使用AJAX的Google站點(diǎn):
Gmail
Google Suggest
Google Maps

關(guān)于作者: Jay 具有10年以上的IT工作經(jīng)驗(yàn),并且自從Java & J2EE誕生那天起就開始接觸他們了。

 
 
 

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多