Mind using AJAX?
Here's a comprehensive solution that you can relate your question to.
Here's a bit more insight into the same. Or even
this could be of some help.
Not into AJAX? The following code snippet in your
servlet is supposed to let you know if the download has completed.
Supposedly, the file download will be completed as soon as the while loop is exited.
Hope that helps.