2 Minutes To Code (β Version)

JSP - 2 minute tutorial

Introduction to 'out ' implicit object

If you have seen the earlier examples on jsp expressions , jsp scriptlets, jsp declarations etc., you would notice that all html tags in a jsp page end up verbatim in the corresponding webpage.

However, there is one more way to add content to jsp page, and that is using the implicit object out as shown in the example below. out is an implict object, which means that all jsp pages have access to this object and there is no need to declare or initialize it explicitly.

out is an instance of class javax.servlet.jsp.JspWriter. It is a reference to the output stream of the response to the http request. By using its methods print() and println(), bytes/characters can be sent to the http client.
			out.print("<h2 style=color:green >Welcome</h2>");          // Sends "Hi" and new line to response stream

The above code will create the following webpage :

Some commonly used methods of the "out" object are :
Method Deacription
append(...) Adds the given character to the output stream. Is overloaded.
clear() Clears the buffer. If even part of response has gone to client, then throws exception.
clearBuffer() Clears the current buffer. Unlike clear(), does not throw exception.
close() Closes the output stream. Methods write, print cannot be used henceforth.
flush() Sends the contents of buffer to the client.
newLine() Adds a new line to the output stram.
print(...) Sends char, string, objects etc to the output stream. Is overloaded.
println(...) Same as print(...) above. Also adds a new line to the output stream.
write(...) Same as print(...) above, except that it accepts only primitives as arguments. Is overloaded.

Other implicit objects available to all jsp pages e.g. request, response, session etc., are dealt with in the next few pages.