• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why can't get the object "out" in this way?  RSS feed

 
Yashnoo lyo
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We use "PrintWriter out=response.getWriter()"to get a "out" object in Servlet and output text to webbrowser with it.But I think the PrintWriter is a class and Why can't I get the object "out" use this way:"PrintWriter out=new PrintWriter()"?I think the later is easier to understand.Anyone can help me? :roll:
 
Chris Mathews
Ranch Hand
Posts: 2712
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For one, a PrintWriter needs to wrap another Writer or OutputStream, therefore it does not have a no-arg constructor.
If PrintWriter had a no-arg constructor then your code would work perfectly fine. However, whatever you printed to the PrintWriter would not be sent to the HttpServletResponse and therefore the client would never see it.
Plain and simple, if you want to send output to the Servlet's client then you need to get your writer from the HttpServletResponse. No other choice.
 
Yashnoo lyo
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know it.It is for get my writer.Thank you for your help.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!