implicit object out JSP Example

"out" is one of the 9 implicit objects, Java supports. It represents an object of javax.servlet.jsp.JspWriter, a subclass of character stream java.io.Writer. Any data given to implicit object out JSP goes as output stream which is sent to client as response. That is, Programmer uses out object to send data to client.

It is equivalent to PrintWriter object of Servlet or equivalent to System.out.println() but which prints at command prompt.

In Servlet, we create a PrintWriter (a subclass of java.io.Writer) object as follows.

PrintWriter out = response.getWriter();

In JSP, the out object is readily available without any effort to create. Here, out is an object of JspWriter (a subclass of java.io.Writer).

One small notable difference between JspWriter and PrintWriter (of Servlets) is that JspWriter can throw exceptions but PrintWriter does not. But functionally (purpose) both are one and the same.

Some methods of JspWriter are clear(), overloaded print() and println() methods, getBufferSize(), newLine() etc.

Following example on implicit object out JSP uses to send data to client.

File Name: UsingOut.jsp

<%
  out.println("Using out implicit object of JSP
"); // sending a string message int x = 10, y = 20; out.println("Product of " + x + " and " + y + " is " + x*y + "
"); // same syntax of System.out.println() out.println("See how much easy to use out object, no closing like out.close() is required"); %>

ima

Whatever, data is put within print()/println() methods of out object, goes to client. Whether you use print() or println(), it is required to put <br> for new line.

Leave a Comment

Your email address will not be published.