• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

difference between out.write(....) and out.print(...) statements

 
pradeep singh
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi to all
Can anybody tell me :What is the difference between these below statements (means difference between print and write methods)and when we used them separately .Please explain with examples?

1> out.println("hi this is a statement");
2> out.write("hi this is a statement");
 
Venkata Sunil Yadiki
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As per docs ServletOutputStream is suitable for writing binary data in the response (using response.getOutputStream() & out.write()) and PrintWriter is used to send character text to the client (using response.getWriter() & out.print())
 
Nguyen Hoang
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that.
out.println() is method of JspWriter.
out.write() is method of java.io.Writer.

So, you can not use "write" method to write primary type (exp boolean) that "print" can do.
 
Sameer Raza Ali
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The difference of the class it comes from is obvious but i think the question is
why does a jsp expression <%=new Date()%> will translate to an out.print(new Date) in generated Servlet
and a template text like <p> example<p> will translate to an out.write("<p>example<p>")
The out implicit variable is of type JspWriter.

write() method is there from the super class java.io.Writer does this write to the response directly or to the buffer. please explain
what is the difference.

Many Thanks
Sameer
 
Sameer Raza Ali
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oops the answer is above the write does not have the capability to handle primitives, objects as arguments while print method has overloaded versions for each
That explains it
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic