Problems refreshing page with AJAX

Hello,

I have never used AJAX but today I decided to try it to improve my current implementation of a "processing" page that I use to display a waiting message and to check every 5 seconds if a response is available. If it is, the user is redirected to a "processing done" page.

My previous implementation used the refresh tag provided by HTML in the "processing" page:

<meta HTTP-EQUIV="Refresh" CONTENT="5; URL=<%=request.getContextPath()%>/main/CheckForResponse.do"/>

Every 5 seconds a struts action is executed and the action itself will forward to the "processing done" page when a response is available.

This solution works, however I don't like the refresh page effect, so I was trying to use AJAX instead. Now I put in the "processing" page the following script:

function checkForResponse()

{

var xmlHttp;

try

{

// Firefox, Opera 8.0+, Safari

xmlHttp=new XMLHttpRequest();

}

catch (e)

{

// Internet Explorer

try

{

xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");

}

catch (e)

{

try

{

xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");

}

catch (e)

{

alert("Your browser does not support AJAX!");

returnfalse;

}

}

}

xmlHttp.open("GET","<%=request.getContextPath()%>/main/CheckForResponse.do",true);

xmlHttp.send(null);

}

and I have the following html body tag.

<body onLoad="setInterval('checkForResponse()',5000);">

The function executes correctly and the struts action is also executed. However the problem is that for some reason the action doesn't forward to the "processing done" page. It does actually, if I debug, but nothing happens, the "waiting" page keeps being displayed.

Does anyone have an idea about what I could be doing wrong? any suggestion?

Thanks a lot!!

[2937 byte] By [Petuniaa] at [2007-11-26 19:20:30]
# 1

Did you define the call back function correctly? like===

xmlhttp.onreadystatechange = handleHttpEvent;

function handleHttpEvent(){

if (xmlhttp.readyState == 4) {

if (xmlhttp.status == 200) {

// GO TO PROCESSING END PAGE

} else {

window.alert("ERROR");

}

}

}

Also, you might want to check the progress bar application

in bpcatalog -> http://bpcatalog.dev.java.net

yutaa at 2007-7-9 21:38:41 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2

Hi,

thanks for your reply.

I have not defined the callback function because it's the servlet who is invoked by the javascript that should redirect to the processing done page, not the javascript itself.. In fact the servlet (actually struts action) doesn't not return any value that I could check in the callback funtion to understand which page I should display.

Any more suggestion about why this is not working?

Many thanks!

Petunia

Petuniaa at 2007-7-9 21:38:41 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3

First, I'm not sure what you meant by "servlet doesn't return

any value". Either it uses sendRedirect or RequestDispatcher,

it ultimately returns something, right?

Anyway, that's an interesting use case, but I don't think it works

as the browser doesn't recognize the asynchronous

response automatically and acts accordingly.

So, you might want to let the servlet(or the resource that servlet

redirects to) return the content and update the current page.

yutaa at 2007-7-9 21:38:41 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...