J2EE Part 22

1. 課本 JSP 2.0 的第18章 -- AJAX
a.課本 p.18-3頁, XmlHttpRequest 物件是由瀏覽器所提供,不是 Java 提供,所以不同瀏覽器,可能要用不同的語法,來產生相同的結果。在不同瀏覽器使用相同的語法,也有可能

課本範例程式:ch18/ex18-1/sendHttp.htm
<HTML>
<Script language="JavaScript">
function sendHttp()
{
  try{
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
//只有 IE 可以用
  }catch ( e ){
xmlHttp = new XMLHttpRequest();
//其他瀏覽器用這個
  }
//alert(xmlHttp);
//STEP 1 建立HttpRequest物件

//alert(url.value);
xmlHttp.open("GET", url.value, false);
//STEP 2 開啟HTTP連結
xmlHttp.send() ;
//STEP 3 送出HttpRequest物件的請求
//STEP 4、STEP 5 執行於伺服端的ASP網頁
//STEP 6 利用XMLHttp物件的屬性與方法,取得伺服端回應的資料
//statuscode.innerText = xmlHttp.status;

if (!document.getElementById('statuscode').hasChildNodes()) {
  document.getElementById('statuscode').appendChild(document.createTextNode('.'));
}
document.getElementById('statuscode').firstChild.nodeValue = xmlHttp.status;
//取得回應狀態
//statustext.innerText = xmlHttp.statusText;
if (!document.getElementById('statustext').hasChildNodes()) {
  document.getElementById('statustext').appendChild(document.createTextNode('.'));
}
document.getElementById('statustext').firstChild.nodeValue = xmlHttp.statusText;
//取得回應訊息
//repText.innerText = xmlHttp.responseText;
if (!document.getElementById('repText').hasChildNodes()) {
  document.getElementById('repText').appendChild(document.createTextNode('.'));
}
document.getElementById('repText').firstChild.nodeValue = xmlHttp.responseText;
//取得回應的內容
}
</Script>
<BODY>
<Center>
<H1><Font color="blue">HTTP連結的建立與回應訊息的取得</Font></H1><HR>
請輸入測試位址 :
<Input type=text value="responseHttp.jsp" id=url>
<Input type=button onclick="sendHttp()" value="取得回應"><P></P>

<Table bgcolor="DodgerBlue" width=90%>
<tr>
<td bgcolor="SkyBlue" width=15%><B>狀態編碼</B></td>
<td bgcolor="Wheat" width=85%><Span id=statuscode></Span></td>
</tr>
<tr>
<td bgcolor="SkyBlue" width=15%><B>狀態訊息</B></td>
<td bgcolor="Wheat" width=85%><Span id=statustext></Span></td>
</tr>
<tr>
<td bgcolor="SkyBlue"><B>回應內容</B></td>
<td bgcolor="Wheat"><SPAN id=repText></SPAN></td>
</tr>

</Table>
</Center>
</BODY>
</HTML>
responseHttp.jsp
<%
out.print(request.getHeaderNames());
%>

2.插播 web 應用程式 scope
web 應用程式 scope:有效範圍
application:網頁伺服器關閉
session:該次連線(瀏覽器關閉)
page:該網頁有效,下一個網頁無效(要用 get, post 將參數傳到下一個網頁)
request:該需求有效




No comments: