Is there any way of forcing the browser to render the view before the servlet has finished processing? For example,
The obvious issue being here that the page forwarded to could then have it's own servlet, resulting in some nasty multi-threading issues, but if it's a static html page that problem won't arise.
I guess I could do this by forwarding to the view (next.jsp), having some js code that autosubmits the page, and the corrosponding servlet deals with the time-intensive code, returning the same view, but surely there's a better way to go about it than that?
Originally posted by Ramon Querez: The obvious issue being here that the page forwarded to could then have it's own servlet, resulting in some nasty multi-threading issues, but if it's a static html page that problem won't arise.
Even in this case, you could have servletA dispatching to servletB. It doesn't need to be static html. I don't quite understand what you're calling multi-threading issues. The servlets are different and there's only one instance of a serlvet in the container (unless you implement SingleThreadModel). Yes, you need to care about multi-threading issues if you're manipulating the same data. But this is just as usual.
Originally posted by Ramon Querez: I guess I could do this by forwarding to the view (next.jsp), having some js code that autosubmits the page, and the corrosponding servlet deals with the time-intensive code, returning the same view, but surely there's a better way to go about it than that?[/QB]
I think your code is already the solution you're looking for. After you call forward you can still perform work in your servlet. The only thing you can not do is write to the output buffer again (an exception will be thrown). So, if I understand your question correctly, you could do this (exactly the code your provided):
Thanks for the replies. Regarding Leandro's comments:
My question was not regarding the ability to do work in the servlet after calling the forward method on the RequestDispatcher object. It was to do with forcing the browser to display the page (next.jsp) immediately on calling that method.
How do I force the rendering before the servlet exits?
If the page is taking too long to be rendered, the user could think that a problem has occurred....
Similar to Bear's post but a little more specific: what does this have to do with Servlets?
Sure, the request comes in to the Servlet, but if the Servlet delegates to a view, then all of that database gumph after the fact has nothing to do with the Servlet. If you need to do post-processing then fire off a separate worker thread and do some post processing.
Dear Ramon, The RequestDispatcher.forward(..) method forces the container to purge the view data before the thread exits from the servlet. This can be ascertained from the specifications (2.4).
Before the forward method of the RequestDispatcher interface returns, the response content must be sent and committed, and closed by the servlet container. [SRV8.4-Servlet Spec 2.4]
Also, please note that as per specifications the the Container Provider must ensure that the dispatch of the request to a target servlet occurs in the same thread of the same VM as the original request and hence the DB processing work won't start till the forwarded view content generation is completed.
I hope this helps.
subject: Rendering a View before the Servlet finishes