FAQ


Advertisement



What is the out implicit object?

The out implicit object is used for writing data to the output stream and is an object of type javax.servlet.jsp.JspWriter. When you use implicit out object to pass the data to the client from your JSP, the JSP Engine/container creates a JSPWriter object and put it in the _jspService() method. You don't need to bother about writing _jspService() method in your JSP, JSP Engine does that work for you.

If the JSP page directive selects autoflush="true" then all the I/O operations on this class shall automatically fluch the contents of the buffer if an overflow condition would result if the current operation were performed without a flush. If autoflush="false" then all the I/O operations on this class shall throw an IOException if performing the current opertion would result in a buffer overflow condition. JspWriter is a buffered version of PrintWriter that is used to send output to the client. Note that you can adjust the buffer size, or even turn buffering off, through use of the buffer attribute of the page directive. Also note that out is used almost exclusively in scriptlets, since JSP expressions automatically get placed in the output stream, and thus rarely need to refer to out explicitly.

The following table describes common methods of the out object:

Method Description
clear Clears the output buffer without writing the contents to the client. This method throws an exception if the buffer was flushed.
clearBuffer Clears the output buffer without writing the contents to the client.
flush Flushes the buffer by writing the contents to the client. When passing huge data to the client from your JSP/Servlet, user may need to wait till the ServletOutputStream or JSPWriter flushes the data. This happens generally whenever you have a number of gifs per page and you want to pass it to the client. The better approach is to flush the data partly using flush() method rather than flushing whole data at a time.
getBufferSize Returns the size of the buffer in bytes. Returns 0 if the output is not buffered.
getRemaining Returns the number of empty bytes in the buffer.
isAutoFlush Returns true if the output buffer is flushed automatically.
newLine Writes a newline character to the output.
print Writes a value to the output, with no newline character.
println Writes a value to the output, including a newline character. The method internally calls print() method and there is no need for a new line separation when generating html pages. So a small overhead of calling one more method is reduced if you use print() method directly.

JSP page authors are prohibited from writing directly to either the PrintWriter or OutputStream associated with the ServletResponse.(See JavaServer Pages Specification Version 1.2): For example, you can not use the following code in you JSP otherwise you will get compiling error:

<% response.getWriter().println("Hello!"); %>


Printer-friendly version Printer-friendly version | Send this 
article to a friend Mail this to a friend

Previous Next vertical dots separating previous/next from contents/index/pdf Contents

  |   |