< Prev
Next >



JSP out Object





JSP inbuilt out object of type JspWriter(a buffered version of PrintWriter) which is used to write a message or a value to be displayed in the web page sent back to the client. In other words, out object is used to send a message or output to the client.


out implicit object is mainly used in JSP scriplet tag <% %> to write a value back to the user. In JSP expressions, out object is not needed because JSP expression yields a value which is automatically displayed back to the user.



Some useful methods available to out object.


The methods in the table below are part of javax.servlet.jsp.JspWriter class. Hence,these methods are available to the JSP implicit out object.

Methods Description
void print(int i) This method prints an int.
void print(char c) This method prints a char.
void print(char []) This method prints a char.
void print(boolean b) This method prints a boolean.
void print(float f) This method prints a float.
void print(double d) This method prints a double.
void print(Object ob) This method prints an object.
void print(String str) This method prints a String.
void println(int i) This method prints an int and terminates the line.
void println(char c) This method prints a char and terminates the line.
void println(char []) This method prints a char array and terminates the line.
void println(boolean b) This method prints a boolean and terminates the line.
void println(float f) This method prints a float and terminates the line.
void println(double d) This method prints a double and terminates the line.
void println(Object ob) This method prints an object and terminates the line.
void println(String str) This method prints a String and terminates the line.
void print(int x) This method prints an int and terminates the line.
void clear() This method clears the content of buffer.
void flush() This method flushes the buffer.





Using JSP out object to print values/message.


In this example, we will print the values of primitive variables and a String object using JSP's inbuilt out object.

Out.jsp
<html>

<head>
<title>JSP inbuilt out object </title>
</head>

<body>

<h1>Using the JSP out object to print values.</H1>
<%
int i = 100;
char c = 'c';
short s = 10;
double d =2.5;
float f = 10.5f;
String str = "What a pleasant day it is!";
%>
Displaying value in an int  : <% out.println(i); %> <br/>
Displaying value in a char  : <% out.println(c); %> <br/>
Displaying value in a short : <% out.println(s); %> <br/>
Displaying value in a float : <% out.println(f); %> <br/>
Displaying value in a double: <% out.println(d); %> >% out.println("<br/>"); %>

Displaying value in a String : <% out.println(str); %>
</body>

</html>



executing this JSP page displays the values present in primitive variables like int, short, char, float, double and even an String object, using JSP's inbuilt out object.






Difference between JSPWriter and PrintWriter


JSP's implicit out object(a type of JspWriter class) writes a message or a value in an in-memory buffer. This buffer is flushed when it is full, or when the JSP page has reached the end of its execution and its content is written to the response's PrintWriter. But using PrintWriter does not have any buffer, hence it directly writes a message or values in a response to user and it doesn't wait for any buffer to be filled or until the JSP's end is reached.


In the next example, we will write a message using JSP's inbuilt out object of JspWriter class and we will write a message/output using PrintWriter. The message printed using PrintWriter will always be printed prior than message printed using out object of JSPWriter class, irrespective of its placement in JSP.

Out2.jsp
<html>

<head>
<title>JspWriter vs PrintWriter </title>
</head>

<body>

< %@ page import ="java.io.PrintWriter" %>

<h1>Using the JSP out object to print values.</h1>
<% out.println("Hello from JSP out object"); %>

<br/>
<br/>

<h1>Using the PrintWriter object to print values.</h1>
<% PrintWriter pw = response.getWriter();
pw.println("Hello using PrintWriter"); %>

Thanks for reading and best wishes for your future.

</body>

</html>



executing this JSP page shows you how PrintWriter prints a message/output faster than a message printed by inbuilt out object of JspWriter class.





Please subscribe our social media channels for notifications, we post a new article everyday.

Decodejava Google+ Page Decodejava Facebook Page  DecodeJava Twitter Page

Coming Next
-
C++ and Python

Ad2