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?
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
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!
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
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.
"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.