wood burning stoves 2.0*
The moose likes Servlets and the fly likes getWriter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "getWriter" Watch "getWriter" New topic
Author

getWriter

Rajeev Hegde
Greenhorn

Joined: Aug 04, 2010
Posts: 2
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

Joined: Oct 13, 2005
Posts: 39436
    
  28
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

Joined: Aug 04, 2010
Posts: 2
Thanks for replying

I understood.

But when is the first time response gets write object and who gives it? container?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Nov 04, 2009
Posts: 2066

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?


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Nov 04, 2009
Posts: 2066

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

Joined: Jan 10, 2002
Posts: 61437
    
  67

You have Java code in your JSP?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: getWriter