DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> AJAX入門 >> AJAX詳解 >> Jsp中使用xmlhttp進行數據交互
Jsp中使用xmlhttp進行數據交互
編輯:AJAX詳解     

 1.客戶端提交請求

Domain = " test.com"
sXml = "<?XML version=""1.0"" encoding=""gb2312"" ?>" &_
     "<Package>" &_
     "<Domain>" & Domain & "</Domain>" &_
     "</Package>"
 Set oD = Server.CreateObject("Microsoft.XMLHTTP")
 oD.Open "Post", "http://Server/Domain_Check.JSP", False
 oD.Send(sXML)
 
 If oD.Status = 200 Then
  Result = oD.responseText
  Set o = Server.CreateObject("Microsoft.XMLDOM")
  o.LoadXML(Result)
  
  Domain = CInt(o.selectSingleNode("//Result").text)
  Response.Write Domain
  Set o = Nothing
  Set oD = Nothing
 End If

2.服務器端解析數據包

<%@ page import="Java.util.*" %>
<%@ page import="Java.text.*" %>
<%@ page import="Java.io.*" %>
<%@ page import="org.w3c.dom.*" %>
<%@ page import="Javax.XML.parsers.*" %>
<%@ page contentType="text/Html; charset=gbk" %>
<%
 //獲得數據包
 request.setCharacterEncoding("UTF-8");
 BufferedReader in = request.getReader();
 String line;
 String XMLRequest = "";
 String domain_name = "";
 
 while ((line = in.readLine()) != null)
 {
  //構造數據包
  xmlRequest = XMLRequest + line + "\n";
 }
 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
 DocumentBuilder db = factory.newDocumentBuilder();
 ByteArrayInputStream stream = new ByteArrayInputStream(XMLRequest.getBytes("UTF-8"));
 Document doc = db.parse(stream);
 Element root = doc.getDocumentElement();

 NodeList Domain = root.getElementsByTagName("Domain");
 if (Domain.getLength() == 1) {
  Element e = (Element) Domain.item(0);
  domain_name = (String) e.getFirstChild().getNodeValue();
 }
//返回數據
 out.println("<?XML version=\"1.0\" encoding=\"gb2312\" ?>\n");
 out.println("<Package>\n");
 out.println("\t<Result>" + domain_name + "</Result>\n");
 out.println("</Package>");
%>

3.得到返回結果

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved