views:

395

answers:

1

In JSP page I have written:

var sel = document.getElementById("Wimax");
var ip = sel.options[sel.selectedIndex].value;
var param;
var url = 'ConfigurationServlet?ActionID=Configuration_Physical_Get';
httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
httpRequest.open("POST", url, true);
httpRequest.onreadystatechange = handler(){
if (httpRequest.readyState == 4) {
if (httpRequest.status == 200) {
param = 'ip='+ip;
param += 'mmv='+mmv;
param += "tab="+tab;
}};
httpRequest.send(param);

I want this param variable in my ConfigurationServlet. Can anyone tell me how to get this json object in servlet?

Update: I changed my statements and now it is showing status code as 200.

var index = document.getElementById("Wimax").selectedIndex;
var ip = document.getElementById("Wimax").options[index].text;
httpReq = GetXmlHttpObject();
alert(httpReq);
var param = "ip=" + ip; 
param += "&mmv=" + mmv; 
param += "&tab=" + tab; 
alert("param "+param);
var url="http://localhost:8080/WiMaxNM/ConfigurationServlet?ActionID=Configuration_Physical_Get";
url = url+"?"+param;
httpReq.open("GET",url,true);
alert("httpReq "+httpReq);
httpReq.onreadystatechange = handler;
httpReq.send(null);

But new problem has occured. Control is not at all going to the servlet action ID as specified in url. Please tell me what is wrong here.

A: 

The code in the handler will only be invoked AFTER the request is been sent. You need to populate param before this. You would also need to concatentate separate parameters by &.

Thus, e.g.

// ...
httpRequest.onreadystatechange = handler() {
    // Write code here which should be executed when the request state has changed.
    if (httpRequest.readyState == 4) {
        // Write code here which should be executed when the request is completed.
        if (httpRequest.status == 200) {
            // Write code here which should be executed when the request is succesful.
        }
    }
};

param = 'ip=' + ip;
param += '&mmv=' + mmv;
param += "&tab=" + tab;
httpRequest.send(param);

Then you can access them in the servlet the usual HttpServletRequest#getParameter() way.


That said, the Ajax code you posted there will only work in Microsoft Internet Explorer, not in all the four other major webbrowsers the world is aware of. In other words, your Javascript code won't work for about half of the people in the world.

I suggest to have a look at jQuery to lessen all the verbose work and bridge the crossbrowser compatibility pains. All your code could be easily replaced by

var params = {
    ip: $("Wimax").val();
    mmv: mmv,
    tab: tab
};
$.post('ConfigurationServlet?ActionID=Configuration_Physical_Get', params);

And still work in all webbrowsers!

Update: as per your update, the final URL is plain wrong. The ? denotes a start of the query string. You already have one in your URL. You should use & to chain parameters in the query string. I.e.

url = url + "&" + param;
BalusC
for using HttpServletRequest#getParameter(), i have to give hidden id to param and then document.form id.submit. Instead of this cant i get it through the object of ActiveXObject()??
divi
I don't get you. Please get it all straight for yourself. At w3schools.com are good tutorials to learn the Ajax trivials and at Coreservlets.com are good tutorials to learn the Servlet trivials.
BalusC
Alright BalusC.but one more problem i have. My application is showing status code = 500(internal server error). Can u suggest me something for this?
divi
An exception was thrown in the server side. Read the server logs.
BalusC
Thanks BalusC for telling this. But again i am getting internal server error. And no server log has been created.
divi
Either the logging is improperly configured or you looked at the wrong place for the logs. Run a debugger.
BalusC