Servlet and javascript

Hi !!

i have a problem with my java web application in netbeans. Im using Ajax to parse attributes to a servlet from my jsp page and to get back data in XML format to the javascript . the XML attributes that i want to get back is actually a message that confirms the succesfull insertion of the form data in my data base.the message is used to popup an alarm box in my javascript and to inform the user of the succesfull or not succesfull insertion . The problem is that im getting the wrong message despite the fact that the insertion was being succesfull!!! the strange thing is that when im running the code in debug mode through firebug everything work great and the right message appears!! i was considering the fact that the problem is happening because the readyState is not changing the state from 1 to 4 very quickly to let the servlet to be execute first , but unfortunately im wrong !

is there anybody that can help with this ?(:

This is a wild guess but your JavaScript file maybe “cached” in your browser. So, when you enabled firebug it downloaded the JS from your server. Another theory is that you have a race condition in your AJAX call. Hopefully you’re using jQuery, prototype or other AJAX framework to do this.

mmm , maybe you’ re right ! it is really tricky though…i also tried setTimeout function " setTimeout (call(), 10 );"before if (readyState == 4) but nothing comes up…same again !

thank you for your reply…

you should really consider to use prototype

http://www.prototypejs.org/

or even jQuery. No one codes straight up AJAX anymore… of course, you can for learning purpose.

this is my code :

Messages.js




    var name;
    var Surname;
    var FatherName;
    var Address;
    var TelephoneNo;
    var IdentNo;
    var InDOY ;
    var AFM ;
    var Occupation;
    var BankAccount;
    var Bank;
    var Element ;
    var Categories;
    var messageValue;
    var xmlhttp;
    var interval;

function InsertPayeeMessages()
{
       name          =  document.getElementById("InName");
       Surname       =  document.getElementById("InSurname");
       Occupation    =  document.getElementById("InOccupation");
       FatherName    =  document.getElementById("InFatherName");
       Address       =  document.getElementById("InAddress");
       TelephoneNo   =  document.getElementById("InTelephoneNo");
       IdentNo       =  document.getElementById("InIdent_No");
       InDOY         =  document.getElementById("InDOY");
       AFM           =  document.getElementById("InAFM");
       BankAccount   =  document.getElementById("InBank_Account");
       Bank          =  document.getElementById("InBank");
       Categories    =  document.getElementById("InCateg_id");
       Element       =  document.getElementById("unconfirmed");

if(name.valueOf().value == "" || Categories.valueOf().value == "" || AFM.valueOf().value == "" ||  InDOY.valueOf().value == "" || IdentNo.valueOf().value == "" || FatherName.valueOf().value == ""  || Surname.valueOf().value == "" || Address.valueOf().value == "" || TelephoneNo.valueOf().value == "")
{
        alert("Plese fill the empty fields !");
}
        
if(name.valueOf().value != "" && AFM.valueOf().value != "" && InDOY.valueOf().value != "" && IdentNo.valueOf().value != "" && FatherName.valueOf().value != "" &&  Surname.valueOf().value != "" && Address.valueOf().value != "" && TelephoneNo.valueOf().value != "")
{

if(confirm("The Employer " + "'" + encodeURI(name.value) + "  " + encodeURI(Surname.value) + "'" + " is about to be iserted to the data base.\
are you sure you want to continue ?  "))

{
        Element = document.getElementById("confirmed");
}
else
{
        Element = document.getElementById("unconfirmed");
}

 SQLreturn();
  
 if (Element.valueOf().value != "unconfirmed"  )
 {

 if (messageValue != "entered")
    {
     alert("The Employer  " + "'" + encodeURI(name.value) + "  " + encodeURI(Surname.value) + "'" + "  exists ." );
    }
   else
    alert(" Employer " + "'" + encodeURI(name.value) + "  " + encodeURI(Surname.value) + "'" + " inserted to data base  ! ");
    }
   
}
}

function RequestInit() {
    if (window.XMLHttpRequest) {
        if (navigator.userAgent.indexOf('MSIE') != -1) {
            isIE = true;
        }
        return new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        isIE = true;
        return new ActiveXObject("Microsoft.XMLHTTP");
    }
}

function SQLreturn() {

   var req_url = "InsertElements?PName=" + encodeURI(name.value) + "&PSurname=" + encodeURI(Surname.value) + "&PFatherName=" + encodeURI(FatherName.value) + "&POccupation=" + encodeURI(Occupation.value) + "&PAddress=" + encodeURI(Address.value ) + "&PTelephoneNo=" + encodeURI(TelephoneNo.value) + "&PIdent_No=" + encodeURI(IdentNo.value) + "&PBank_Account=" + encodeURI(BankAccount.value )+ "&PBank=" + encodeURI(Bank.value ) + "&PDOY=" + encodeURI(InDOY.value)+ "&CategID=" + encodeURI(Categories.value.toString()) + "&PAFM=" + encodeURI(AFM.value)  + "&confirmation=" + encodeURI(Element.value) ;
   xmlhttp = RequestInit();
   xmlhttp.open("GET",req_url,true);
   xmlhttp.onreadystatechange = call;
   xmlhttp.send(null);
}


function call() {



if (xmlhttp.readyState == 4) {
       if(xmlhttp.status == 200)
       {          
          parse(xmlhttp.responseXML);         
       }
    }
  alert(null); <------when i put an alert box it shows me the right message !  but after i click 4 times ok !! :) it drives me crazy !!!
}

