aspose file tools*
The moose likes Servlets and the fly likes Rendering a View before the Servlet finishes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Rendering a View before the Servlet finishes" Watch "Rendering a View before the Servlet finishes" New topic
Author

Rendering a View before the Servlet finishes

Ramon Querez
Greenhorn

Joined: May 30, 2007
Posts: 2
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?
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Hi, welcome to the ranch!

This may be way off base, but I dimly recall seeing a page that flushed and closed the output stream and then did more work.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Leandro Melo
Ranch Hand

Joined: Mar 27, 2004
Posts: 401
Hi.

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):



Does that help at all?


Leandro Melo
SCJP 1.4, SCWCD 1.4
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
The solution to this is simple.Do nothing.
Explained by Leandro Melo.

What issues are you facing now ,using the code that you have mentioned?


Rahul Bhattacharjee
LinkedIn - Blog
Ramon Querez
Greenhorn

Joined: May 30, 2007
Posts: 2
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....
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60082
    
  65

What's taking so long? Saving data to a DB should not be a blocking operation.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Pravin Jain
Ranch Hand

Joined: May 20, 2007
Posts: 60
probably you can try adding
response.flushBuffer()
after calling forward.


The Zen of Java Programming.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

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.
Sandeep Deb
Greenhorn

Joined: Jun 01, 2007
Posts: 15
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.

Regards,
Sandeep.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Rendering a View before the Servlet finishes
 
Similar Threads
How to send request data
Adding Component on Runtime
Struts with WSAD 5.0 question
opening a new window from servlet
Conditional wrapping with tag