Table of contents
1.
Introduction
2.
What are JSP Implicit Objects?
3.
Types of Implicit objects in JSP
3.1.
Request
3.2.
Response
3.3.
PageContext
3.4.
Session
3.5.
Application
3.6.
Config
3.7.
Out
3.8.
Page
3.9.
Exception
4.
Frequently Asked Questions
4.1.
Q1. What are the JSP implicit objects?
4.2.
Q2. What is explicit and implicit objects?
4.3.
Q3. How many implicit objects are there?
5.
Conclusion
Last Updated: Aug 29, 2024
Medium

JSP Implicit Objects

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

JavaServer Pages (JSP) support dynamic content on Web pages. Java code can be inserted into HTML pages by using JSP tags. 

JSPs are Java servlets designed to operate as an application user interface in Java web applications. In the world of Java programming, a servlet is a class used to extend the capabilities of servers that host applications that can be accessed via a request-response model of communication.

JSP Implicit Objects

JSP Implicit Objects are Java objects automatically made available to developers by the JSP Container on each page without any explicit declaration from developers. This is an essential component of the JSP application.

What are JSP Implicit Objects?

You can access Implicit Objects through scripting elements. The author of a JSP does not need to declare or instantiate implicit objects. JavaBeans and Servlets access them programmatically through the container, automatically instantiating them. This is why implicit objects are so named. The container parses these and inserts them into the generated servlet code. In other words, they are only available in JSP service methods and not in any declarations.

Whenever we create web applications, we need objects such as to request, response, config, context, session, etc. To get these objects, we have to write some Java code. Since the objects above are frequently required in web applications, JSP technology provides support for them in JSP Implicit Objects to alleviate developers' burdens.

All of these implicit objects are available for use directly inside JSP scriptlets. The JSP compiler generates the servlet program for every JSP file; the following nine implicit objects will be created in the _jspService() method.

Types of Implicit objects in JSP

Request

It is inbuilt in javax.servlet.http.HttpServletRequest. It has the sole purpose of getting the data entered on a JSP page from the previous JSP page. We often ask users for their details while dealing with JSP forms. This object is used to extract that information and pass it to another JSP page (action page) for validation.

Intro.html

<!DOCTYPE html>
<html>
    <head>
        <title>JSP implicit objects</title>
    </head>
    <body>
        <form action="new.jsp">
            Enter your name: <input type="text" name="name" /> <br />

            <input type="submit" value="confirm" />
        </form>
    </body>
</html>

new.jsp

<%@ page import = " java.util.* " %> 
<% 
	String name = request.getParameter("name");  
	out.print("Hey!!!"+name+" have a good day."); 
%>

Output

When we load the intro.html file in the browser it will ask for input, after that new.jsp file will get loaded according to the input.

It shows the user a Hello message, and getParameter() returns the Parameter value as a name from the user.

Response

An object from HttpServletResponse is used to modify or handle a response sent back to the client (browser) after processing a request.

intro.html

<!DOCTYPE html>
<html>
    <head>
        <title>implicit object</title>
    </head>
    <body>
        <form action="new.jsp">
            OPEN CODING NINJA'S

            <input type="submit" value="OPEN" /><br />
        </form>
    </body>
</html>

detail.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="ISO-8859-1" />
        <title>JSP response</title>
    </head>
    <body>
        <%  response.sendRedirect("https://www.codingninjas.com/"); %>
    </body>
</html>

Output

When we load the intro.html file in the browser it will show a button to open the details.jsp file.

This example shows how to use a response and its method sendRedirect. After clicking on open, users are redirected to https://www.codingninjas.com/.

PageContext

The javax.servlet.jsp uses this object as an implicit object when accessing the attributes for a page, request, application, or session.

intro.html

<!DOCTYPE html>
<html>
    <head>
        <title>implicit object</title>
    </head>
    <body>
        <form action="new.jsp">Enter your name: <input type="text" name="name" /> <input type="submit" value="Enter" /><br /></form>
    </body>
</html>

new.jsp

<!DOCTYPE html>
<html>
    <head>
        <title>PageContext</title>
    </head>
    <body>
        <%  String name = request.getParameter("name"); out.print("Hey!!!" + name); pageContext.setAttribute("User", name, PageContext.SESSION_SCOPE); %>
        <a href="next.jsp">next jsp page</a>
    </body>
</html>

next.jsp

<!DOCTYPE html>
<html>
    <head>
        <title>page context</title>
    </head>
    <body>
        <%  String name = (String) pageContext.getAttribute("User", PageContext.SESSION_SCOPE); out.print("Hey!!!" + name); %>
    </body>
</html>

Output

When we load the intro.html file in the browser it will ask for the input after entering the value and pressing the Enter button then new.jsp file will get loaded.

Now, when we click on the next jsp page it will load the next.jsp file.

As shown in the example above, new.jsp sets the attribute client in the Session scope and passes it to the next.jsp, which retrieves the attribute and displays the result.

Session

The session object is implicit under the javax.servlet.http.HttpSession package. This object is used to store user data used by other JSP pages as long as the user is still logged in to the page.

intro.html

<!DOCTYPE html>
<html>
    <head>
        <title>JSP implicit object</title>
    </head>
    <body>
        <form action="new.jsp">Enter your name:<input type="text" name="name" /> <input type="submit" value="Enter" /><br /></form>
    </body>
</html>

new.jsp

