开发者

How to setup an intermediate (waiting/processing) page while the servlet finishes its work

开发者 https://www.devze.com 2023-03-17 22:23 出处:网络
We are using Java JSP / Servlet technology. When a user submits a form on our website, the form is processed by a servlet, which takes 5 to 15 seconds to finish its work and send the results back to t

We are using Java JSP / Servlet technology. When a user submits a form on our website, the form is processed by a servlet, which takes 5 to 15 seconds to finish its work and send the results back to the browser. How do I set up an intermediate page that tells the user that the request is being processed and asks the user to wait for a little while before the results are being displayed?

I am guessing that the intermediate page will have a meta tag or uses javascript to check and see periodically if the results are available.

However, I do not know how the servlet will complete its work if it has already redirected to the intermediate (waiting) page? Does the servlet needs to start a new thread on the server that proces开发者_开发技巧ses the user's request and then immediately redirect the user to the waiting page? Or is there a better way?

I know that I could probably use AJAX. But I was wondering if I could do it without using AJAX.


I wouldn't do it the hard way. It would involve nasty session hacks/tokens/redirects.

I'd just use JavaScript to display some animated gif when the submit button is pressed.

<input type="submit" value="submit" 
    onclick="document.getElementById('loader').style.display = 'block';">
...
<img id="loader" src="loader.gif" style="display: none;" />

In the Servlet code, do not set the response headers and/or write/flush the first bit to the response body before the long running process is finished. Otherwise the page will immediately blank out. In other words, do not touch the HttpServletResponse in any way until the long running process is really finished. This way the original form with the animated gif will be presented as long as possible to the enduser.


You can start a new thread which does the processing. In the same time immediately return the response to client with response code 201 (Accepted) - along with a link where they can get the result when it is available.

In Javascript, you can poll on the given result link for availability of result and get one when it is available.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号