精彩专题推荐:建站之入门课 建站之必修课 建站之关键课 网站价值所在 流量提高专题 css+div 标准 个人网站打造全过程
返回建站学首页
导航:
建站首页 | 网站设计 | 网站开发 | 网站运营 | 网页软件 | 建站指南 | 搜索优化 | 图像处理 | 视频教程 | 书籍教程 | 建站专题
当前位置:首页>网站开发>ajax教程>正文

AJAX编程实践之与服务器通信


来源: 时间:06-11-28 点击: 点击这里收藏本文


  关于这个控件有个方法可以在各broswer中通用的JS代码:

// --------------------------------------------------------------------
// Function: CreateXMLDOM
//
// Purpose: Creates a new XML DOM.
//
// Parameters: None
//
// Returns: XMLDOM object OR null
// --------------------------------------------------------------------
function CreateXmlDOM()
{
 var oXML = new ActiveXObject(GetXmlParserProgID());
 try
 {
  oXML.setProperty( " AllowXsltScript " , true );
 }
 catch (err) {}

 oXML.async = false ;
 oXML.validateOnParse = false ;
 oXML.resolveExternals = false ;
 oXML.setProperty( " SelectionLanguage " , " XPath " );
 try {oXML.setProperty( " NewParser " , true );} catch (e) {}

 return oXML;
}

// --------------------------------------------------------------------
// Function: GetXmlParserProgID
//
// Purpose:
// Gets the ProgID of the highest available version of the
// Microsoft XML parser.
//
// Parameters: None
//
// Returns: String (i.e. "Msxml2.DOMDocument.4.0")
//
// --------------------------------------------------------------------
function GetXmlParserProgID()
{
 var MAX_MAJOR_PARSER_VERSION = 10 ;
 var MIN_MAJOR_PARSER_VERSION = 0 ;
 var MAX_MINOR_PARSER_VERSION = 9 ;
 var MIN_MINOR_PARSER_VERSION = 0 ;

 var sProgID = g_sXmlParserProgID;
 var bFound = false ;

 if ( ! sProgID)
 {
  // Iterate through possible versions
  for ( var nMajor = MAX_MAJOR_PARSER_VERSION; nMajor >= MIN_MAJOR_PARSER_VERSION; nMajor -- )
  {
   for ( var nMinor = MAX_MINOR_PARSER_VERSION; nMinor >= MIN_MINOR_PARSER_VERSION; nMinor -- )
   {
    // Set up the classname for the version that we're trying to instantiate
    sProgID = " Msxml2.DOMDocument. " + nMajor + " . " + nMinor;

    try
    {
     if ( new ActiveXObject(sProgID))
     {
      bFound = true ;
      break ;
     }
    }
    catch (e)
    {}
   }

   if (bFound)
   {
    // store in a global variable to speedup subsequent calls
    g_sXmlParserProgID = sProgID;
    break ;
   }
  }
 }

 return sProgID;
}

  然后直接用其load方法(本地)。

var xmlDoc = new ActiveXObject( " MSXML2.DOMDocument.3.0 " );
xmlDoc.load(local_XML_FileName);

  当然也可以直接从server取来(用get方法即可),然后以responseText的方法

xmlht.Open( " GET " ,server_XML_FileName, true );
xmlht.onreadystatechange = stateChange;
xmlht.Send( null );

function handleStateChange() {
 if (xmlHttp.readyState == 4 ) {
  if (xmlHttp.status == 200 ) {
   xmlDoc.loadXML(xmlht.responseText);
  }
 }
}

  实际上xmlDoc.loadXML(xmlht.responseText)所得到的就是一个于内存中的DOM了,而直接用responseXML的话就直接可以解析为一个DOM了!(注意load(FILE)与loadXML(DOM)是不同的)

  此时servert process :

import java.io. * ;
import javax.servlet. * ;
import javax.servlet.http. * ;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class PostingXMLExample extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

String xml = readXMLFromRequestBody(request);
Document xmlDoc = null ;
try {
xmlDoc =
DocumentBuilderFactory.newInstance().newDocumentBuilder()
.parse( new ByteArrayInputStream(xml.getBytes()));
}
catch (ParserConfigurationException e) {
System.out.println( " ParserConfigurationException: " + e);
}
catch (SAXException e) {
System.out.println( " SAXException: " + e);
}

/**/ /* Note how the Java implementation of the W3C DOM has the same methods
* as the JavaScript implementation, such as getElementsByTagName and
* getNodeValue.
*/
NodeList selectedPetTypes = xmlDoc.getElementsByTagName( " type " );
String type = null ;
String responseText = " Selected Pets: " ;
for ( int i = 0 ; i < selectedPetTypes.getLength(); i ++ ) {
type = selectedPetTypes.item(i).getFirstChild().getNodeValue();
responseText = responseText + " " + type;
}

response.setContentType( " text/xml " );
response.getWriter().print(responseText);
}

private String readXMLFromRequestBody(HttpServletRequest request) {
StringBuffer xml = new StringBuffer();
String line = null ;
try {
BufferedReader reader = request.getReader();
while ((line = reader.readLine()) != null ) {
xml.append(line);
}
}
catch (Exception e) {
System.out.println( " Error reading XML: " + e.toString());
}
return xml.toString();
}
}

  DOM,JDOM,JAXP随便你自己选好了!
9 7 3 1 2 4 8 :

  把此文章收藏到:          
广而告之
文章搜索
  • Google JZxue.Com

关于我们 | 联系我们 | 友情链接 | 网站地图
Copyright © 2005 - 2006 建站学 All rights reserved.