JSP - Implicit Objects

There is another concept of JSP, which are Java objects made available by the developers for each page, allowing developers to call them directly without any explicit declaration. JSP implicit objects are essential components used in this regard. In this chapter, you will learn how to deal with implicit objects and implement them.

Implicit Objects of JSP

The JSP engine produces these objects during the translation phase (i.e., at the time of translation from JSP to Servlet). They are being formed within the service method so that JSP developers can use them directly in Scriptlet without declaration and initialization.

There are a total of nine implicit objects supported by the JSP. These are:
  1. out: javax.servlet.jsp.JspWriter
  2. request: javax.servlet.http.HttpServletRequest
  3. response: javax.servlet.http.HttpServletResponse
  4. session: javax.servlet.http.HttpSession
  5. application: javax.servlet.ServletContext
  6. exception: javax.servlet.jsp.JspException
  7. page: java.lang.Object
  8. pageContext: javax.servlet.jsp.PageContext
  9. config: javax.servlet.ServletConfig

The brief information about the implicit objects are given below:

The out Implicit Object

  • An out object is an implicit object for writing data to the buffer and sending output as a response to the client's browser.
  • The out implicit object is an instance of a javax.servlet.jsp.jspWriter class.
  • You will learn more about the various concepts of the out Object in subsequent chapters.
Example (HTML file):
<!DOCTYPE html>
<html>
    <head>
        <title>Please insert a User name and a password</title>
    </head>
    <body>
        <% out.println("Today's date-time: "+java.util.Calendar.getInstance().getTime()); %>
    </body>
</html>
Output:
Today's date-time: Nov 01 12:10:05 IST 2020

The request Implicit Object

  • A request object is an implicit object that is used to request an implicit object, which is to receive data on a JSP page, which has been submitted by the user on the previous JSP/HTML page.
  • The request implicit object used in Java is an instance of a javax.servlet.http.HttpServletRequest interface where a client requests a page every time the JSP engine has to create a new object for characterizing that request.
  • The container creates it for every request.
  • It is used to request information such as parameters, header information, server names, cookies, and HTTP methods.
  • It uses the getParameter() method to access the request parameter.

Here is an example of a JSP request implicit object where a user submits login information, and another JSP page receives it for processing:

Example (HTML file):
<!DOCTYPE html>
<html>
    <head>
        <title>Please insert a User name and a password</title>
    </head>
    <body>
        <form action="login.jsp">
            Please insert Username: <input type="text" name="u_name" /> <br />
            Please insert Password: <input type="text" name="passwd" /> <br />
            <input type="submit" value="Submit Details" />
        </form>
    </body>
</html>
Example (login.jsp):
<%@ page import = " java.util.* " %>
<% 
String username = request.getParameter("u_name"); 
String password = request.getParameter("passwd"); 
out.print("Name: "+username+" Password: " +passwd);
%>

The lesson "JSP Request" contains more detailed information about "The request Implicit Object" and its methods.

The response Implicit Object

  • A response object is an implicit object implemented to modify or deal with the reply sent to the client (i.e., browser) after processing the request, such as redirect responding to another resource or an error sent to a client.
  • The response implicit object is an instance of a javax.servlet.http.HttpServletResponse interface.
  • The container creates it for every request.
  • You will learn more about the various concepts of the request and response in subsequent chapters.

The session Implicit Object

  • A session object is the most commonly used implicit object implemented to store user data to make it available on other JSP pages until the user's session is active.
  • The session implicit object is an instance of a javax.servlet.http.HttpSession interface.
  • This session object has different session methods to manage data within the session scope.
  • You will learn more about the use of the session in subsequent chapters.

The application Implicit Object

An application object is another implicit object implemented to initialize application-wide parameters and maintain functional data throughout the JSP application.

The exception Implicit Object

  • An exception implicit object is implemented to handle exceptions to display error messages.
  • The exception implicit object is an instance of the java.lang.Throwable class.
  • It is only available for JSP pages, with the isErrorPage value set as "True". This means Exception objects can only be used in error pages.
Example (HTML file):
<!DOCTYPE html>
<html>
    <head>
        <title>Enter two Integers for Division</title>
    </head>
    <body>
        <form action="submit.jsp">
            Insert first Integer: <input type="text" name="numone" /><br />
            Insert second Integer: <input type="text" name="numtwo" /><br />
            <input type="submit" value="Get Results" />
        </form>
    </body>
</html>
Example (submit.jsp):
<%@ page errorPage="exception.jsp" %>
  
<% 
String num1 = request.getParameter("numone"); 
String num2 = request.getParameter("numtwo"); 
int var1= Integer.parseInt(num1);
int var2= Integer.parseInt(num2);
int r= var1 / var2;
out.print("Output is: "+ r);
%>
Example (exception.jsp):
<%@ page isErrorPage='true' %>
  
<% 
out.print("Error Message : ");  
out.print(exception.getMessage());
%>

The page Implicit Object

A page object is an implicit object that is referenced to the current instance of the servlet. You can use it instead. Covering it specifically is hardly ever used and not a valuable implicit object while building a JSP application.

<% String pageName = page.toString();
out.println("The current page is: " +pageName);%>

The config Implicit Object

A config object is a configuration object of a servlet that is mainly used to access and receive configuration information such as servlet context, servlet name, configuration parameters, etc. It uses various methods used to fetch configuration information.

You will learn more about how to set and use config objects in later chapters.


Scroll Back to Top