"implicit" Objects in JSP


Implicit Objects In JSP:

The JSP implicit objects are used for the making the page dynamic. It is created by the web container. These implicit objects are java objects that implement the Servlet and JSP API. These are predefined objects that are accessible to all JSP pages. These objects are called implicit objects, because you don't need to instantiate these objects.

Features of Implicit objects:

  • JSP implicit objects allow developers to access services and resources provided by the web container.
  • JSP implicit objects are used to generate the dynamic content of the web page.
  • JSP implicit objects are available to all JSP pages.
  • JSP implicit objects help in handling the HTML parameters, forward a request to a web component, and include the content of the component.

Types of Implicit Objects:

There are nine implicit objects which are generally used, these are:

The Request Object: Request parameter are ed as parameter to the -jspService() method when a client request is made. You can use the request objects in a JSP page similar to using these in Servlet.The request implicit object is generally used to get request parameters, request attributes, header information and query string values.

The Response Object:  The response objects is used to carry the response of a client request after the -jspService() method is executed. The response  object is of the javax.servlet.http.HttpServletResponse type.

The Out Object: The out implicit object provides access to the servlet's output stream. The out implicit object is used to write the output content. It represents the output content to be sent to the client.   

The Page Object: The page object refers to the Servlet of the JSP page processing the current request, and is of  the java.lang.object type.It represents the current JSP page. That is, it serves as a reference to the java Servlet object that implements the JSP page on which it is accessed.It is rarely used in a document. It cannot be used directly to call the Servlet methods.

The pageContext Object: It represent the context of the current JSP page. It provides a single API to manage the various scoped attributes. This  objects is of the javax.servlet.jsp.PageContext type. That is it provides methods to get and set attributes in different scopes and for transferring requests to other resources.

The application object: The application object refers to the entire environment of a web application to which a JSP page belongs. It gives facility for a JSP page to obtain and set information about the web application in which it is running.

The session Object: The session object helps access data and is of the HttpSession type. it is declared if the value of the session attribute in a page directive is true. The session implicit object is used to store session state for a single user.

The config Object: The config object specifies the configuration of the parameters ed to a JSP page. It is an instance of the java class that implements javax.servlet.ServletConfig interface. It gives facility for a JSP page to obtain the initialization parameters available.

The exception Object:  This object is an instance of the java.lang.Throwable class. It is available in JSP error pages only that have the ErrorPage attribute set to true with page directive.

The use of implicit: Following is an example in which i uses the JSP implicit object to make my pages dynamic:

    a. index.jsp:

          <html>
              <body bgcolor="skyblue">
              <form action="request.jsp">
              Name:<input type="text" name="name" />
                      <input type="submit" value="ok" />

              </body>
        </html>

  
    b. request.jsp:

         
      <html>
         <head><title>Use of implicit objects</title></head>
         <body bgcolor="pink" >
         <b><%=request.getParameter("name")%> </b><br/></br>
         Your Request details are <br/><br/>
         <table border="2">
         <tr><th>Name</th><th>Value</th></tr>
         <tr><td>request method</td>
         <td><%=request.getMethod() %></td></tr>
         <tr><td>request URI</td>
         <td><%=request.getRequestURI() %></td></tr>
         <tr><td>request protocol</td>
         <td><%=request.getProtocol() %></td></tr>
         <tr><td>browser</td>
         <td><%=request.getHeader("user-agent") %></td></tr>
         </table>
         <%if (session.getAttribute("sessionVr")==null){
         session.setAttribute("sessionVr", new Integer(0));
         }%>
         <table>
         <tr><th align="left>"Remaining implicit objects</th></tr>
          <tr>
        <form name="form1" action="pageContext.jsp" method="post">
        <td><input type="radio" name="other" value="Yes">Yes</td>
        <td><input type="radio" name="other" value="No">No</td></tr>
        <tr><td><input type="submit" value="submit"></td></tr>
        </form>
        </table>
        </body
     </html>
 

       c. pageContext.jsp:

          <html>
              <head>
              <title>Intermediate page</title>
              </head>
              <body bgcolor="yellow">
                Intermediate page
              <%
                  if("Yes".equals(request.getParameter("other")))
                  {
                  pageContext.forward("other");
                  }
              %>
              </body>
         </html>

Output:

a. index.jsp:


index.gif

 b. request.jsp:

request.gif

c. pageContext.jsp:

pagecontext.gif


Similar Articles