aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes AJAX part in jsp is not functioning with IE 6.0 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "AJAX part in jsp is not functioning with IE 6.0" Watch "AJAX part in jsp is not functioning with IE 6.0" New topic
Author

AJAX part in jsp is not functioning with IE 6.0

Bunty Paul
Greenhorn

Joined: Jun 26, 2006
Posts: 28
Hi All,

I have designed a jsp page and have written a javascript code using ajax so that the data submitted to the servlet is sent through this script code.

The jsp page is responsible to display records. For example,
when the page is loaded it displays about 10 records. When i click the next button it does not communicate with the servlet to give me the next 10 records.

The problem is when i tried to test with IE 6 the data gets retreived only once and for the next retreival it does not communicate with the respective servlet at all. With Firefox the same code works fine.

This is the code...

/**
* This function is used to perform ajax call
*
* url - url for request
* formNameOrIndex - either formName or formIndex
* namedTag - name of the tag is to be updated
*/

function retrieveURL(url,formNameOrIndex,namedTag) {

tagName = namedTag;
//get the (form based) params to push up as part of the get request
url=url+getFormAsString(formNameOrIndex);

//Do the Ajax call
if (window.XMLHttpRequest) { // Non-IE browsers
req = new XMLHttpRequest();
req.onreadystatechange = processStateChange;
try {
req.open("GET", url, true);
} catch (e) {
alert("Problem Communicating with Server\n"+e);
}
req.send(null);//if method=get
} else if (window.ActiveXObject) { // IE
req = new ActiveXObject("Microsoft.XMLHTTP");
req.onreadystatechange = processStateChange;
try {
req.open("GET", url, true);
} catch(e) {
alert("Problem Communicating with Server\n"+e);
}
req.send(null);
}
}


/**
* This function used to get reponse from given url when request
status is successful.
*/
function processStateChange() {

if (req.readyState == 4) { // Complete
if (req.status == 200) { // OK response
splitAndReplace(req.responseText);
} else {
alert("Problem with server response:\n " + req.statusText);
}//end if-else
}//end if
}

/**
* This function used to prepare form-bean
*/
function getFormAsString(formNameOrIndex){
//alert('getFormAsString');

//Setup the return String
returnString ="?";
//Get the form values
formElements=document.forms[formNameOrIndex].elements;
//loop through the array , building up the url
//in the form /strutsaction.do&name=value
for(var i=0;i<formElements.length;i++){
//we escape (encode) each value
if(i==0)
returnString=returnString+escape(formElements[i].name)+"="+escape(formElements[i].value);
else
returnString=returnString+"&"+escape(formElements[i].name)+"="+escape(formElements[i].value);
}
alert(returnString);
return returnString;
}

/**
* This function used to split response text into given tag and replace existing with new HTML tags.
*/
function splitAndReplace(responseText) {

//alert('splitAndReplace');

//split document into tag
splittedElements = responseText.split("</"+tagName+">");

//Process each element
for(var i=0;i < splittedElements.length;i++) {

//check that this begins with <tagName id=
//leave other-tag that having no attribute named id
if((tagPos = splittedElements[i].indexOf("<"+tagName +" id=")) >-1 ){

//remove everything before <tagName id=
if(tagPos > 0) {
subString = splittedElements[i].substring(tagPos);
splittedElements[i] = subString;
}//end if

//get the id of tag
//get the name - between the 1st and 2nd quote mark
startNamePos=splittedElements[i].indexOf('"')+1;
endNamePos=splittedElements[i].indexOf('"',startNamePos);
name=splittedElements[i].substring(startNamePos,endNamePos);

//get the content - everything after the first > mark
startContentPos=splittedElements[i].indexOf('>')+1;
content=splittedElements[i].substring(startContentPos);


//alert("name : " + name);
//alert("content : " + content);

//Now update the existing Document with this element
//check that this element exists in the document
if(document.getElementById(name)){
document.getElementById(name).innerHTML = content;
} else {
alert("Element:"+name+"not found in existing document");
}//end if-else
}//end if

}//end for
}

Thanks,
Paul
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Moving to the HTML/Javascript forum where the AJAX gurus hang out.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
I am not a guru...lol

Well first off open() should be first, than on readystatechange

Is the URl changing for each request? If not it is being cached by the browser. To get around that issue, you would need to add no-cache headers on your serverside page and you can add a timestamp/random number to the url as a querystring parameter. Or you can go with the post instead of a get.

Personally you are wasting lines of code by having an open/send statement for the native object and an open/send statement for the ActiveX. I would say look into prototype.js to do you Ajax calls.

Eric
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61658
    
  67

Originally posted by Eric Pascarello:
I would say look into prototype.js to do you Ajax calls.


Second that motion.

I've been using Prototype for some time now, not only for its Ajax simplifications, but its other extensions as well, and it's made my life a lot easier.

Anyone who follows my posts knows I'm very skeptical about 3rd party libraries and frameworks, but Prototype is a great example of a well-thought-out and useful library.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: AJAX part in jsp is not functioning with IE 6.0