Share on Google+Share on Google+

JSP implicit object out

Advertisement
In this Section, we will discuss about implicit object out & it's methods.

JSP implicit object out

In this Section, we will discuss about implicit object out & it's methods. Out object denotes the Output stream in the context of page. The class or the interface name of the object out is "jsp.JspWriter". This object is instantiated implicitly from JSP Writer class and can be used for displaying anything within delimiters. 

Methods in out object

  • print
  • println
  • newLine
  • clear
  • clearBuffer
  • flush
  • isAutoFlush
  • getBufferSize
  • getRemaining.

(1)print() : The print method of out object writes the value to the output without a newline character.

Example :

If a JSP program has the following statements :

  out.print("Welcome!!!");
  out.print("Guests");

Output :

  Welcome!!! Guests

(2)println() : The println method of out object writes the value to the output with a newline character.

  out.println("Welcome!!!");
  out.println("Guests");

Output :

  Welcome!!!
  Guests

(3)newLine : The 'newLine' method of out object is used to write a newline character to the output.

Example : If a JSP program has the following statements :

out.write("Welcome!!!");
out.newLine();
out.write("Guests");

Output :

  Welcome!!!
  Guests

(4)clear :

The clear method of out object is used to clear the output buffer. This method does not write any contents to the client. An exception is thrown by this method if the buffer was flushed.

Syntax :  out.clear() ;  

(5)clearBuffer :

The 'clearBuffer' method of out object is used to clear the output buffer. This method does not write any contents to the client. The only difference between the 'clear' method of out object and 'clearBuffer' method is

  • 'clear' method throws an exception when the buffer is flushed.
  • 'clearBuffer' method does not throw an exception when the buffer is flushed.

Example : 

if (out.getBufferSize() != 0)
                out.clearBuffer();

(6)flush : Two methods of out object, 'clear' and 'clearBuffer' are used to clear the output buffer without writing any contents to the client. The 'flush' method of out object is used to flush the buffer by writing the contents to the client.

Example :

 out.flush();
 <jsp:forward page="roseindia.jsp&">

In the above example, no forwarding will take place. This is because of the "out.flush()" method. This method will flush the "JspWriter" buffer. Once the content is written to the browser, a forward cannot be invoked.

(7)isAutoFlush() : The "isAutoFlush" method of 'out' object returns a 'true' value if the output buffer is automatically flushed otherwise returns 'false'.

Syntax:     out.isAutoFlush() ;

(8)getBufferSize : The "getBufferSize" method of 'out' object is used to return the size of the buffer. The returned value of the size of the buffer is in bytes. If the output is not buffered, then the "getBufferSize" method returns a 0 byte.

Example :

if (out.getBufferSize() != 0)
    out.getBufferSize()

(9)getRemaining() : The "getRemaining" method of out object is used to return the number of empty bytes in the buffer.

Syntax :  out.getRemaining() ;

EXAMPLE USING "out" OBJECT

<HTML>

<BODY>

<%

// This scriptlet declares and initializes "date"

System.out.println( "Evaluating date now" );

java.util.Date date = new java.util.Date();

%>

Hello! The time is now

<%

out.println( date );

out.println( "<BR>Your machine's address is " );

out.println( request.getRemoteHost());

%>

</BODY>

</HTML>

OUTPUT :

Advertisements

Advertisement

Posted on: June 23, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.