Meaningless Drivel is fun!*
The moose likes Servlets and the fly likes How does PrintWriter really work (with ServletOutputStream)? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "How does PrintWriter really work (with ServletOutputStream)?" Watch "How does PrintWriter really work (with ServletOutputStream)?" New topic
Author

How does PrintWriter really work (with ServletOutputStream)?

Florian Heinisch
Greenhorn

Joined: Mar 03, 2005
Posts: 15
I would like to know how PrintWriter really works. The Book "Head First Servlets & JSPs" states on page 132: "The PrintWriter wraps the ServletOutputStream [...] the PrintWriter has a reference to the ServletOutputStream and delegates calls to it. There's just ONE output stream back to the client, but the PrintWriter decorates the stream by adding higher-level character-friendly methods."

How does the PrintWriter get the reference? Is it implemented by the Servlet container? How does this work in detail?

Another fact that confused me a bit is William Brogden's statement in http://www.coderanch.com/t/351065/Servlets/java/Printwriter-vs-ServletOutputStream that the PrintWriter does Unicode character conversion. Does this mean that any text that I send from the servlet to the client via the PrintWriter arrives encoded in Unicode?

Thanks for any help!
[ March 03, 2005: Message edited by: Florian Heinisch ]
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
The HttpServletResponse object is responsible for wrapping the PrintWriter around the response stream. From the JavaDoc for HttpServletResponse.getWriter() method...

The character encoding used is the one specified in the charset= property of the setContentType(java.lang.String) method, which must be called before calling this method for the charset to take effect.


So, the response object will use the appropriate encoding.


James Carman, President<br />Carman Consulting, Inc.
Florian Heinisch
Greenhorn

Joined: Mar 03, 2005
Posts: 15
Hi James,

thanks a lot for your quick answer. You brought me a step further. So the PrintWriter does not necessarily convert into Unicode, right?

What happens in the background when following code gets executed:

How does the String "Hello World" gets written to the ServletOutputStream?
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
Inside the servlet response, when you ask for the PrintWriter, it's going to do the following...





At the end of the response, the servlet container will make sure everything is flushed by calling flushBuffer() on the response object.
Florian Heinisch
Greenhorn

Joined: Mar 03, 2005
Posts: 15
Hi James,

now I got the point. Thank you so much! You really helped me a lot!
 
Don't get me started about those stupid light bulbs.
 
subject: How does PrintWriter really work (with ServletOutputStream)?