Magazine
 
Quick Review:Ajax
 
Ajax Examples
 

var strNum = document.getElementById(“numstring”);
strNum.value = req.responseText;
}
}
}
function clear(){
var num = document.getElementById(“num”);
num.value = “”;
}
function focusIn(){
document.getElementById(“num”).focus( );
}
</script>
</head>
<body onload=”focusIn();”>
Enter the Number here: <input type=”text” id=”num” name=”num”
onkeyup=”convertToString();”>
<br><br>
Equivalent String: <input type=”text” size=”20" readonly id=”numstring”>
</body>
</html>

The above html page has input text field of id “num” with “onkeyup” attribute that is set to the JavaScript function “convertToString()”. This method is called each time the user release the key up.

“convertToString ()” first tries to get HTTP request object maintaining the browser compatibility. In this example, this object is stored in the variable named “xmlHttp”. Read how to get HTTP request object on page 12

Next step is to determine which method should be invoked after getting the response from the server. In this example, the method “callback ()” is assigned for this purpose. Now the request for servlet “AJAXResponseServlet” is forwarded to the server along with number as parameter.

AJAXResponseServlet.java

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
public class AJAXResponseServlet extends HttpServlet {
public void doGet( HttpServletRequest request, HttpServletResponse response ) throws
ServletException, IOException {
String number = request.getParameter(“num”);
String strRepr = null;

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