Magazine
 
AJAX: Redefining Web Applications
 

}
}
• On Servlet side, we can construct JSON object using
• StringBuffer [common approach]
• Using JSON library
• Can be downloaded from: http:// www.JSON.org/java/json_simple.zip Code illustrations for Constructing and parsing response data using JSON:

AJAXCharacterDecoderUsingJSON.html

<!DOCTYPE HTML PUBLIC “-//W3C//DTD
HTML 4.01 Transitional//EN”>
<html>
<head>
<title>AJAX Character Decoder</title>
<meta http-equiv=”keywords”
content=”keyword1,keyword2,keyword3">
<meta http-equiv=”description”
content=”this is my page”>
<meta http-equiv=”content-type”
content=”text/html; charset=UTF-8">
<!—<link rel=”stylesheet” type=”text/css”
href=”./styles.css”>—>
<script language=”javascript”>
var request;
function convertToXML( ){
var key =
document.getElementById(“key”);
var keypressed =
document.getElementById(“keypressed”);
keypressed.value = key.value;
var url = “/ajaxapp/
CharacterDecoderUsingJSON?key=” +
escape(key.value);
if(window.XMLHttpRequest){
request = new XMLHttpRequest();
} else{
request = new
ActiveXObject(“Microsoft.XMLHTTP”);
}
request.open(“Get”,url,true);
request.onreadystatechange = callback;
request.send(null);
}

 

function callback( ){
if(request.readyState == 4){
if(request.status == 200){
populateUsingJSON( );
}
}
}
function populateUsingJSON( ){
var jsonData = request.responseText;
var jsonObject = eval(‘(‘+jsonData+’)’);
var binary =
document.getElementById(“binary”);
binary.value =
jsonObject.conversion.binary;
var octal =
document.getElementById(“octal”);
octal.value = jsonObject.conversion.octal;
var decimal =
document.getElementById(“decimal”);
decimal.value =
jsonObject.conversion.decimal;
var hexadecimal =
document.getElementById(“hexadecimal”);
hexadecimal.value =
jsonObject.conversion.hexadecimal;
}
function focusIn( ){
document.getElementById(“key”).focus;
}
</script>
</head>
<body onload=”focusIn( );”>
<h1>AJAX Character Decoder</h1>
<table>
<tr>
<td>
Enter the key here: &nbsp;
<input type=”text” id=”key” name=”key”
maxlength=”2" size=”2"
onkeyup=”convertToXML( );”>
</td>

Nov 2007 | Java Jazz Up | 54
previous
index
next
 
View All Topics
All Pages of this Issue
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   Download PDF