Magazine
 
Quick Review:Ajax
 

Ajax Examples

else if(userNumber.equals(“2”)){
response.getWriter().write(“<name>Ravi,Kant</name>”);
}
else{
response.getWriter().write(“<name>,</name>”);
}
} else {
//set Status code 204 (SC_NO_CONTENT) indicating that the request succeeded but no new
information to return.
response.setStatus(HttpServletResponse.SC_NO_CONTENT);
}
%>

The server executes the JSP code, which sends first and last name in xml format. First and last names are separated with comma and are enclosed within <name> element. After getting the response, method “updatepage()” is called to manipulate the returned data.

if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200) {
// For XML formatted response
var message = xmlHttp.responseXML.getElementsByTagName(“name”)[0];
results = message.childNodes[0].nodeValue.split(“,”);
document.getElementById(‘firstName’).value = results[0];
document.getElementById(‘lastName’).value = results[1];
}
}

The function first checks if everything is fine. To get the xml response, responseXML property of the xmlHttp object is used. Get the nodes values of xml response and split it with comma. Now splitted values are set to the input components of id “firstName” and “lastName”. First name and last names of the user are updated and so displayed on the page.

3. Checking User Name Availability in database

This example checks the user name availability using Ajax. If user name does not exist then it congratulates the user otherwise it warns the user to choose any other name.

Ajax Example

Apr 2008 | Java Jazz Up | 20
 
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