I am writing a servlet to perform a financial transaction. I can re-write it as several related servlets if necessary. Rather than bog down on details, I will explain what I want to accomplish, and the experienced among you will be able to advise me on how to approach this.
A doPost calls my servlet, sending it about 20 parameters. The servlet checks to make certain that the parameters are valid. If they are not, it sends a response, advising the user what is wrong, and allows for a back page to fix the errant items. All this works. My problem arise once the servlet is satisfied with the parameters, and gets down to executing the business logic.
What I want to do is display a "Please wait, we are processing your request" web page while my servlet is chugging away. After the servlet has finished, I want to display a page "Transaction is completed, print this for your records". I want the Transaction Completed page to replace the Please Wait page, but I could live with Transaction Completed appearing in a separate tab or window.
I don't think that I need detailed code at this point. I think that I need general guidance. This is a common web transaction. I must be missing something in the bigger picture.
Using Ajax is the best way to be able to do things exactly the way that you want (whatever that is, you don;t really say). Trying to control the flush and other stuff like that is doomed to failure and a flat spot on the side of your head.
With pure HTML, what you want is two transactions, not one. The HTML specs only allow one response for each request, not two. Do it like this:
browser does POST/GET to kick off transaction
server responds with HTML page saying "please wait" that also automatically does another GET/POST for the "completion page"
Servlet finishes transaction and responds with normal answer
It can look like one POST to the user, but its really two.
Or you can use AJAX
Joined: Mar 26, 2011
Bear & Pat,
Thank you for your responses.
Pat – Your suggestion is the out-of-the-box insight that I sought. The first wave of HTML issues a POST for the second wave of HTML. The browser makes one POST, the servlet one response, yet two pages are displayed. If I understand this correctly, and I were crazy enough, I could set up a daisy chain of pages, each one calling the next.
But can the first wave of HTML issue another POST without user intervention? Doesn’t the user need to press a button to initiate another POST?
No disrespect meant, but that's hardly an "out of the box" insight (which I'm sure Pat will agree with) -- it's a fairly standard way of doing things prior to the advent of Ajax. And as I said, it doesn't give you a whole lot of control, which your original post seemed to indicate that you want. But if that's good enough for your purposes it is a whole lot simpler than the Ajax approach.