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
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!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>Insert title here</title> </head> <body> </body> </html> |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!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>Insert title here</title> </head> <body> <% String userName = request.getParameter("name"); %> <br> User name in request object is <%=userName%> </body> </html> |
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.
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!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>Insert title here</title> </head> <body> <% String userName = request.getParameter("name"); if(userName!=null) { session.setAttribute("sessionUserName", userName); application.setAttribute("applicationUserName", userName); } %> <br> User name in request object is <%=userName%> <br> User name in session object is <%=session.getAttribute("sessionUserName") %> <br> User name in application context object is <%=application.getAttribute("applicationUserName") %> </body> </html> |
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.

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.

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.

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.

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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!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>Insert title here</title> </head> <body> <% String userName = request.getParameter("name"); if(userName!=null) { session.setAttribute("sessionUserName", userName); application.setAttribute("applicationUserName", userName); pageContext.setAttribute("pageContextUserName", userName); pageContext.setAttribute("pageContextSessionUserName", userName,PageContext.SESSION_SCOPE); pageContext.setAttribute("pageContextApplicationUserName", userName,PageContext.APPLICATION_SCOPE); pageContext.setAttribute("pageContextRequestUserName", userName,PageContext.REQUEST_SCOPE); } %> <br> User name in request object is <%=userName%> <br> User name in session object is <%=session.getAttribute("sessionUserName") %> <br> User name in application context object is <%=application.getAttribute("applicationUserName") %> <br> User name in pageContext object is <%=pageContext.getAttribute("pageContextUserName") %> <br> User name in pageContext object with Session Scope is <%=pageContext.getAttribute("pageContextSessionUserName",PageContext.SESSION_SCOPE) %> <br> User name in pageContext object with Application Scope is <%=pageContext.getAttribute("pageContextApplicationUserName",PageContext.APPLICATION_SCOPE) %> <br> User name in pageContext object with Request Scope is <%=pageContext.getAttribute("pageContextRequestUserName",PageContext.REQUEST_SCOPE) %> </body> </html> |
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
- first take two parameters (string and an object)
- 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!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>Insert title here</title> </head> <body> <% String userName = request.getParameter("name"); String s=null; if(userName!=null) { session.setAttribute("sessionUserName", userName); application.setAttribute("applicationUserName", userName); pageContext.setAttribute("pageContextUserName", userName); pageContext.setAttribute("pageContextSessionUserName", userName,PageContext.SESSION_SCOPE); pageContext.setAttribute("pageContextApplicationUserName", userName,PageContext.APPLICATION_SCOPE); pageContext.setAttribute("pageContextRequestUserName", userName,PageContext.REQUEST_SCOPE); s=(String) pageContext.findAttribute("pageContextRequestUserName"); } %> <br> User name in request object is <%=userName%> <br> User name in session object is <%=session.getAttribute("sessionUserName") %> <br> User name in application context object is <%=application.getAttribute("applicationUserName") %> <br> User name in pageContext object is <%=pageContext.getAttribute("pageContextUserName") %> <br> User name in pageContext object with Session Scope is <%=pageContext.getAttribute("pageContextSessionUserName",PageContext.SESSION_SCOPE) %> <br> User name in pageContext object with Application Scope is <%=pageContext.getAttribute("pageContextApplicationUserName",PageContext.APPLICATION_SCOPE) %> <br> User name in pageContext object with Request Scope is <%=pageContext.getAttribute("pageContextRequestUserName",PageContext.REQUEST_SCOPE) %> <br> Page Context object Scope is <%=s%> </body> </html> |
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
- first with one argument(a string to identify attribute name)
- 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