• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Rendering a View before the Servlet finishes

 
Ramon Querez
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Leandro Melo
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ramon Querez
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64720
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's taking so long? Saving data to a DB should not be a blocking operation.
 
Pravin Jain
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
probably you can try adding
response.flushBuffer()
after calling forward.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic