Ajax is a very famous technology for devolop higly interactive web sites. Here I would likes to describe how to do a ajax call to a servelt.
First of all you going to need the following java script for call to the servelt.
function callServer(callback,urlparas) {
httpRequest = false;
if (window.XMLHttpRequest) {
httpRequest = new XMLHttpRequest();
} else if (window.ActiveXObject) {
try {
httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch(e) {
try{
httpRequest = new ActiveXObject("Microsoft.XMLHTTP")
} catch (e){
alert("fetchData, e: " + e);
}
}
} else {
return false;
}
httpRequest.onreadystatechange = callback;
httpRequest.open("POST", "RequestHandler", true);
httpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
httpRequest.send(urlparas);
}
Now lets see wht each above function really does.
if (window.XMLHttpRequest) {
httpRequest = new XMLHttpRequest();
} else if (window.ActiveXObject) {
try {
httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch(e) {
try{
httpRequest = new ActiveXObject("Microsoft.XMLHTTP")
} catch (e){
alert("fetchData, e: " + e);
}
}
} else {
return false;
}
above mentioned script part is for creating correct httpRequest object for the ajax call. As you can see if browser supports "window.XMLHttpRequest" it creates a "XMLHttpRequest" type of a httpRequest and so on.. If browser does not support httpRequest simply this function will return false. That means you cannot make ajax calls.
httpRequest.onreadystatechange = callback;
For every ajax call, you have to specify a function which should get executed after web page receive a response from the server. As a example callback function may fill a table with the received data from the server. Here we passing the callback function a parameter.
httpRequest.open("POST", "RequestHandler", true);
Line above describes, which servlet we going to call and the method of calling. Here in this example servlet name is "RequestHandler" and the calling method is "POST". In java web application you should specify all servelts in a file called web.xml. Here you have to provide the exact servelt name you provide in the web.xml file.
httpRequest.send(urlparas);
In most times you wants to make a Ajax call with appended paras to URL. As a example URL "RequestHandler?action=login&loginid=mars&pwd=46". So for make a Ajax call like I mentioned, you have to pass urlparas as "action=login&loginid=mars&pwd=46" to this function.
Hmmmm ! That is all about the function we used to make a Ajax call..
-------------------------------------------------------------------------------------
Here as a example I would like to specify a simple example about how to use above function in a web appliacation. For tht I'm using following servelt. Here I have assumed you have a some knoledge of web devolopment in java.
public class RequestHandler extends HttpServlet {}
Lets think we passing the parameter called "action=sayhello" to the servelt and our callback javascript function is as follows.
function printHello() {}
First lets see how to call above mentoed callServer() function from an web page. Please include the callServer() function in same the same page or write that function in a separate js file and import it in to your web page. Here in the following code, I assumed you have done either one of above ways to specify the callServer() function.
callServer(printHello,"action=sayhello"); // calling the server
// callback function
function printHello() {
if (httpRequest.readyState == 4) {
if (httpRequest.status == 200) {
alert(httpRequest.responseText);
}
}
}
function printHello() {
if (httpRequest.readyState == 4) {
if (httpRequest.status == 200) {
var response = httpRequest.responseText;
alert(response);
}
}
}
In above printHello() function, we have checked readyState and status of the httpRequest.responseText object to make sure, we recived a succesfull response from the server. So it is required to check those things. Then we just alerting the response from the server to the user. In this case, it should alert as "Hello from the server"
Now lets concider about the servlet. The servelt should read the passed parameter by ajax call and respond to it. The code for doing it as follows.
public class RequestHandler extends HttpServlet {
response.setContentType("text/xml;charset=UTF-8");
String strAction = request.getParameter("action");
final ServletOutputStream writer = response.getOutputStream();
if (strAction.equals("sayhello") {
writer.print("Hello from the server") ;
}
}
Now lets see what above code doing. First we setting the response conetent type. Which here it can be either plain text or xml formatted and the charset is utf-8.
And in next line, we readin the para,eters in the url and then servelt acting acording to the parameter value in the if condition.