Updated June 7, 2023
Introduction to JSP Objects
The objects in Java that are made available by the JSP container to the developers on each page, which they can call directly without having to declare them explicitly, are called JSP implicit objects, which are also called pre-defined variables, and nine implicit objects 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, the 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 translation phase.
- 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 JSP supports
Given below are the implicit objects that JSP supports:
1. JSP Exception implicit object
Developers use the Exception implicit object to print exceptions, specifically applying it on the pages that contain errors.
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, we define an array of strings to store three strings. However, when we attempt to access the string at index 3, which is unavailable, it raises an Array Index Out-of-Bounds Exception.
2. JSP out implicit object
The output implicit object is the implicit object that utilizes the data to be written to the buffer.
Example:
Program to demonstrate our 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 our implicit object prints the string together as the output.
3. JSP config implicit object
The initialization parameter for a specific JSP page can be obtained using the implicit object config created by the JSP container for each page.
Example:
Program to demonstrate implicit config 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 use config.getServletName() to obtain the servlet’s name.
4. JSP requests 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 setting, getting, or removing the attributes can be done using an implicit request 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 the 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 setting, getting, or removing the attributes from the application’s scope can be done 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 the 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 containing the driver’s name. Then the application’s implicit object is used to obtain the driver’s name from the XML file in another.
6. JSP response implicit object
The responses can be added or manipulated, like directing one reaction to another resource or sending the error, etc., 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, in the program, we use the response implicit object 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, request, session, or application 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 sets the three parameters, key, value, and scope. Here the key is Message, the value is Welcome to JSP, and scope is a page that can be accessed using PAGE_SCOPE. We obtain the value of the key by using the getAttribute method of the PageContext implicit object, resulting in the output.
8. JSP session implicit object
The session’s information or to set the attributes, get or remove the details 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, we write code to store a message using a session implicit object in one file, and we can obtain it using a session implicit object in another file.
9. JSP page implicit object
The JSP assigns the reference of the auto-generated servlet class to the page implicit object.
Example:
Program to demonstrate implicit page 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, we use the page.toString() method to display the name of the JSP page.
Recommended Articles
We hope that this EDUCBA information on “JSP Objects” was beneficial to you. You can view EDUCBA’s recommended articles for more information.