• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

getWriter

 
Rajeev Hegde
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does it mean when its said getWriter() returns the printwriter object???...

Printwriter out =new Printwriter()
this also should create a printwriter object

then why this Printwriter out =response.getwriter()

And... response object can call get Writer method since it is inherited from servletResponse( interface).. tat means getwriter is abstract method..

so where is the implementation for getWriter() ...?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Careful about spellings: getwriter() and getWriter() are different things.

If you use a get method, you obtain a reference to whatever that object already has as a field. So response.getWriter() will give you whichever Writer response is already using.

If you write new Writer(), however, you get a completely different Writer.
 
Rajeev Hegde
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for replying

I understood.

But when is the first time response gets write object and who gives it? container?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, the container. You don't want a new one, you want the one that will actually write to the response.

Moving to servlets forum.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've a doubt regarding this. If we use two reference to refer this PrintWriter, what will happen? I used it, but the output is different, when we write using these two reference. Why is that?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Without knowing what you actually did, it's impossible to answer that question. If you have two references to the same object there will be no difference.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Newton wrote:Without knowing what you actually did, it's impossible to answer that question. If you have two references to the same object there will be no difference.


In a JSP page, I've used two references to refer the same PrintWriter object, with the use of getWriter() method. And, I used this two reference to write the output. But they aligned in different way.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64959
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have Java code in your JSP?
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:You have Java code in your JSP?


I used it at the beginning level, not now. At that time, I've this experienced, that's why asked you!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic