JSP

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
Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

2 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

2 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

2 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

2 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

2 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

3 years ago