JSP Implicit Objects

Implicit Objects:

Implicit Objects are the ones which are available to the users which can be used without declaration. These pre-defined or implicit variables are the ones which are similar to our conventional JAVA objects which we can be used in JSP to control and modify the flow of functioning.

 

In JSP we have nine Implicit Objects as below:

Object Type
out JspWriter à This is used to output the data to the End User.
request HttpServletRequest à The request object where we request data.
response HttpServletResponse à The response object which we get the response from the request.
config ServletConfig à This configuration object is mainly used in servlet for its construction.
application ServletContext àThis context object is used in the context of the application.
session HttpSession à This is mainly used in web based applications where the time and session of an application plays a key.
pageContext PageContext à Used in performance increase of JspWriters.
page Object à This can be used to call methods defined by servlet classes.
exception Throwable à This is like the JAVA exception classes which can be used for tapping of errors.

Out Implicit Object:

This is the Print Writer object which is used to write data to buffer and to the output of client. This is primarily an object of JSP Writer. This allows us to access Servlet output stream. The output which needs to be sent to the client (browser) is passed through this object. In simple words out implicit object is used to write content to the client.

 

Please find the below example for better understanding.

PrintWriter out=response.getWriter();

 

But in JSP we don’t have to write this code, wherein we can write as below highlighted:

This has been explained with a simple doubler array program.

We have used out.println() method.

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Passing Array to Method</title>
</head>
<body>
 
<%!void doubler(int a[]) {
 
for (int i = 0; i < a.length; i++) {
a[i] *= 2;
}
}%>
 
<%
int array[] = { 1, 2, 3, 4, 5 };
 
out.println("Before calling method" + "<br>");
for (int i = 0; i < array.length; i++) {
out.println("Array [" + i + "] = " + array[i] + "<br>");
}
 
doubler(array);
 
out.println("After calling method" + "<br>");
 
for (int i = 0; i < array.length; i++) {
out.println("Array [" + i + "] = " + array[i] + "<br>");
}
%>

</body>
</html>

Output of the above code:

Implicit

In the above example we have only the use of out.println method alone.
But there are other methods which are available for OUT Implicit objects as below:

void print()
void newLine()
void clear()
void clearBuffer()
void flush()
boolean isAutoFlush()
int getBufferSize()
int getRemaining()

void print(): This method is similar to that of println() method just for the fact that the writes the value in the same line even if there are two print() methods used.

Please find the below example where we can find it more clear.

out.print(“William Shakespeare”);
out.print(“Charles Dickens”);

 

The output of the above piece of code will be like below:

William ShakespeareCharles Dickens

If we can see there will not be a space or a new line character introduced between them which is just similar to normal flat file content.

void newLine():

This method adds a new line to the output like hitting an Enter button and then writing the data.

out.print(“William Shakespeare”);
out.newLine();
out.print(“Charles Dickens”);

 

The output of the above piece of code will be like below:

William Shakespeare

Charles Dickens

 

void clear(): 

This method clears the buffer before the buffer writes the output content to the client or server.

The clear method can be used like below:

Out.clear();

In the above example for out.println() method place this out.clear() after the second for loop before the end of the program (ie. %>). You will be able to find out the meaning of this method.

void clearBuffer():

This method is similar to the clear() method. The clearbuffer() method can be used for an already flushed buffer***  too. But if we use a clear() method for a flushed buffer then we will end up in an exception. So this clear() and clearBuffer() method is used to clear the content in the buffer based on the above parameters.

Flushing and Flushed Buffer:

But here we have used as a flushed buffer.  What is a flushed buffer mean. As we know buffer is a temporary storage place where the data is kept before going to the client.

So flushing means the data processed so far can go to the client even if the rest of the page isn’t done yet. It’s useful when you have a lot of data to send like in a download.

For example, we can render an html Div with an animated image (like a hour glass) showing the status of the page downloading in process, while the page is still being buffered and once the content is fully buffered, you may append some javascript to hide the hour glass and render the content to the screen once the processing is finished.

void flush():

This method too empties the buffer just like clear() method but it forces the buffer to write the content to the output before flushing it, which means whatever is there in buffer would be written to the client screen before clearing the buffer.

boolean isAutoFlush() :  It returns a Boolean value true/false. It is used to check whether the buffer is automatically flushed or not.

int getBufferSize(): This method returns the size of output buffer in bytes.

int getRemaining(): It returns the number of bytes remaining before hitting the buffer overflow condition.

Posted on July 12, 2014 in Java Server Pages

Share the Story

Leave a reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to Top