• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why we go for response.getWriter()

 
Arun Maalik
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look again at the API for the PrintWriter class.
http://java.sun.com/j2se/1.5.0/docs/api/java/io/PrintWriter.html

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.
 
Scott Selikoff
author
Saloon Keeper
Posts: 4020
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Arun Maalik
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks sir
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Bill
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic