JSP Implicit Objects

Introduction of Implicit Objects in JSPs

In the previous tutorial, we have seen JSP page and the include directive, in this tutorial, we will try to understand the scopes in JSP and the Context object in JSPs i.e. implicit objects. We have previously learned that JSPs are the form of Servlets, Tomcat is converting JSPs into Servlets behind the scenes. There are few differences between them, the way of accessing context objects in JSPs are different from the servlet.

 

Request Scope in JSPs

Request Scope as we have learned in the servlets can only be accessible on the same page. Let’s try to understand with an example.

Request Scope Example

First, we will create a new JSP with name “objects .jsp”(we have learned it in our previous JSP tutorial to create a JSP file) the new file will look like this

The user will pass parameter through URL, and we will display it in the body part of above JSP code.

Explanation:

The body part after editing our code will look like this.

Notice the above source we have used the request object which is not defined anywhere in the given source; every JSP page has this implicit object which we can use, try to run the file and analyze the output. The variable userName will have the null value set for the first time. Now try to pass the query parameters as “?name= John Doe” at the end of the URL as shown below.

1 - JSP passing query parameter

Session and the Context Scopes in JSPs

Just like request object we also have the session object and the context predefined in JSP, the variable name for session object is  “session” but the variable name for the context object is a little bit different, and that is “application” we can make use of them as follows.

First Test Run:

You know the properties of the session and the context object, if not you can see out Servlet tutorial on the session and the context object. Letts analyze them by running our source. The first run will look like this without query parameter.

Session and Context objects first run giving null
Session and Context objects first run giving null

Test run with query parameter:

After passing query parameter like this “?name=JohnDoe ” in the URL and reloading the page will give the output like this.

Session and Context objects with Query Parameters
Session and Context objects with Query Parameters

Test run without query parameter:

When we remove the query parameter that we have just added from the URL and reload Or refresh the page, the page will give the output like this.

Ilustration of the Session and Context objects without Query Parameters
Illustration of the Session and Context objects without Query Parameters

Test run on another browser:

Now copy and paste the given URL without the query string in another browser and analyze the output, reloading the page will give the output like this.

Ilustration of Context onbject in JSP
Illustration of Context object in JSP

Page Context Object in JSP

To set an attribute for the user, we also have another object called page context object. This object provides us another scope for us; we can set attribute by using the setAttribute() function of the pageContext object.

Why this new pageContext object in JSP?

By using the pageContext object we can setAttribute for the session object and the context object, we can also set the attribute for the request object with the help of this pageContext object. It means that by using one object we can set multiple attribute values. To illustrate this concept we have an example below.

Example of pageContext object:

setAttribute() function in Page Context object

Notice the above-given example of the pageContext object, with the pageContext object we have used two kinds of setAttribute() functions

  1. first take two parameters (string and an object)
  2. then take three parameters (a string an object and an integer)

To define multiple attributes for the user with the same object we have used the setAttribute function which is taking three parameters, each with the different string to identify uniquely.

findAttribute() function in Page Context object

There could be a case where you don’t know the scope of your attribute. In this case, we have another method inside the Page Context object, and that is “findAttribute()” function there is an example shown below to demonstrate its use.

removeAttribute() function in Page Context object:

There could also be a case where you want to remove an attribute from the scope. In this case, we have another method inside the Page Context object, and that is “removeAttribute()”, it is available for us with the two signatures

  1. first with one argument(a string to identify attribute name)
  2. second with two arguments (string an object and an integer)

With the help of page context object, we can include another resource in our JSP page, and we can also forward the request to another resource, to forward the request we use pageContext.forward(“resourceName”) method. In the next tutorial, you will learn about init params.

Summary

In this tutorial, we have accomplished following things.

  • Request Scope in JSP
  • Session scopes in JSP
  • Application scope in JSP
  • Page Context in JSP
  • Example of Scopes in JSP

Leave a Reply

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