What is JSP Implicit Object?

In the previous tutorial, we have seen the Different Scriptlet Tags that are used in JSP pages. In this tutorial, we will cover all the Implicit Objects available in JSP.

What is Implicit Object in JSP?

The Implicit Objects are Java Objects that are created by the Container in JSP and we as a developer call them without having been explicitly declared.

  • It is created during the int the first step of the Life Cycle i.e Translation phase of JSP.
  • Web Container creates Implicit Objects.
  • They are also known as Pre-defined Variables.

Implicit Objects

There are 9 types of Implicit Objects in Container.

Let’s see every Object in detail

out Implicit Object

To write data JSP provides implicit Object called out. It is the Object of Type JspWriter.

  • It is used to write data and to send output to the client.
  • out is present in javax.servlet.jsp.jspWriter class.
  • It is Similar to Servlet where we have used printWriter object.

Example

<%@ 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 name1="Ross";
String name2="Joey";
String name3="Chandler";
out.println("name1: "+name1);
out.println("<br>name1: "+name2);
out.println("<br>name2: "+name3);
%>
</body>
</html>

Output

request Implicit Object

For each request of JSP to the server, the web container creates the object of the request.

  • It is an object of the type HttpServletRequest.
  • For every request, the web container creates the object of the request.
  • It uses getParameter() method to retrive request parameter.

Example

In this example, index.html request is sent, and then it is processed and sent to the requestImplicitobj.jsp page.

index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="requestImplicitobj.jsp">
<input type="text" name="university">
<input type="submit" value="Click" id="button-1"/>
</form>
</body>
</html>

requestImplicitobj.jsp

<%@ 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 university=request.getParameter("university");
out.println("University "+university);
%>
</body>
</html>

Output

response Implicit Object

It is an implicit object created by the web container. It creates an object to represent the response to the client.

  • It is of the type javax.servlet.http.HttpServletResponse.
  • The object of response is created.
  • It is used to send a response to other resources.

Example

In this example, we are redirecting the page to https://codedec.com/

indexpage.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="responseobj.jsp">
<input type="text" name="name">
<input type="submit" value="Go" id="button-1"/>
</form>
</body>
</html>

responseobj.jsp

<%@ 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>
<%
response.sendRedirect("https://codedec.com/");
%>
</body>
</html>

Output

config Implicit Object

The config object is used to get the initialization parameter for a JSP page.

  • config is the object of java.servlet.servletConfig.
  • It is used to get the initialization parameter from the web.xml file.
  • It is created by a web container for each page.

Example

In this example, we will get the Servlet name from the web.xml file.

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns="http://java.sun.com/xml/ns/javaee"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  id="WebApp_ID" version="3.0">
  <display-name>OurFirstJSPProgram</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>ABCServlet</servlet-name>
    <jsp-file>/config.jsp</jsp-file>
  </servlet>
  <servlet-mapping>
    <servlet-name>ABCServlet</servlet-name>
    <url-pattern>/config</url-pattern>
  </servlet-mapping>
</web-app>

config.jsp

<%@ 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 nameOfServlet=config.getServletName();
out.println("Name of Servlet is "+nameOfServlet);
%>
</body>
</html>

Output

application Implicit Object

It is an implicit object that is used to get the context information and attributes in JSP.

  • It is an object of javax.servlet.ServletContext.
  • It is created only once by the web container.
  • It is also used to get the initialization parameter from the web.xml file.

Example

In this example, the application attribute is used to get the context path.

<%@ 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 app=application.getContextPath();
out.println("Context Path "+app);
%>
</body>
</html>

Output

session Implicit Object

This implicit object is used to get,  set, and remove an attribute of Session Object in JSP.

  • It is an object of HttpSession.
  • As we know, the session object is used to keep the client track between client requests.

Example

In this example, we are setting the attribute variable and fetching it in another page.

session1.jsp

<%@ 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>
<%
session.setAttribute("user","Hello JSP");
%>
<a href="session2.jsp">Click for user</a>
</body>
</html>

session2.jsp

<%@ 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 user=(String)session.getAttribute("user");
out.println("User is "+user);
%>
</body>
</html>

Output

pageContext Implicit Object

pageContext is an object of PageContext class that provides useful context information for when JSP technology used in Servlet Environment.

  • It is an object of type PageContext class.
  • It is used to set, get, remove an attribute from a page, request, session, application.

Example

In this example, we will be setting the attribute using pageContext object and we will get the values of the attribute using pageContext.

<%@ 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>
<%
pageContext.setAttribute("name","Nicolas",pageContext.PAGE_SCOPE);
pageContext.setAttribute("RollNo","21",pageContext.REQUEST_SCOPE);
pageContext.setAttribute("Year","2020",pageContext.SESSION_SCOPE);
pageContext.setAttribute("Occupation","Scientist",pageContext.APPLICATION_SCOPE);
String name=(String)pageContext.getAttribute("name");
String roll=(String)pageContext.getAttribute("RollNo",pageContext.REQUEST_SCOPE);
String year=(String)pageContext.getAttribute("Year",pageContext.SESSION_SCOPE);
String Occupation=(String)pageContext.getAttribute("Occupation",pageContext.APPLICATION_SCOPE);
out.println("Using PAGE_SCOPE pageContext Implicit :"+name);
out.println("<br><br>Using REQUEST_SCOPE pageContext Implicit :"+roll);
out.println("<br><br>Using SESSION_SCOPE pageContext Implicit :"+year);
out.println("<br><br>Using APPLICATION_SCOPE pageContext Implicit :"+Occupation);
%>
</body>
</html>

Output

page Implicit Object

The page object is assigned to the reference of the generated Servlet class.

  • It is an object of a type Object class.
  • It acts as a reference for the current JSP page.

Example

In this example, we will be using the page object to get the page name.

<%@ 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 nameOfPage=page.toString();
out.println("Page Name is :"+nameOfPage);
%>
</body>
</html>

Output

exception Implicit Object

As we know, Exception is an event that arises during the execution of a program that needs to be handled on the JSP page for that we use the exception implicit object.

  • It is a type of throwable class.
  • It is used for exception handling in JSP.
  • It should be used in the error page.

Example

In this example, we are taking a simple exception example of divide by zero exception.

<%@ 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>
<%@ page isErrorPage="true" %>
<%
int a=10;
int b=0,c;
c=a/b;
out.println(c);
%>
<%= exception %>
</body>
</html>

Output

Thus these are the Implicit Object in JSP I hope you might have a clear understanding of Implicit Object now.

In the next article, we shall see Directives in JSP like Page Include and Taglib with a sample example.