It’s an instance of javax.servlet.jsp.JspWriter. This allows us to access Servlet output stream. The output which needs to be sent to the client (browser) is passed through this object. In simple words out implicit object is used to write content to the client.
Methods of OUT Implicit Object
Let’s see each of the out’s method in detail –
- void print(): This method writes the value which has been passed to it. For e.g. the below statement would display a sentence Out Implicit Object in jSP – BeginnersBook to the output screen (client browser).
out.print(“Out Implicit Object in jSP - BeginnersBook”);
- void println(): This method is similar to the print() method, the only difference between print and println is that the println() method adds a new line character at the end. Let’s have a look at the difference with the help of an example.
out.print(“hi”); out.print(" "); out.print(“hello”);
output on browser: There will not be a new line between the outcomes of all 3 out.print statements.
output on browser:
- void newLine(): This method adds a new line to the output. Example –
out.print(“This will write content without a new line”); out.newLine(); out.print(“I’m just an another print statement”);
This will write content without a new line
I’m just an another print statement
As you know print statement doesn’t add a new line. We have added a new line between two out.print statements using newLine() method.
- void clear(): It clears the output buffer without even letting it write the buffer content to the client. This is how it can be called –
- void clearBuffer(): This method is similar to the clear() method. The only difference between them is that when we invoke out.clear() on an already flushed buffer it throws an exception, however out.clearBuffer() doesn’t.
- void flush() : This method also clears the buffer just like clear() method but it forces it to write the content to the output before flushing it, which means whatever is there in buffer would be written to the client screen before clearing the buffer.
- boolean isAutoFlush() : It returns a Boolean value true/false. It is used to check whether the buffer is automatically flushed or not.
- int getBufferSize(): This method returns the size of output buffer in bytes.
- int getRemaining(): It returns the number of bytes remaining before hitting the buffer overflow condition.
OUT Implicit Object Example
In this example we are using print and println methods of OUT for displaying few message to the client.
<HTML> <HEAD> <TITLE> OUT IMPLICIT OBJECT EXAMPLE </TITLE> </HEAD> <BODY> <% out.print( "print statement " ); out.println( "println" ); out.print("Another print statement"); %> </BODY> </HTML>
print statement println
Another print statement