• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

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

 
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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");
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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())
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
This is awkward. I've grown a second evil head. I'm going to need a machete and a tiny ad ...
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic