In this tutorial, you will learn out implicit object in JSP with the help of examples. It is an instance of javax.servlet.jsp.JspWriter. This allows user to access Servlet output stream. The output which needs to be sent to the client (browser) is passed through this object. Simply put, the JSP out implicit object is used to write content to the client.
Methods of out implicit object
- void print(): This method writes the value that is passed into it. For example, the following statement would display a message “Out Implicit Object in jSP – BeginnersBook” on 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.
when using print():
out.print(“hi”); out.print(" "); out.print(“hello”);
output: There will not be a new line between the outcomes of all 3 out.print() statements.
when using println():
- void newLine(): This method adds a new line in 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 we learned that print statement doesn’t add a new line. We have added a new line between two out.print() statements using
- void clear(): It clears the output buffer even if the content in the buffer is not written on 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 call
out.clear()on an already flushed buffer it throws an exception, however
out.clearBuffer()doesn’t throw any exception even if the buffer is already flushed.
- 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 the buffer is cleared.
- boolean isAutoFlush() : It returns a Boolean value true/false. It is used to check whether the buffer is automatically flushed or not. If buffer is flushed, it returns true else it returns false.
- 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.
JSP out implicit object Example
In this example we are using
println() methods of out object to print messages on the output screen.
<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