Introduction to JSP Objects
The objects in Java that is made available by the JSP container to the developers in each page which they can call directly without having to declare them explicitly are called JSP implicit objects which are also called as pre-defined variables and there are nine implicit objects that are supported by JSP namely Exception of type Throwable, config of type ServletConfig, out of type JspWriter, request of type HttpServletRequest, page of type object, application of type ServletContext, response of type HttpServletResponse, pageContext of type pageContext and session of type HttpSession.
Creating and Defining the Implicit Objects in JSP
- The JSP engine creates the JSP implicit objects during the translation of JSP to Servlet which is nothing but the phase of translation.
- There is no necessity to initialize and declare the JSP implicit objects because the creation of implicit objects happens inside the service method which enables us to use these JSP implicit objects directly within our script without the need for initialization and declaration.
Implicit Objects that are Supported by JSP
Given below are the implicit objects that are supported by JSP:
1. JSP Exception implicit object
The implicit object using which the exception can be printed is called Exception implicit object and this implicit object is used only in the pages containing error.
Example:
Program to demonstrate Exception implicit object.
Code:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" isErrorPage="true"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Exception implicit object</title>
</head>
<body>
<%String[] str={"welcome","to","JSP"};
out.println(str[3]);%>
<%= exception %>
</body>
</html>
Output:
In the above program, an array of strings is defined to store three strings and we are trying to access the string at index 3 which is not available. Hence Array Index out of bounds exception is raised.
2. JSP out implicit object
The implicit object using which the data can be written to the buffer is called out implicit object.
Example:
Program to demonstrate out implicit object.
Code:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>out Implicit object</title>
</head>
<body>
<% String str1="Welcome to";String str2="JSP";
out.println("The first and second string together is: " +str1);
out.println(str2);
%>
</body>
</html>
Output:
In the above program, two string variables are used to store two strings. Then out implicit object is used to print the string together as the output.
3. JSP config implicit object
The initialization parameter for a specific JSP page can be obtained by using the implicit object config which is created by the JSP container for each page.
Example:
Program to demonstrate config implicit object.
Code:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>config implicit object</title>
</head>
<body>
<% String name = config.getServletName();
out.println("The name of the servlet is: " +name);%>
</body>
</html>
Output:
In the above program, we are making use of config.getServletName() to obtain the name of the servlet.
4. JSP request implicit object
The request information like information of the header, parameter, remote address, content type, name of the server, port of the server, character encoding etc. can be obtained and to set, get or remove the attributes can be done using request implicit object.
Example:
Program to demonstrate exception implicit object.
Code:
Contents in Firstpart.html program:
<form action="Request.jsp">
<input type="text" name="String">
<input type="submit" value="Enter"><br/>
</form>
Contents in Request.JSP program:
<%
String str=request.getParameter("String");
out.print(str);
%>
Output:
In the above program, an html code to take the input from the user is written in one file. Then the request implicit object is used in program to obtain the input entered by the user in the second file.
5. JSP application implicit object
The initialization parameter from the configuration file can be obtained and to set, get or remove the attributes from the scope of application can be done by using the implicit object Application.
Example:
Program to demonstrate application implicit object.
Code:
Contents in Web.xml program:
<application>
<servlet>
<servlet-name>Welcome to JSP</servlet-name>
<jsp-file>/application.jsp</jsp-file>
</servlet>
<context-param>
<param-name>nameofthedriver</param-name>
<param-value>JSP Driver</param-value>
</context-param>
</application>
Contents in application.JSP program:
<%
String name=application.getInitParameter("nameofthedriver");
out.print("The name of the driver is:"+name);
%>
Output:
In the above program, an xml code is written in one file which consists of the name of the driver. Then application implicit object is used to obtain the name of the driver from the xml file in another file.
6. JSP response implicit object
The responses can be added or manipulated like directing one response to another resource or sending the error etc. can be done using the response implicit object.
Example:
Program to demonstrate response implicit object.
Code:
Contents in Firstpart.html program:
<form action="Response.jsp">
<input type="text" name="String">
<input type="submit" value="Enter"><br/>
</form>
Contents in Response.JSP program:
<%
response.sendRedirect("http://www.facebook.com");
%>
Output:
In the above program, an html code to take the input from the user is written in one file. Then the response implicit object is used in program to redirect the input entered by the user to a website in the second file.
7. JSP PageContext implicit object
The attributes can be set, get or removed from the scope of either page or request or session or application by using PageContext implicit object.
Example:
Program to demonstrate PageContext implicit object.
Code:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>PageContext implicit object</title>
</head>
<body>
<%pageContext.setAttribute("Message","Welcome to JSP",pageContext.PAGE_SCOPE);
String str = (String)pageContext.getAttribute("Message");
out.println("The message that is set using PageContext implicit object is: " +str);
%>
</body>
</html>
Output:
In the above program, the PageContext implicit object is used to set the three parameters, key, value and scope. Here the key is Message, value is Welcome to JSP and scope is page which can be accessed using PAGE_SCOPE. The value of the key is obtained by using get attribute of PageContext implicit method as the output.
8. JSP session implicit object
The information of the session or to set the attributes, get or remove the attributes can be obtained using Session implicit object.
Example:
Program to demonstrate session implicit object.
Code:
Contents in Session1.JSP program:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Session implicit object demo file 1</title>
</head>
<body>
<% session.setAttribute("Message","Welcome to JSP"); %>
<a href="Session2.jsp">Click the link to obtain the message</a>
</body>
</html>
Contents in Session2.JSP program:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Session implicit object demo file 2</title>
</head>
<body>
<% String msg = (String)session.getAttribute("Message");
out.println("The message from the Session1.jsp file is: " +msg);
%>
</body>
</html>
Output:
In the above program, code is written to store a message using session implicit object in one file which can be obtained by using session implicit object in another file.
9. JSP page implicit object
The reference of the servlet class that is auto generated is assigned to page implicit object in JSP.
Example:
Program to demonstrate page implicit object.
Code:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Page implicit object</title>
</head>
<body>
<% String str = page.toString();
out.println("The name of the page is: " +str);%>
</body>
</html>
Output:
In the above program, page.toString() method is used to display the name of the JSP page.
Recommended Articles
This is a guide to JSP Objects. Here we discuss the introduction, creating and defining the implicit objects in JSP and implicit objects that are supported by JSP. You may also have a look at the following articles to learn more –
6 Online Courses | 12 Hands-on Projects | 53+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses