2

I have two jsp pages and one servlet. When user clicks submit button then processing is done in servlet and then result goes to another jsp page so that user sees his result. But I want to show a loading gif so that that gif will run till user gets his result and if in the mid time user stops that browser from loading then automatically that gif will also stop, how to do it?? In javascript or other only user will see that gif picture but when user will stop browser's processing still that gif is running which should not happen. How can i resolve this??

index.jsp---------------goes to a servlet------------result.jsp

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
sujit
  • 251
  • 5
  • 12
  • 23
  • See similar question at http://stackoverflow.com/questions/327681/method-for-displaying-loading-message – Amit Dec 09 '11 at 10:30

6 Answers6

2

Since all of this is going to happen in the user's browser, the solution cannot be in Java, it will have to be JavaScript, reacting to the onabort event.

As for stopping a GIF animation, that's not really possible (except perhaps through nasty hacks). I suggest using JavaScript (e.g. jQuery builtins or plugins) to do the animation as well.

Update: On second thought, why do you think you need to do anything at all? If the browser aborths the loading, it's their own fault, and the browser already has a loading animation that does exactly what you want. Why duplicate that?

Community
  • 1
  • 1
Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
  • ok if i can avoid gif then i can show a message something like "please wait...". If this message can be processed from servlet then before displaying result page it will display"please wait...". How to do it? In servlet i am not able to do it as when request is coming to the servlet then first servlet is fetching data then immediately redirects to result.jsp page – sujit Dec 09 '11 at 10:44
  • @sujit: Aborting the page load is purely client behaviour. There is NOTHING your can do in a servlet that will have any effect on this. – Michael Borgwardt Dec 09 '11 at 10:45
  • thnx but some error is coming on window.onabort event please write that code so that i can call it on my form. – sujit Dec 09 '11 at 10:50
1

That's nothing Java specific since everything will happen in the browser. You might need some JavaScript to open up that popup before the request is issued and register an event handler that closes the popup on window.onabort.

Thomas
  • 87,414
  • 12
  • 119
  • 157
1

Try this

function doWork() {
  //start loading gif
  $.ajax({
        'type': 'POST',
        'cache': false,
        'contentType': 'application/x-www-form-urlencoded; charset=UTF-8',
        'url': #your_servlet_url#,
        'data': $('form#' + #your_form_id#).serialize(),
        success: function (data) {//data can be json or plain HTML depending on your scenario
            // stop the loading gif
            //redirect to result.jsp if you need to redirect user to completely new page with 'data' received
            // or just send the result.jsp with processed data from the servlet and load the content 'data'
            // received above in  some div on index.jsp
        },
        error: function (xhr, status, err) {  
            //request cancelled by user or due to any other reason such timeout
            if (xhr.status == 0) {
                //stop the loading gif
            }
            // you can handle other errors(401,403 etc) too by examining xhr, status and err objects
        }
  });
}
vikas
  • 1,535
  • 1
  • 13
  • 22
1

Two things I can think of:

  • on submit simply show the gif. All browsers I have checked will not change the current page until the result from the second one is received.

  • use ajax (jQuery) to show the results. Then you can use an ajax activity indicator

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
0

The jQuery JavaScript library probably can provide some nice in progress effect. For the future: HTML5 has a progressbar or such. Use of Ajax might make sence (callback when loaded say inside DIV).

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
0

A very crude solution would be to use ajax for your form submit. You can display a loading gif on submit and as soon as you receive a response update the page and remove the gif. Jquery ajax method would be able to handle abort by user, which would enable you to write logic to remove the gif for instance(or any other processing on page in case of abort)

Other possibility is if you use frameworks like struts(2.0). This gives a wait and execute interceptor especially for this kind of purpose(display an intermediate page while request is processed)

Raks
  • 870
  • 3
  • 11
  • 27