Introduction To AJAX With JSP by J. M. V. Swamy Naidu - M.C.A.
Introduction To AJAX With JSP by J. M. V. Swamy Naidu - M.C.A.
Introduction to
AJAX
Using AJAX in combination with JSP
J. M. V. Swamy Naidu, M.C.A.
[2009
]
[[email protected]]
Introduction to AJAX 2009
HTML / XHTML
JavaScript
JavaScript
XML [Extensible Markup Language]
HTML [Hyper Text Markup Language]
CSS [Cascading Style Sheet]
Because the server returns a new page each time the user
submits input, traditional web applications can run slowly
and tend to be less user-friendly.
she will not notice that scripts request pages, or send data
to a server in the background.
<script type=”text/javascript”>
function createObject() {
request_URL =
“http://localhost:8080/AJAXResponseJSP.jsp?”;
var temp = “”;
if( window.XMLHttpRequest ) {
temp = new XMLHttpRequest();
}
if( window.ActiveXObject ) {
temp = new
ActiveXObject(Microsoft.XMLHTTP);
}
return temp;
}
</script>
open():
This is the method that actually establishes
the communication with the server. It takes three
parameters, they are:
1. Type of Method: It either GET or POST.
2. Request handler’s URL: The URL for the
request handler on the server’s side that
generates the response.
3. Mode of transmission: It’s a Boolean value
that indicates the Asynchronous
transmission true or false. By default it is
true.
function makeAndSendRequest() {
request_Object = createObject();
request_Object.open(“GET”, request_URL,
true);
request_Object.onreadystatechange =
responseHandler;
request_Object.send(null);
}
Here,
In the first line of the code it’s a call to the
method createObject(), that we’ve already
created, that returns the XMLHttpRequest object.
In the second line, it’s a call to the AJAX
method open using GET method.
In the third line, it’s the assignment of the
value that is the name of the method [don’t use
any parenthesis here] used whenever the ready
So here we need the value for the status is 200 and for
the readyState are 4. Now it’s time to write code.
function responseHandler() {
if( request_Object.readyState == 4 ) {
if( request_object.status == 200 ) {
displayResponse(request_Object.responseText);
}
}
}
4. Display response:
function displayResponse(val) {
document.getElementById(“divX”).innerHTML
= “<b>” + val + “</b>”;
}
request_Object.onreadystatechange =
responseHandler;
request_Object.send(null);
}
function responseHandler() {
if( request_Object.readyState == 4 ) {
if( request_Object.status == 200 ) {
displayResponse(request_Object.responseText);
}
}
}
function displayResponse(val) {
//Here divX is the id of the div tag inside the
//AJAXRequestHTML.htm file, we set the value for that.
document.getElementById(“divX”).innerHTML = “<b>”
+ val + “</b>”;
}
</script>
</head>
<body bgcolor=”#ffffff” text=”#ff00ff”>
<form action=”” method=””>
<table align=”center”>
<tr>
<td>Enter your name:</td>
<td><input type=”text”
name=”myName” /></td>
</tr>
<tr><td colspan=”2”><input type=”button”
value=”Send”
onclick=”makeAndSendRequest(myName.value)” /></td>
</tr>
</table>
</form>
<div id=”divX” align=”center”><div>
</body>
</html>
Notice: If you copy and paste the content from here, there
may be a problem with the double quotes that makes coding
error. Please check them or retype them in the corresponding
html and jsp pages.
Regards
Naidu, MCA
For any queries or suggestions on this article
contact me @
[email protected]
http://www.NaiduMCA.co.cc