Streaming output java -


i have servlet, mapped url, long task , outputs data while it's working.

what want call url , see output in real-time.

let's take example:

package com.tasks;  public class longtaskwithoutput extends httpservlet {     private static final long serialversionuid = 2945022862538743411l;      @override     protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {         response.addheader("content-type", "text/plain");         printstream out = new printstream(response.getoutputstream(), true);          for(int i=0; i<10;i++) {             out.println("# " + i);             out.flush();             try {                 thread.sleep(1000);             } catch(exception e){}         }     }  } 

with following in web.xml:

... <servlet>     <servlet-name>longtaskservlet</servlet-name>     <servlet-class>com.tasks.longtaskwithoutput</servlet-class>     <description>long task servlet</description> </servlet> <servlet-mapping>     <servlet-name>longtaskservlet</servlet-name>     <url-pattern>/longtask</url-pattern> </servlet-mapping> ... 

what happens

if browse localhost/myapp/longtask, browser makes me wait 10 seconds, prints out text @ once.

what should happen

the text should sent browser it's written output stream, , browser should render 1 line every second.

as can see, put out.flush() sure stream flushes every second, still doesn't work.

i tried response.flushbuffer(), had same result.

is there way achieve this?


update

as @madconan suggested, tried use output stream directly:

outputstream out = response.getoutputstream();  for(int i=0; i<10;i++) {     out.write(("# " + + "\n").getbytes());     out.flush();     try {         thread.sleep(1000);     } catch(exception e){} } 

the result, unfortunately, still same.

this upstream issue. browser not going display data receives it. may wait until request complete. might have additional chunking if going through proxy. if snoop on network traffic, bet go through expected.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -