The Out Object in JSP

By: Sathya Narayana Viewed: 153211 times    

The out object is an instance of the javax.servlet.jsp.JspWriter class. It's used to create the content returned to the client and has the following useful methods available.

clear() clears the contents of the buffer; it throws an exception if some data has already been written to the output stream:

public abstract void clear()
throws java.io.IOException

clearBuffer() clears the contents of the buffer but doesn't throw an exception if some data has already been written to the output stream:

public abstract void clearBuffer()
throws java.io.IOException

close() flushes and then closes the output stream:

public abstract void close()
throws java.io.IOException

flush() flushes the output buffer and sends any bytes contained in the buffer to their intended destination. flush() will flush all the buffers in a chain of Writers and OutputStreams:

public abstract void flush()
throws java.io.IOException

getBufferSize() returns the size in bytes of the output buffer:

public int getBufferSize()

getRemaining() returns the number of bytes still contained in the buffer. It will return 0 if it's unbuffered:

public abstract int getRemaining()

isAutoFlush() returns true if the buffer flushes automatically when an overflow condition occurs:

public boolean isAutoFlush()

newLine() writes a new line character to the output stream:

public abstract void newLine()
throws java.io.IOException

print() prints the specified primitive data type, Object,or String to the client:

public abstract void print(boolean b)
throws java.io.IOException
public abstract void print(char c)
throws java.io.IOException
public abstract void print(int i)
throws java.io.IOException
public abstract void print(long l)
throws java.io.IOException
public abstract void print(float f)
throws java.io.IOException
public abstract void print(double d)
throws java.io.IOException
public abstract void print(char[] s)
throws java.io.IOException
public abstract void print(String s)
throws java.io.IOException
public abstract void print(Object obj)
throws java.io.IOException


Here's an example:

<%
try
{
boolean b = false;
out.print(b);

}
catch(java.io.IOException ioe)
{
//Catch error.
}
%>

The previous will print false in the browser window.

println() prints the specified primitive data type, Object, or String to the client, followed by a new line character at the end. The no-argument version simply writes a new line character:

public abstract void println()
throws java.io.IOException
public abstract void println(boolean x)
throws java.io.IOException
public abstract void println(char x)
throws java.io.IOException
public abstract void println(int x)
throws java.io.IOException
public abstract void println(long x)
throws java.io.IOException
public abstract void println(float x)
throws java.io.IOException
public abstract void println(double x)
throws java.io.IOException
public abstract void println(char[] x)
throws java.io.IOException
public abstract void println(String x)
throws java.io.IOException
public abstract void println(Object x)
throws java.io.IOException

For example:

<%
try
{
out.println("<html><title>Page Title</title></html>");
}
catch(java.io.IOException ioe)
{
//Catch error.
}
%>

Most Viewed Articles (in JSP )

Latest Articles (in JSP)

Comment on this tutorial