<!DOCTYPE html>
<html>
    <head>
        <title>session</title>
    </head>
    <body>
        <%  String name = request.getParameter("name"); out.print("Hey!!! " + name); session.setAttribute("Individual", name); %>
        <a href="next.jsp">nextjsp page</a>
    </body>
</html>

next.jsp

<!DOCTYPE html>
<html>
    <head>
        <title>session</title>
    </head>
    <body>
        <%  String name = (String) session.getAttribute("Individual"); out.print("Hey!!!" + name); %>
    </body>
</html>

Output

When we load the intro.html file in the browser it will ask for the input after entering the value and pressing the Enter button then new.jsp file will get loaded.

Now, when we click on the next jsp page it will load the next.jsp file.

Using this code, a session will be generated. Once the form is submitted, the form is returned to new.jsp, which is a session page. If we click the following JSP page link, we get to the next.jsp, which says Hey! Student.

Application

JSP uses the implicit Application object to obtain initialization parameters and share attributes across all pages. JSP is an implicit object of the ServletContext implementation of javax.servlet.ServletContext. All the JSP pages would have access to any attributes added by the application implicit object.

Application-wide initialization parameters and user data are obtained and maintained through this method.

new.jsp

<%@ page import="java.io.*,java.util.*" %> <!DOCTYPE html>
<html>
    <head>
        <title>Application Implicit</title>
    </head>
    <body>
        <% Integer count= (Integer)application.getAttribute("clicks"); if( count ==null || count== 0 ){ count = 1; } else{ count = count+ 1; } application.setAttribute("clicks", count); %>
        <h3>Number of page refresh:  <%= count %></h3>
    </body>
</html>

Output

An application and the method getAttribute are the objects used in the example. A given attribute name returns the object associated with that attribute.

Config

The object is of the type javax.servlet.ServletConfig. This service configuration object allows you to obtain servlet configuration information, like servlet context, servlet name, or configuration parameters.

next.jsp

<% 
    String servletname=config.getServletName(); 
	out.print("Name of servlet is: "+servletname);   
%>

new.xml

<web-app>
    <servlet>
        <servlet-name>jsp</servlet-name>
        <jsp-file>/next.jsp</jsp-file>
    </servlet>
    <servlet-mapping>
        <servlet-name>jsp</servlet-name>
        <url-pattern>/next</url-pattern>
    </servlet-mapping>
</web-app>

Output

Here, the servlet-mapping is performed by new.xml, and the servlet name gets printed in the output.

Out

This is the most commonly used implicit object provided by java.servlet.jsp.JspWriter. It is used to write content to the client (browser). You can use various methods to format output messages for display to a browser and deal with buffers.

new.jsp

<html>
    <head>
        <title>OUT OBJECT</title>
    </head>
    <body>
        <% out.print( "Hey Ninjas's" ); out.println( "How are you doing?" ); %>
    </body>
</html>

Output

Here is an example that uses the method out.println() to demonstrate out implicit objects. It prints the output on the screen.

Page

An implicit object under the java.lang.Object.class.Page class represents a current Servlet instance (a JSP page converted into a Servlet). The implicit object can be used instead of the actual one. This implicit object is rarely used and is useless when building JSP applications.

next.jsp

<html>
    <head>
        <title>Page object</title>
    </head>
    <body>
        <% String pageName = page.toString(); out.println("Page Name is " +pageName);%>
    </body>
</html>

Output

As a result of a successful compilation, a reference page is generated.

Exception

In exception handling, java.lang.Throwable.Exception implicit object is used to display error messages. Only pages that have set isErrorPage to true have access to this object.

intro.html

<html>
    <body>
        <form action="new.jsp">
            Enter Ist number:<input type="number" name="val1"/><br/>
            <br/>
            Enter IInd number:<input type="number" name="val2" /><br/>
            <br/>
            <input type="submit" value="result" />
        </form>
    </body>
</html>

new.jsp

<%@ page errorPage="next.jsp" %>
<% 
	String var1 = request.getParameter("one"); 
	String var2 = request.getParameter("two"); 
	int value1= Integer.parseInt(var1); 
	int value2= Integer.parseInt(var2); 
	int result= value1 / value2;
	out.print("Division is: "+ result); 
%>

next.jsp

<%@ page isErrorPage="true" %>
<h3>An exception occured please enter again</h3>
Exception is: <%= exception %>

Output

When we load the intro.html file in the browser it will ask for the input after entering the values and pressing the Divide button then new.jsp file will get loaded.

If there is no error, then the output will be displayed in the browser.

If we enter some invalid inputs, as shown below:

Then it will display an error message.

The HTML page in this example opens and requests input. After entering these numbers, new.jsp performs calculations, and if the page throws an error, next.jsp is invoked and throws java.langNumberFormatException and checks the data.

You can also read about the JSP Architecture and Lifecycle.

Frequently Asked Questions

Q1. What are the JSP implicit objects?

JSP implicit objects are predefined variables in JSP that provide access to various objects including request, response, session, and application.

Q2. What is explicit and implicit objects?

Explicit objects are explicitly created by developers, while implicit objects are automatically available in the environment, such as JSP implicit objects.

Q3. How many implicit objects are there?

In JavaServer Pages (JSP), there are nine standard implicit objects: request, response, pageContext, session, application, out, config, page, and exception.

Conclusion

The container creates an implicit object in JSP and is available directly. 
Since they are implicit objects, they can only be used in scriptlets and expressions tags because code written in scriptlets is executed in service methods, and code written in declaration tags is class-level.

Recommended Readings:

Live masterclass