Use send redirect before doGet finish

I have a servlet that emails a report to the user when they fill in the form. Then the servlet redirects to another page. I try to send the redirect page first and let the process continue as shown in the code:

public void doGet(HttpServletRequest request, HttpServletResponse response) {

response.sendRedirect(response.encodeRedirectURL(揳nother_page?;

processAndsendEmail(); //takes 10 seconds

}

However the server (Tomcat) seems to send the redirect page after the doGet method is done. The user can not see the another_page until the process is finished.

Any idea to make this work?

Thanks.

[642 byte] By [BruceJina] at [2007-11-27 8:35:36]
# 1

The send redirect method sets a header code in the response. If the response is still in the buffer until the method completes then the client never sees until then. I think a better solution would be to use a separate thread to send the e-mail. Kick off the e-mail thread and then do the redirect.

Or you could try flushing the ServletOutputStream to clear the buffer.

tolmanka at 2007-7-12 20:32:16 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...