If you want to use GET, you can pass the variables by URL encode it and add them to the url request, like this :
var url = "/path/to/myservlet";
var params = "somevariable=somevalue&anothervariable=anothervalue";
var http = new XMLHttpRequest();
http.open("GET", url+"?"+params, true);
http.onreadystatechange = function()
{
if(http.readyState == 4 && http.status == 200) {
alert(http.responseText);
}
}
http.send(null);
If you have long datas to pass, using POST is the preferred way, here is an example of such code :
var http = new XMLHttpRequest();
var url = "/path/to/myservlet";
var params = "lorem=ipsum&name=binny";
http.open("POST", url, true);
//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.onreadystatechange = function() {//Call a function when the state changes.
if(http.readyState == 4 && http.status == 200) {
alert(http.responseText);
}
}
http.send(params);
You will be able to read those datas in your servlet HttpServletRequest
using the getParameter(String name)
method.
Hope this helps. :-)