File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes why we go for response.getWriter() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "why we go for response.getWriter()" Watch "why we go for response.getWriter()" New topic

why we go for response.getWriter()

Arun Maalik
Ranch Hand

Joined: Oct 25, 2005
Posts: 216
Dear sir
why we go for response.getWriter() ,PrintWriter is a class we can make the object via PrintWriter obj=new PrintWriter();Why we did like that in every servlet program.

Thanks and Regards
Arun kumar maalik
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

Look again at the API for the PrintWriter class.

There is no empty constructor.
In order to create one, you need something some kind of outputstream or file.

The container insures that you have a valid outputstream that will go back to the client.

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3753

If you're asking why its done that way everytime (I agree that it is repetitive), its because java is not a pure web language. Most of the use of java in servlet/jsps are extensions of the language in order to make it more accessible to web programmers.

Its why PHP is often a lot faster than writing in JSP/Servlets, but with the added work comes more power and more control. For example, you can have servlets that perform work and never use the print writer.

[OCA 8 Book] [Blog]
Arun Maalik
Ranch Hand

Joined: Oct 25, 2005
Posts: 216
Thanks sir
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13037
For servlets to work, it must be possible to configure the response output stream for binary output (images, etc) or character output (HTML) under the programmers control. Therefore we have getWriter() and getOutputStream() - the programmer must choose the appropriate one.
I agree. Here's the link:
subject: why we go for response.getWriter()
It's not a secret anymore!