JSP Implicit Objects

This article is a series of JSP Tutorial. In this article, we will learn what are JSP implicit objects with an example.

The JSP implicit objects are the Java objects that the JSP Container makes available to the developers in each page and the developer can call them directly without being explicitly declared. JSP Implicit Objects are also called pre-defined variables.
These implicit objects are created by container-generated statements at the beginning of the _jspService() method and are assigned predetermined names that are the same in all JSP pages.
There are total 9 implicit objects available in JSP.

Implicit Objects and their Types

Implicit ObjectType
requestjavax.servlet.http.HttpServletRequest
responsejavax.servlet.http.HttpServletResponse
pageContextjavax.servlet.jsp.PageContext
sessionjavax.servlet.http.HttpSession
applicationjavax.servlet.ServletContext
outjavax.servlet.jsp.JspWriter
configjavax.servlet.ServletConfig
pagejava.lang.Object
exceptionjavax.servlet.jsp.JspException

Implicit Objects in JSP Page

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" isErrorPage="true" %>
    
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <%= request %>
 
    <%= response %>
 
    <%= pageContext %> 
 
    <%= session %> 
 
    <%= application %> 
 
    <%= out %>
  
    <%= config %> 
 
    <%= page %> 
 
    <%= exception %>
 
</body>
</html>
Let's briefly look into each implicit objects.

1. The request Object

The request object is an instance of a javax.servlet.http.HttpServletRequest object. Each time a client requests a page the JSP engine creates a new object to represent that request.
The request object provides methods to get the HTTP header information including form data, cookies, HTTP methods etc.

2. The response Object

The response object is an instance of a javax.servlet.http.HttpServletResponse object. Just as the server creates the request object, it also creates an object to represent the response to the client.
The response object also defines the interfaces that deal with creating new HTTP headers. Through this object, the JSP programmer can add new cookies or date stamps, HTTP status codes, etc.

3. The out Object

The out implicit object is an instance of a javax.servlet.jsp.JspWriter object and is used to send content in a response.
The initial JspWriter object is instantiated differently depending on whether the page is buffered or not. Buffering can be easily turned off by using the buffered = 'false' attribute of the page directive.
The JspWriter object contains most of the same methods as the java.io.PrintWriter class. However, JspWriter has some additional methods designed to deal with buffering. Unlike the PrintWriter object, JspWriter throws IOExceptions.

4. The session Object

The session object is an instance of javax.servlet.http.HttpSession and behaves exactly the same way that session objects behave under Java Servlets.
The session object is used to track client session between client requests.

5. The application Object

The application object is a direct wrapper around the ServletContext object for the generated Servlet and in reality an instance of a javax.servlet.ServletContext object.
This object is a representation of the JSP page through its entire lifecycle. This object is created when the JSP page is initialized and will be removed when the JSP page is removed by the jspDestroy() method.
By adding an attribute to an application, you can ensure that all JSP files that make up your web application have access to it.

6. The config Object

The config object is an instantiation of javax.servlet.ServletConfig and is a direct wrapper around the ServletConfig object for the generated servlet.
This object allows the JSP programmer access to the Servlet or JSP engine initialization parameters such as the paths or file locations etc.

7.The pageContext Object

The pageContext object is an instance of a javax.servlet.jsp.PageContext object. The pageContext object is used to represent the entire JSP page.
This object is intended as a means to access information about the page while avoiding most of the implementation details.
This object stores references to the request and response objects for each request. The application, config, session, and out objects are derived by accessing attributes of this object.
The pageContext object also contains information about the directives issued to the JSP page, including the buffering information, the errorPageURL, and page scope.

8. The page Object

This object is an actual reference to the instance of the page. It can be thought of as an object that represents the entire JSP page.
The page object is really a direct synonym for this object.

9. The exception Object

The exception object is a wrapper containing the exception thrown from the previous page. It is typically used to generate an appropriate response to the error condition.

In this next article, we will learn an important topic that is JSP Directives with an example.
\

Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours


Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course

Comments