// Write the response back to the browser PrintWriter out = response.getWriter(); out.println(responseText);
// Close the writer out.close(); }
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Process the request in method processRequest processRequest(request, response, " GET " ); }
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Process the request in method processRequest processRequest(request, response, " POST " ); } } 对get and post方法都用processRequest来处理。
要向服务器发送相关复杂的查询串,可以将模型变化为XML发送到server 。
client端:
function createXML() { var xml = " <pets> " ;
var options = document.getElementById( " petTypes " ).childNodes; var option = null ; for ( var i = 0 ; i < options.length; i ++ ) { option = options[i]; if (option.selected) { xml = xml + " <type> " + option.value + " <\/type> " ; } }
xml = xml + " <\/pets> " ; return xml; }
function sendPetTypes() { createXMLHttpRequest();
var xml = createXML(); var url = " PostingXMLExample?timeStamp= " + new Date().getTime();
// -------------------------------------------------------------------- // 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的方法
/**/ /* 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; }