• Post Reply Bookmark Topic Watch Topic
  • New Topic

Printwriter vs ServletOutputStream

 
DC Dalton
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im a touch confused on the use of the Printwriter.out = getWriter();, the PrintStream out = new PrintStream (response.getOutputStream()); & the ServletOutputStream out = res.getOutputStream(); . I have looked through the API & do notice some minor differences such as the flush in printwriter but beside that I really dont see much difference. By looking at other peoples servlets I see many of them use the PrintStream out approach. I was taught to use the ServletOutputStream in servlets. Is there any advantage to these Im missing or am I just confusing the issue for myself?
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a BIG difference. PrintWriter should only be used to output Strings because it will do a Unicode character conversion.
PrintStream does not do Unicode character conversions correctly but is handy if you are entirely in ASCII.
ServletOutputStream should be used for binary data, it will not do character conversion.
Character conversion is surprisingly expensive so if you have a lot of text (for instance read from an ASCII file) consider keeping it as byte[] instead of as String objects.
Bill

------------------
author of:
 
DC Dalton
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, that makes sense to me but let me ask you this. Returning information from a database & outputting html to the browser........is any one of these more efficient or should I say less server intensive than another. I had tried the printwriter out = getWriter(); (or something like that, I may not have the code exactly right but I saw it on one of the other postings here) a servlet a couple of weeks ago & the compiler told me it couldnt be used in this way. Im writing servlets rigth now that are nothing more than db queries (MySql) that are outputted in html to the browser. Thanks for the info.....BTW Im halfway thru your exam cram (getting ready for the test) & really enjoy it!
 
Bosun Bello
Ranch Hand
Posts: 1511
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Like Bill said, for what you are doing, you just need to use printWriter.

Bosun
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It all depends on what you are getting back from the database. If your data is just returned as a String, PrintWriter is fine. If you are getting back a byte array, then OutputStream is what you want.
Also if you have a low usage site the Unicode conversion cost doesn't make much difference. If you have to wring the last bit of performance out of a site, take a look at the book "Java Performance and scalability, Vol 1" by Dov Bulka, ISBN0-201-70429-3, for discussion of optimizing.
Bill
 
DC Dalton
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im going to give it a shot today & see what changes I have to make........thanks for the info
 
Joseph Sweet
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everyone,

Any good book about this subject of comparing streams, writers, etc., how they work from inside, why we need so many of them?
Somehow I get a headache whenever I try to figure out what actually the difference is between all those streams that wrap(wrap(wrap())) each other.

Thanks.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65522
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Brains! Brains!...." (talk about resurrecting a zombie! )

This would probably be better asked in the I/O forum as it's really not Servlet-specific. But I don't want to move this old thread there so I'm going to close it and let you copy/paste your question into a new topic there.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!