• Post Reply Bookmark Topic Watch Topic
  • New Topic

About flush() in Servlet

 
Jose Mon
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am trying to send data partially to the browser. So I use flush() method in PrintWriter Object. This will work fine when the data to be send is more than some 100 characters. But if it 5-10 chars the flush is not sending the data at that moment.



Here if stringToPrint conatains more than 100 characters it is sending the data at that moment itself. But otherwise it is sending it later.

Any Idea about his.
Thanjs in Advace.


[BSouther: Added UBB CODE tags]
[ March 21, 2007: Message edited by: Ben Souther ]
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suspect that the servlet is indeed flushing the output stream.
If this is the first time it is flushed, the buffer will send the response headers plus whatever you have written.
However, on the web browser side, the browser can decide that it does not have enough to even try to show something and will just wait for more. Perhaps you should ensure that what you send makes good HTML sense.

Bill
 
Jose Mon
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any way to meet the requirement. By using any header entry or by setting minimum cache or buffer or like that.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What exactly is your requirement? If you need to progressively disclose parts of the page, you'll need to use a means that gives you more control over the process. flush() was never meant for that purpose.
 
Jose Mon
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I want to progressively display the items. Like,

Contacting Vendor................
Validating CreditCard............
Calculating Amount..............
Retriving Amount...............
.............
..............

Here, each process take some time(about 20 sec)

In what way I can do this easily.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it that these things are legitimately taking 20 seconds each or do you just want it to appear that they do?
 
Jose Mon
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, those process take that much time. So I need to show the progress to the user.
 
Jose Mon
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, Any simple way for doing this?
 
Jose Mon
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, Any simple way for doing this?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
20 seconds is a long time to hold a browser connection open.

An alternate technique would be to spawn a new thread for your long running process, and return a status page to the user right away.
That page could refresh every n seconds until the process has finished.

I have an example app that does just this at:
http://simple.souther.us/not-so-simple.html
Look at LongRunningProcess.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!