Magazine
 
Quick Review:Ajax
 

Ajax Examples

 

responseXML.append(“</converted-values>”);
System.out.println(“\n”+responseXML.toString());
response.setContentType(“text/xml”);
response.setHeader(“Cache-Control”, “no-cache”);
response.getWriter().write(responseXML.toString());
}
else{
responseXML = new StringBuffer(“\r\n<converted-values>”);
responseXML.append(“\r\n<binary>?</binary>”);
responseXML.append(“\r\n<octal>?</octal>”);
responseXML.append(“\r\n<decimal>?</decimal>”);
responseXML.append(“\r\n<hexadecimal>?</hexadecimal>”);
responseXML.append(“</converted-values>”);
System.out.println(“\n”+responseXML.toString());
response.setContentType(“text/xml”);
response.setHeader(“Cache-Control”, “no-cache”);
response.getWriter().write(responseXML.toString());
}
}
}

The server executes the servlet code, which sends the data in xml format. After getting the response, method “handleResponse ()” is called to manipulate the returned data.

if(request.readyState == 4){
if(request.status == 200){
if(window.XMLHttpRequest){
nonMSPopulate();
} else{
msPopulate();
}
}
}

This function first checks if everything is fine. The above code checks if it is Microsoft’s browser and call msPopulate() method otherwise nonMSPopulate() method is called. This is done because Microsoft’s XML parser id different than others.

All browsers have built-in XML parser to read and manipulate XML. It reads XML in memory and converts into XML DOM objects. Now these objects can be accessed with JavaScript.

The following JavaScript fragment loads the response data into the parser:

For Non-Microsoft browsers:

var parser = new DOMParser();
var dom = parser.parseFromString(response,”text/xml”);

For Microsoft browser:

var xmlDoc = new ActiveXObject(“Microsoft.XMLDOM”);

Apr  2008 | Java Jazz Up |34
 
previous
index
next
 
Pages: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,

30
, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53 , 54, 55, 56, 57,

58
, 59, 60, 61, 62, 63 , 64, 65 , 66 , 67 , 68 , 69 , 70, 71, 72, 73, 74, 75, 76, 77, 78,   Download PDF