jQuery in Action, 2nd edition*
The moose likes Servlets and the fly likes Display partial response Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Display partial response" Watch "Display partial response" New topic
Author

Display partial response

Rovas Kram
Ranch Hand

Joined: Aug 08, 2003
Posts: 135
I think I have seen a web page display part of the html response while the server is still returning information. Is there a way of coding your servlet so that for a given page part of the html response can be displayed before the servlet is done writting to the output stream?
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
At any point, you can do a out.flush() - that should send any characters currently in the output buffer. Of course, whether or not the browser display will show any activity depends on the browser and the HTML you are sending. I think a plain <p> paragraph will get displayed but if you are building a table the browser may wait until the table is finished to show it.
Bill
Rovas Kram
Ranch Hand

Joined: Aug 08, 2003
Posts: 135
Thanks William,

I shall try that presently.
Atul Prabhu
Ranch Hand

Joined: Dec 17, 2002
Posts: 60
Hi William,

Could u tell me the reason why response.sendRedirect() does not work after using out.flush().

----
Atul
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
Flushing the output buffer for the first time sends the HTTP headers. Since redirect involves setting a header line, if you do it after the headers have been sent you get a IllegalStateException.
Bill
Peter Bell
Greenhorn

Joined: Mar 25, 2003
Posts: 19
Another option is to create a task for the long running action.

Assign it an id.

The task writes its progress to a session variable keyed by that id.

Send the user to a page with that id as a parameter.

That page loads the progress from the session based on the id. The page reloads itself (using Javascript or a meta refresh tag) until the task is complete.


RimuHosting - JSP and Java Hosting
Atul Prabhu
Ranch Hand

Joined: Dec 17, 2002
Posts: 60
Hi Willaim,

Here are two cases :

Case 1:

out.println("Wait....");
out.flush();
response.sendRedirect("next.html");


In this case I get a Illegal State Exception (its right w.r.t Willaim)

Case 2:

out.println("Wait...");
response.sendRedirect("next.html");

In this case I dont get any exception.It redirects to the next page.
Doesn't out.println() stmt set the HTTPHeaders..?

I am a bit confused. Do you have any good tutorial which explains this concept in detail?

--
Atul
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Display partial response