aspose file tools*
The moose likes Servlets and the fly likes streaming output Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "streaming output" Watch "streaming output" New topic
Author

streaming output

shankar vembu
Ranch Hand

Joined: May 10, 2001
Posts: 309
I was just trying to test whether i can send streaming output to the client from my servlet. So i wrote the following code:

This is actually taken from a javaworld article.
Now the flush is not working for me. I am using IE. I expect my browser to display countdown number every sec. But what I get is only the final response with all the numbers. So is this not the right way to achive something like a streaming output???
Regards.
Sean Sullivan
Ranch Hand

Joined: Sep 09, 2001
Posts: 427
What servlet engine ( or app server) are you using?
http://java.sun.com/products/servlet/2.3/javadoc/javax/servlet/ServletResponse.html
Have you tried calling flushBuffer() on the HttpServletResponse object?
-Sean
shankar vembu
Ranch Hand

Joined: May 10, 2001
Posts: 309
Originally posted by Sean Sullivan:
What servlet engine ( or app server) are you using?
http://java.sun.com/products/servlet/2.3/javadoc/javax/servlet/ServletResponse.html
Have you tried calling flushBuffer() on the HttpServletResponse object?
-Sean

tomcat. tried flushing the buffer, but it wont work either
shankar vembu
Ranch Hand

Joined: May 10, 2001
Posts: 309
any pointers. i am still stuck with this issue. BTW i guess the same principle is being used in pushlets, right???
Shankar
Mark Latham
Greenhorn

Joined: May 27, 2003
Posts: 22
I copied and pasted the code you posted and tested it on Tomcat 4.1.29 with Mozilla Firebird 0.7 and IE 5.5 as the browsers. It worked as advertised with Firebird, but not with IE. With IE, it didn't display any output until the page was completely rendered.
Though I am always eager to disparage IE, I would not recommend depending on flushing the output of a slow servlet even if it worked correctly. I highly recommend refactoring such a servlet (or tweaking the business requirements) so it is more responsive. If, however, you need a quick and dirty browser-independent solution, you could always display an intermediate processing page while the code executes.
Mark Latham
Greenhorn

Joined: May 27, 2003
Posts: 22
I don't know how it happened, but Shankar's followup question got deleted when I submitted my response. So the thread makes sense, I'll summarize his question regarding the quick and dirty method.
How can you do 2 responses for one request? As soon as you send back the 1st one, you'll lose your reference to the HttpServletResponse and won't be able to send anymore info back to the browser.

You submit to the intermediate page. The intermediate page has 2 purposes:
  • Let the user know her request is being processed
  • Forward the request on to the Servlet that will be doing the actual work


  • For example, I created a JSP named processing.jsp that contains the following:
    *Note: There's an extra n in the onnload because of Java Ranch JavaScript security restrictions.
    Then I request the page processing.jsp?target=flushServlet. The processing page is then immediately returned to the user (after it compiles the first time) while the Servlet is doing its work. This way the user gets immediate feedback that her request is being processed and she's discouraged from submitting the form again.
    And please remember, this solution is what I described as "quick and dirty". I still recommend making the Servlet itself respond faster.
    [ November 26, 2003: Message edited by: Mark Latham ]
    [ November 26, 2003: Message edited by: Mark Latham ]
     
     
    subject: streaming output