首先看下看下相对简单些的--向服务器发送一个包含有名/值对的简单查询串,在这种情况下XHP即可以用GET也可以用POST。
GET
function doRequestUsingGET() { var queryString = " GetAndPostExample? " ; POST function doRequestUsingPOST() { var url = " GetAndPostExample?timeStamp= " + new Date().getTime(); xmlHttp.open( " POST " , url, true ); |
queryString就是名/值对的参数形式了(如name=LiLin&age=23),在调用OPEN方法中,当请求方法是用POST的时候为了确保服务器知道请求体中有请求参数,需要调用setRequestHeader,将Content-Type值设置为application/x-www-form-urlencoded.当然也可不放在请求体中(那就不要用POST啦!)
此时server处理:
import java.io. * ; import java.net. * ; import javax.servlet. * ; import javax.servlet.http. * ; public class GetAndPostExample extends HttpServlet { protected void processRequest(HttpServletRequest request, HttpServletResponse response, String method) // Set content type of the response to text/xml // Get the user's input // Create the response text // Write the response back to the browser // Close the writer protected void doGet(HttpServletRequest request, HttpServletResponse response) protected void doPost(HttpServletRequest request, HttpServletResponse response) |
对get and post方法都用processRequest来处理。
要向服务器发送相关复杂的查询串,可以将模型变化为XML发送到server 。
client端:
function createXML() { var xml = " <pets> " ; var options = document.getElementById( " petTypes " ).childNodes; xml = xml + " <\/pets> " ; function sendPetTypes() { var xml = createXML(); xmlHttp.open( " POST " , url, true ); |
createXML方法无非就是将内容以DOM的样式存到var xml(变量)里。有时也可能出现client直接将本地的一个XML文件直接以DOM(当然可以edit)的样式传送.(也放这个时个的Content-Type应该为text/xml了!)这时可能要用到ActiveXObject("MSXML2.DOMDocument.3.0")这样一个控件了。
关于这个控件有个方法可以在各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 ; return oXML; // -------------------------------------------------------------------- var sProgID = g_sXmlParserProgID; if ( ! sProgID) try if (bFound) 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() { |
实际上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) String xml = readXMLFromRequestBody(request); /**/ /* Note how the Java implementation of the W3C DOM has the same methods response.setContentType( " text/xml " ); private String readXMLFromRequestBody(HttpServletRequest request) { |
DOM,JDOM,JAXP随便你自己选好了!