Steve Kuo
Steve Kuo

Reputation: 63094

JspWriter write versus print

I'm developing some custom JSP tags. In my SimpleTag.doTag() I grab the JspContext and call getOut() to get the JspWriter. When writing to JspWriter, what's the different between write(String) and print(String)? Should I be calling one instead of the other?

Upvotes: 6

Views: 3159

Answers (2)

Pierre
Pierre

Reputation: 35276

from the javadoc:

The 'write' function was inherited from java.io.writer .

The 'print' function: prints "null" if the argument was null. Otherwise, the string's characters are written to the JspWriter's buffer or, if no buffer is used, directly to the underlying writer.

Upvotes: 3

brabster
brabster

Reputation: 43580

The print() method can buffer, the write() method is inherited from the Writer class and cannot - so you may get better performance from the JspWriter's print() method.

In addition, the print() method is overloaded to take many different types of objects as an argument, whereas the write method deals in Strings and chars only.

See the JspWriter javadocs for more details.

Upvotes: 9

Related Questions