function parse(responseXML)
{

    if (responseXML != null)
    {
        var sql = responseXML.getElementsByTagName("SQLresult")[0];
        var message = sql.childNodes[0];
        messageValue = message.getElementsByTagName("result")[0].childNodes[0].nodeValue;
     }
}




a part of the servlet InsertElements.java :


 response.setCharacterEncoding("UTF-8");     
         SQLcommand = J850_jdbc.insertPayee(AFM, Ident_No, Name, Surname, FatherName, Occupation, Address, TelephoneNo, DOY, Bank_Account, Bank, CategDescr);  
         sb.append("<messages>");  
       sb.append("<result>" +  SQLcommand  + "</result>");  
         sb.append("</messages>");  
         response.setContentType("text/xml");  
         response.setHeader("Cache-Control", "no-cache");  
         response.getWriter().write("<SQLresult>" + sb.toString() + "</SQLresult>");  

function SQLreturn() {

   var req_url = "InsertElements?PName=" + encodeURI(name.value) + "&PSurname=" + encodeURI(Surname.value) + "&PFatherName=" + encodeURI(FatherName.value) + "&POccupation=" + encodeURI(Occupation.value) + "&PAddress=" + encodeURI(Address.value ) + "&PTelephoneNo=" + encodeURI(TelephoneNo.value) + "&PIdent_No=" + encodeURI(IdentNo.value) + "&PBank_Account=" + encodeURI(BankAccount.value )+ "&PBank=" + encodeURI(Bank.value ) + "&PDOY=" + encodeURI(InDOY.value)+ "&CategID=" + encodeURI(Categories.value.toString()) + "&PAFM=" + encodeURI(AFM.value)  + "&confirmation=" + encodeURI(Element.value) ;
   [B]var[/B] xmlhttp = RequestInit();
   xmlhttp.open("GET",req_url,true);
   xmlhttp.onreadystatechange = call;
   xmlhttp.send(null);
}



Try that, I’m not 100% sure it’ll solve your problem but I can certainly say your code can not handle multiple ajax call at one time. Again… I STRONGLY suggest jquery!

ok now it works!!!

i used prototype.js as you ve told me to , so now it works!!!:slight_smile:
here is the code i used inside Messages.js



 var req_url = "InsertPayeeElements";
  var pars = "PName=" + encodeURI(name.value) + "&PSurname=" + encodeURI(Surname.value) + "&PFatherName=" + encodeURI(FatherName.value) + "&POccupation=" + encodeURI(Occupation.value) + "&PAddress=" + encodeURI(Address.value ) + "&PTelephoneNo=" + encodeURI(TelephoneNo.value) + "&PIdent_No=" + encodeURI(IdentNo.value) + "&PBank_Account=" + encodeURI(BankAccount.value )+ "&PBank=" + encodeURI(Bank.value ) + "&PDOY=" + encodeURI(InDOY.value)+ "&CategID=" + encodeURI(Categories.value.toString()) + "&PAFM=" + encodeURI(AFM.value)  + "&confirmation=" + encodeURI(Element.value) ;
  var ajaxRequest = new Ajax.Request(req_url, {
    method:       'get',
    parameters:   pars,
    asynchronous: false,
    onComplete:   call
});



thanks a lot !!! you have been very useful !!!

many regards !!

Sometimes, the grammar of SQL will take a lot of time. I think this is a kind of SQL grammar. I once spent a lot of time on it.