Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: JSP freezes after AJAX call !!

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default JSP freezes after AJAX call !!

    Hi,

    After clicking the "Submit" button the Action class does the processing and calls the DAO to persist data in the DB.
    An Ajax funtion is called which checks if the data submitted has changed status. The code is attached below

    function postProcessStateChange(){
     
    if("SUC"==document.forms[0].processingStatus.value)
    {		
     
         var innerHtml = document.getElementById("processMessage").innerHTML;
         innerHtml = "<bean:message key="common.maintenanceMsg2"/>&nbsp;&nbsp;
                            <bean:message key="trade.maintenance.tmsTradeId"/>&nbsp;:&nbsp; " + document.forms[0].tmsTradeId.value;
     
         document.getElementById("processMessage").innerHTML = innerHtml;
    }
     
    if("EXC"==document.forms[0].processingStatus.value)				
      document.getElementById("processMessage").innerHTML = "<bean:message key="common.maintenanceMsg3"/>" ;
    if("ERR"==document.forms[0].processingStatus.value)				
      document.getElementById("processMessage").innerHTML = "<bean:message key="common.maintenanceMsg4"/>" ;
     
    if("STA"==document.forms[0].processingStatus.value || "PUB"==document.forms[0].processingStatus.value)
    {
           if(statusTimeOut <= 60000){
           document.getElementById("processMessage").innerHTML = "<img src='../images/refresh_active.gif'>&nbsp;
                                                                                                   <bean:message key="common.maintenanceMsg1"/>" ;
           if(vTradeType == 2) //TRANSRPT
           	self.setTimeout('getRequestStatusTransrpt()', 5000);
           if(vTradeType == 1) //INTL
           	self.setTimeout('getRequestStatusFiss()', 5000);
           if(vTradeType == 0) //DOMESTIC
           					self.setTimeout('getRequestStatus()', 5000); 			
    			}
    			else
    				document.getElementById("processMessage").innerHTML = "<bean:message key="common.maintenanceMsg4"/>" ;
    		}	
    	}  	
     
     
    function getRequestStatus(){
    	alert("entered getRequestStatus");
    			statusTimeOut = statusTimeOut + 5000;
    			var url = "<html:rewrite action='/trademaintenance' />?methodName=getRequestStatus";
    			retrieveURL(url, "","span");
    			alert("exiting getRequestStatus");
    	}
     
    	function getRequestStatusFiss(){
    		statusTimeOut = statusTimeOut + 5000;
    		var url = "<html:rewrite action='/trademaintenanceINTL' />?methodName=getRequestStatus";
    		retrieveURL(url, "","span");
    }
     
    	function getRequestStatusTransrpt(){
    		statusTimeOut = statusTimeOut + 5000;
    		var url = "<html:rewrite action='/trademaintenanceTRANSRPT' />?methodName=getRequestStatus";
    		retrieveURL(url, "","span");
    }


    The ajax code is

     
    function retrieveURL(url,nameOfFormToPost,ps) {
     
        parser = ps;
        //get the (form based) params to push up as part of the get request
        if(nameOfFormToPost != "")
        	url=url+getFormAsString(nameOfFormToPost);
     
        //Do the Ajax call
        if (window.XMLHttpRequest) { // Non-IE browsers
          req = new XMLHttpRequest();
          req.onreadystatechange = processStateChange;
          try {
          	req.open("POST", url, true); //was get
          } catch (e) {
            alert("Problem Communicating with Server\n"+e);
          }
          req.send(null);
        } else if (window.ActiveXObject) { // IE
          //alert('Before Call');
          req = new ActiveXObject("Microsoft.XMLHTTP");
          if (req) {
     
            req.onreadystatechange = processStateChange;
            req.open("POST", url, true);
            req.send();
          }
        }
      }
     
     
    /*
       * Set as the callback method for when XmlHttpRequest State Changes 
       * used by retrieveUrl
      */
      function processStateChange() {
     
      	  if (req.readyState == 4) { // Complete
          if (req.status == 200) { // OK response
     
            //alert("Ajax response:"+req.responseText);
     
            //Split the text response into Span elements
            spanElements = splitTextIntoSpan(req.responseText);
     
            //Use these span elements to update the page
            replaceExistingWithNewHtml(spanElements);
     
            //Implement this method in your local jsp for local or page specific changes after a ajax call
            postProcessStateChange()
     
          } else {
            alert("Problem with server response:\n " + req.statusText);
          }
        }
        alert("****exiting processStateChange");
      }
     
     
     function splitTextIntoSpan(textToSplit){
     
      	//Split the document
     	returnElements=textToSplit.split("</"+parser+">")
     
     	//Process each of the elements 	
     	for ( var i=returnElements.length-1; i>=0; --i ){
     
     		//Remove everything before the 1st span
     		spanPos = returnElements[i].indexOf("<"+parser);		
     
     		//if we find a match , take out everything before the span
     		if(spanPos>0){
     			subString=returnElements[i].substring(spanPos);
     			returnElements[i]=subString;
     
     		} 
     	}
     
     	return returnElements;
     }
     
    function replaceExistingWithNewHtml(newTextElements){
     
     	alert("new text elements"+newTextElements);
     	//loop through newTextElements
     	for ( var i=newTextElements.length-1; i>=0; --i ){
     
     		//check that this begins with <span
     		if(newTextElements[i].indexOf("<"+parser)>-1){
     
     			//get the name - between the 1st and 2nd quote mark
     			startNamePos=newTextElements[i].indexOf('"')+1;
     			endNamePos=newTextElements[i].indexOf('"',startNamePos);
     			name=newTextElements[i].substring(startNamePos,endNamePos);
     
     			//get the content - everything after the first > mark
     			startContentPos=newTextElements[i].indexOf('>')+1;
     			content=newTextElements[i].substring(startContentPos);
     
     			//Now update the existing Document with this element
     
    	 			//check that this element exists in the document
    	 			if(document.getElementById(name)){
     
    	 				//alert("Replacing Element:"+name);
    	 				//alert("containing"+document.getElementById(name).innerHTML);
    	 				//alert("with"+content);
    	 				document.getElementById(name).innerHTML = content;
    	 			} else {
    	 				alert("Element:"+name+"not found in existing document");
    	 			}
     		}
     	}
     	alert("leaning replaceExistingWithNewHtml");
     }


    Once the call return with the message being displayed in the jsp. I can't used any button "Submit" or "Reset".

    I have a requirement whrer I need to reset the all fields one the processing
    is completed for one trade.


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: JSP freezes after AJAX call !!

    Hello java_freak,

    So when you click the button once, you are unable to click it again?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

Similar Threads

  1. [SOLVED] Trying to call web service (and failing)
    By jamesruk21 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 15th, 2011, 11:35 AM
  2. need programmer for my online call center
    By erinbasim in forum Paid Java Projects
    Replies: 6
    Last Post: March 30th, 2010, 01:45 AM
  3. Under Windows OS, how to call *.EXE produced in Linux OS?
    By tony_lincoln in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 19th, 2010, 12:51 AM
  4. How to use Ajax concepts in jsp page(for view) with servlet?
    By sundarjothi in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: July 8th, 2009, 06:20 AM
  5. [SOLVED] Java exception "result already defined"
    By rptech in forum Object Oriented Programming
    Replies: 3
    Last Post: May 20th, 2009, 05:48 AM