JSP implicit objects OUT with example - Java @ Desk

Saturday, December 21, 2013

JSP implicit objects OUT with example

JSP implicit objects OUT with example

In our last posts, we have learned
JSP implicit objects REQUEST with example,
JSP Implicit Object CONFIG with example ,
JSP Implicit Object APPLICATION with example ,
JSP Implicit object session with example ,
JSP implicit object PageContext with example,
JSP implicit object RESPONSE with example,

The "out" implicit variable of a JSP implementation is of javax.servlet.jsp.JspWriter class type. The data on the JSP page is written using the JspWriter object that is referenced by the implicit variable "out" which is initialized automatically using methods in the PageContext objects.

In all there are 9 implicit JSP objects.

This object is used to print the output content to the client response on browser.

Below are some of the methods used with object out :
1) print(String s) - Print a string. If argument is null, then "null" is printed in the client response.
2) println(String x) - Print a String and then terminate the line.
3) newLine() - Write a line separator. The line separator string is defined by the system property line.separator, and is not necessarily a single newline ('\n') character.
Example for JSP implicit object out below:
 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"  
   pageEncoding="UTF-8"%>  
 <%@ page import="java.util.Date" %>  
 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
 <html>  
 <head>  
 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">  
 <title>Implicit Object Out Example</title>  
 </head>  
 <body>  
      <%-- out object example --%>  
      <h4>Implementation of JSP Implicit Object 'out'</h4>  
      <strong>Current Time is </strong>: <% out.print(new Date()); %><br><br>  
      <strong>out.print(String s) </strong>: <% out.print("Print a string."); %><br>  
      <strong>out.println(String s) </strong>: <% out.println("Print a String and then terminate the line."); %><br>  
      <%out.newLine(); %>  
      <% out.println("The difference between print(String s) & println(String s) method is that the second one terminates the line."); %>  
 </body>  
 </html>  


Output is :

Implementation of JSP Implicit Object 'out'
Current Time is : Sun Dec 22 11:38:36 IST 2013

out.print(String s) : Print a string.
out.println(String s) : Print a String and then terminate the line. 
The difference between print(String s) & println(String s) method is that the second one terminates the line. 







No comments:

Post a Comment