JSP out Object

JSP out Object is implicitly available to the developer on Java Server Pages. 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.

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("To dineshonjava.com");

Output :

Welcome!!! dineshonjava.com

println() :

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

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

Output :
Welcome!!!
Dinesh

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("Dinesh");

Output :
Welcome!!!
Dinesh

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() ;

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();

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="hello.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.

isAutoFlush() :

The “isAutoFlush” method of ‘out’ object returns a ‘true’ value if the output buffer is automatically flushed otherwise returns ‘false’.
Syntax: out.isAutoFlush() ;

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()

getRemaining() :

The “getRemaining” method of out object is used to return the number of empty bytes in the buffer.
Syntax : out.getRemaining() ;

JSP out Object Example:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>JSP pageContext Object Example</title>
</head>
<body>
<%  
 out.println("Hello Dinesh!!!!!!");
%>
</body>
</html>

Output on browser: Hello Dinesh!!!!!!

As you can see above, using JSP out object, String response is displayed on the browser when JSP served a request.

Copy implicitobjects folder on webapp directory of tomcat at
C:Program Files (x86)Apache Software FoundationTomcat 7.0webapps

and restart server and hit the following URL
http://localhost:8080/implicitobjects/index.jsp

 

Download Example.

 

<<Previous <<   || Index ||   >>Next >>
Previous
Next