• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why should we set response.setContentType before using PrintWriter or OutputStream?

 
akash kumar
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why should we set response.setContentType before using PrintWriter or OutputStream?
 
Lorand Komaromi
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
akash kumar wrote:why should we set response.setContentType before using PrintWriter or OutputStream?


You have to tell the browser what kind of data are you sending so that it can handle it appropriately...
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13074
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Obviously you can't set response headers if the response output stream has been flushed.

Now you might think that writing is safe because the output buffer would hold some characters before flushing - that way lies very mysterious future bugs.

Bill
 
akash kumar
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
William Brogden wrote:Obviously you can't set response headers if the response output stream has been flushed.

Now you might think that writing is safe because the output buffer would hold some characters before flushing - that way lies very mysterious future bugs.

Bill



Does it mean the web server starts sending the results to the browser even before the doGet or doPost methods are returned?
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13074
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does it mean the web server starts sending the results to the browser even before the doGet or doPost methods are returned?


Absolutely!

Thats why, if your application has to sent a content-length header, you have to provide your own buffering or temporary file scheme while you build the complete response.

Bill
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic