JSP EL Implicit Objects.

JSP EL Implicit Objects

  • The javaserver pages (jsp) technology defines a number of EL implicit objects. These should not be confused with the JSP implicit objects
  • Each of these El implicit objects contains maps with keys and values. The exception is the EL implicit object pageContext.
JSP EL Implicit Objects Type Description
pageContext pageContext The EL pageContext object can bee used to get the request, response, session, servletConfig, ServletContex references etc. Furthermore, all these references provide the ability to extract a lot of information about the servlet environment.
pageScope Map This is a Map that contains all page-scoped variables. The Map is keyed on the name of the variable.
requestScope Map This is a Map that contains all request-scoped variables. The Map is keyed on the name of the variable.
sessionScope Map This is a Map that contains all session-scoped variables. The Map is keyed on the name of the variable..
applicationScope Map This is a Map that contains all application-scoped variables. The Map is keyed on the name of the variable.
param Map This is a Map that contains the names of the parameters to a page. Each parameter name is mapped to a single string value.
paramValues Map This is a Map that maps a parameter name to a string array of all the values for that parameter.
header Map This is a Map that contains the values of each header name.
headerValues Map This is a Map that maps a header name to a string array of all the possible values for that header.
cookie Map This is a Map that maps cookie names to a single Cookie object. If more than one cookie exists for a given name, the first of these cookies is used for that name.
initParam Map This is a Map that maps context initialization parameter names to their string parameter values.

Example of using JSP EL implicit objects.

In the example we use Netbeans IDE and Glassfish Server.

You can download this example here (needed tools can be found in the right menu on this page).

If you like to participate in the review of this example you must first create a Web project in Netbeans (the project name is EL_Objects).

JSP file to list Implicit objects data.

  • Here is the jsp-file that lists some of the possible data from some of the jsp implicit objects:
    <%@page contentType="text/html" pageEncoding="UTF-8" 
            import="java.util.*" %>
    
    <!DOCTYPE html >
    <html>
      <head>
        <meta http-equiv="Content-Type" 
              content="text/html; charset=UTF-8">
        <title>Library Book Control</title>
        <style>
          .gradientdown {
            background:  #e0e6ff;
            background: -webkit-linear-gradient(top, #e0e6ff 0%,#eeeeee 100%); /* Chrome10+,Safari5.1+ */
            background: -o-linear-gradient(#e0e6ff, #eeeeee); /* For Opera 11.1 to 12.0 */
            background: -moz-linear-gradient(top, #e0e6ff, #eeeeee); /* For Firefox 3.6 to 15 */
            background: linear-gradient(top, #e0e6ff 0%,#eeeeee 100%); /* W3C Standard syntax (must be last) */
          }
          table tr.data:nth-of-type(odd) { 
            background: #eeeeee; 
          }
          table tr th  {text-align: left;}
          table {
            width: 100%; table-layout: fixed;
          }
        </style>  
    
      </head>
      <body onload="submitForm();">
        <div style="max-width:700px; ">
          <%
            Cookie cookie1 = new Cookie("message", "This is my cookie!");
            cookie1.setMaxAge(24 * 60 * 60);
            response.addCookie(cookie1);
            application.setAttribute("person", "Person in Application Scope");
            // this is only for demo
            session.setAttribute("sessiondata",
                    new Object() {
                      public String toString() {
                        return "This is the session data";
                      }
                    });
            request.setAttribute("time",
                    (new java.util.Date()).toString());
          %> 
          <jsp:useBean id="sessionBook" class="doc.Book" scope="session" />
          <jsp:setProperty name="sessionBook" property="isbn_no" 
                           value="0201703092" />
          <jsp:setProperty name="sessionBook" property="short_desc" 
                           value="The Session Book" />
          <jsp:setProperty name="sessionBook" property="price" 
                           value="39" />
          <jsp:useBean id="pageBook" class="doc.Book" scope="page" />     
          <jsp:setProperty name="pageBook" property="isbn_no" 
                           value="0471777781" />
          <jsp:setProperty name="pageBook" property="short_desc" 
                           value="The Page Book" />
          <jsp:setProperty name="pageBook" property="price"
                           value="32" />
          <jsp:useBean id="requestBook" class="doc.Book" scope="request" />     
          <jsp:setProperty name="requestBook" property="isbn_no"
                           value="0764557599" />
          <jsp:setProperty name="requestBook" property="short_desc" 
                           value="The Request Book" />
          <jsp:setProperty name="requestBook" property="price"
                           value="42" /> 
          <h2>EL Implicit object info:</h2>  
          <table style="width:100%;" >
            <tr class="gradientdown">
              <th style="width:40%;">Result</th> 
              <th >pageContext object</th> 
            </tr>
            <tr class="data"  style="word-wrap: break-word;">
              <td>${pageContext.request.contextPath}</td> 
              <td>${'${'}pageContext.request.contextPath} </td> 
            </tr>
            <tr class="data"  style="word-wrap: break-word;">
              <td>${pageContext.request.servletPath}</td> 
              <td>${'${'}pageContext.request.servletPath} </td> 
            </tr>
            <tr class="data"  style="word-wrap: break-word;">
              <td>${pageContext.request.method}</td> 
              <td>${'${'}pageContext.request.method}</td> 
            </tr>
            <tr class="data"  style="word-wrap: break-word;">
              <td>${pageContext.request.requestURI}</td> 
              <td>${'${'}pageContext.request.requestURI}</td> 
            </tr>
            <tr class="data"  style="word-wrap: break-word;">
              <td>${pageContext.request.requestURL}</td> 
              <td>${'${'}pageContext.request.requestURL}</td> 
            </tr>
            <tr class="data" style="word-wrap: break-word;">
              <td>${pageContext.request.queryString}</td> 
              <td>${'${'}pageContext.request.queryString}</td> 
            </tr>
            <tr class="data"  style="word-wrap: break-word;">
              <td>${pageContext.request.getAttribute("time")}</td> 
              <td>${'${'}pageContext.request.getAttribute("time")}</td> 
            </tr>
            <tr class="data"  style="word-wrap: break-word;">
              <td>${pageContext.request.getParameter("isbn_no")}</td> 
              <td>${'${'}pageContext.request.getParameter("isbn_no")}</td> 
            </tr>  
            <tr class="data"  style="word-wrap: break-word;">
              <td>${pageContext.request.getParameter("short_desc")}</td> 
              <td>${'${'}pageContext.request.getParameter("short_desc")}</td> 
            </tr>  
            <tr class="data"  style="word-wrap: break-word;">
              <td>${pageContext.request.getParameter("price")}</td> 
              <td>${'${'}pageContext.getParameter("isbn_no")}</td> 
            </tr>  
            <tr class="data"  style="word-wrap: break-word;">
              <td>${pageContext.request.getParameter("action")}</td> 
              <td>${'${'}pageContext.request.getParameter("action")}</td> 
            </tr>  
            <tr class="data"  style="word-wrap: break-word;">
              <td>${pageContext.servletContext.serverInfo}</td> 
              <td>${'${'}pageContext.servletContext.serverInfo}</td> 
            </tr>  
            <tr class="data"  style="word-wrap: break-word;">
              <td>${pageContext.servletContext.virtualServerName}</td> 
              <td>${'${'}pageContext.servletContext.virtualServerName}</td> 
            </tr>  
            <tr class="data"  style="word-wrap: break-word;">
              <td>${pageContext.servletContext.getInitParameter("welcome")}</td> 
              <td>${'${'}pageContext.servletContext.getInitParameter("welcome")}</td> 
            </tr> 
            <tr class="data"  style="word-wrap: break-word;">
              <td>${pageContext.session.getAttribute("sessiondata")}</td> 
              <td>${'${'}pageContext.session.getAttribute("sessiondata")}</td> 
            </tr> 
            <tr class="data"  style="word-wrap: break-word;">
              <td>${pageContext.session.id}</td> 
              <td>${'${'}pageContext.session.id} </td> 
            </tr>
            <tr class="data"  style="word-wrap: break-word;">
              <td>${pageContext.servletConfig.getInitParameter("initParam")}</td> 
              <td>${'${'}pageContext.servletConfig.getInitParameter("initParam")}</td> 
            </tr>        
          </table>    
          <hr>
          <table style="width:100%;" >
            <tr class="gradientdown">
              <th style="width:40%;">Result</th> 
              <th >pageScope</th> 
            </tr>
            <tr class="data"  style="word-wrap: break-word;">
              <td>${pageScope.pageBook.isbn_no}</td> 
              <td>${'${'}pageScope.pageBook.isbn_no}</td> 
            </tr>
            <tr class="data"  style="word-wrap: break-word;">
              <td>${pageScope.pageBook.short_desc}</td> 
              <td>${'${'}pageScope.pageBook.short_desc}</td> 
            </tr>
            <tr class="data"  style="word-wrap: break-word;">
              <td>${pageScope.pageBook.price}</td> 
              <td>${'${'}pageScope.pageBook.price}</td> 
            </tr>
          </table>  
          <hr>
          <table style="width:100%;" >
            <tr class="gradientdown">
              <th style="width:40%;">Result</th> 
              <th >requestScope</th> 
            </tr>
            <tr class="data"  style="word-wrap: break-word;">
              <td>${requestScope.requestBook.isbn_no}</td> 
              <td>${'${'}requestScope.requestBook.isbn_no}</td> 
            </tr>
            <tr class="data"  style="word-wrap: break-word;">
              <td>${requestScope.requestBook.short_desc}</td> 
              <td>${'${'}requestScope.requestBook.short_desc}</td> 
            </tr>
            <tr class="data"  style="word-wrap: break-word;">
              <td>${requestScope.requestBook.price}</td> 
              <td>${'${'}requestScope.requestBook.price}</td> 
            </tr>
            <tr class="data"  style="word-wrap: break-word;">
              <td>${requestScope.time}</td> 
              <td>${'${'}requestScope.time}</td> 
            </tr>
          </table>    
          <hr>
          <table style="width:100%;" >
            <tr class="gradientdown">
              <th style="width:40%;">Result</th> 
              <th>sessionScope</th> 
            </tr>
            <tr class="data"  style="word-wrap: break-word;">
              <td>${sessionScope.sessionBook.isbn_no}</td> 
              <td>${'${'}sessionScope.sessionBook.isbn_no}</td> 
            </tr>
            <tr class="data"  style="word-wrap: break-word;">
              <td>${sessionScope.sessionBook.short_desc}</td> 
              <td>${'${'}sessionScope.sessionBook.short_desc}</td> 
            </tr>
            <tr class="data"  style="word-wrap: break-word;">
              <td>${sessionScope.sessionBook.price}</td> 
              <td>${'${'}sessionScope.sessionBook.price}</td> 
            </tr>
            <tr class="data"  style="word-wrap: break-word;">
              <td>${sessionScope.sessiondata}</td> 
              <td>${'${'}sessionScope.sessiondata}</td> 
            </tr>
          </table>    
          <hr>
          <table style="width:100%;" >
            <tr class="gradientdown">
              <th style="width:40%;">Result</th> 
              <th>param</th> 
            </tr>
            <tr class="data"  style="word-wrap: break-word;">
              <td>${param.isbn_no}</td> 
              <td>${'${'}param.isbn_no}</td> 
            </tr>
            <tr class="data"  style="word-wrap: break-word;">
              <td>${param.short_desc}</td> 
              <td>${'${'}param.short_desc}</td> 
            </tr>
            <tr class="data"  style="word-wrap: break-word;">
              <td>${param.price}</td> 
              <td>${'${'}param.price}</td> 
            </tr>
          </table>    
          <hr>
          <table style="width:100%;" >
            <tr class="gradientdown">
              <th style="width:40%;">Result</th> 
              <th>applicationScope</th> 
            </tr>
            <tr class="data"  style="word-wrap: break-word;">
              <td>${applicationScope.person}</td> 
              <td>${'${'}applicationScope.person}</td> 
            </tr>
          </table>    
          <hr>
          <table style="width:100%;" >
            <tr class="gradientdown">
              <th style="width:40%;">Result</th> 
              <th>cookie</th> 
            </tr>
            <tr class="data"  style="word-wrap: break-word;">
              <td>${cookie.message.value}</td> 
              <td>${'${'}cookie.message.value}</td> 
            </tr>
          </table>  
          <hr>
          <table style="width:100%;" >
            <tr class="gradientdown">
              <th style="width:40%;">Result</th> 
              <th>initParam</th> 
            </tr>
            <tr class="data"  style="word-wrap: break-word;">
              <td>${initParam.welcome}</td> 
              <td>${'${'}initParam.welcome}</td> 
            </tr>
          </table>  
          <hr>
          <table style="width:100%;" >
            <tr class="gradientdown">
              <th style="width:40%;">Result</th> 
              <th>header</th> 
            </tr>
            <tr class="data"  style="word-wrap: break-word;">
              <td>${header["user-agent"]}</td> 
              <td>${'${'}header["user-agent"]}</td> 
            </tr>
            <tr class="data"  style="word-wrap: break-word;">
              <td>${header["host"]}</td> 
              <td>${'${'}header["host"]}</td> 
            </tr>
            <tr class="data"  style="word-wrap: break-word;">
              <td>${header["accept-encoding"]}</td> 
              <td>${'${'}header["accept-encoding"]}</td> 
            </tr>
            <tr class="data"  style="word-wrap: break-word;">
              <td>${header["accept-language"]}</td> 
              <td>${'${'}header["accept-language"]}</td> 
            </tr>
          </table>    
          <hr> 
        </div>
      </body>
    </html>

    For those who participate in the review: create a JSP file in Netbeans and replace generated code for the JSP with that shown above (the JSP file name is Request.jsp).

  • In this file, we expect some request parameters from a html form and an attribute on the request as well.
  • For this demonstration, we have also created an attribute on the session, page and request object.

I need a Book class for JSP useBean book object creation

  • package doc;
    public class Book {
      private String isbn_no;
      private String short_desc;
      private double price;
    
      public Book(String isbn_no, String short_desc, double price) {
        this.isbn_no = isbn_no;
        this.short_desc = short_desc;
        this.price = price;
      }
    
      public Book() {
        this.isbn_no = "9999999999";
        this.short_desc = "New Book";
        this.price = 0.0;
      }
    
      public void setIsbn_no(String isbn_no) {
        this.isbn_no = isbn_no;
      }
    
      public void setShort_desc(String short_desc) {
        this.short_desc = short_desc;
      }
    
      public void setPrice(double price) {
        this.price = price;
      }
    
      public String getIsbn_no() {
        return isbn_no;
      }
    
      public String getShort_desc() {
        return short_desc;
      }
    
      public double getprice() {
        return price;
      }
    }

    For those who participate in the review: create a JSP file in Netbeans and replace generated code for the java file with that shown above (the java file name is Book and package should be doc).

I like to start up with a JSP file (s) which pass request data (parameters) to the index.jsp file.

  • <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
      <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
        <script language="JavaScript">
          function submitForm() {
            document.getElementById('newBook').submit();
          }
        </script>    
      </head>
      <body onload="submitForm();">
        <form id="newBook" action='index.jsp' method="GET">
          <input type="hidden" name="isbn_no"  value="4501703092"   />
          <input type="hidden" name="short_desc" value="The Startup Book" />
          <input type="hidden" name="price" value="123" />
          <input type="hidden" name="action" value="add"/>
        </form>
      </body>

    For those who participate in the review: create a JSP file in Netbeans and replace generated code for the JSP with that shown above (the JSP file name is Startup).

  • When the file is loaded we simulate a submit of form input data to the index.jsp file.

Creating Deployment descriptor.

  • To run this JSP you have to deploy it to a web-server or a Application server. To deploy means to install the JSP with some instruction to a such server.
  • The instructions are mainly defined to be deployment descriptors. The standard part of the deployment descriptor should be in an XML-file with the name web.xml.

    You may need to create a Deployment descriptor file, web.xml in Netbeans.

  • The contents of the web.xml file should look like this:
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee     
    http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
      <servlet>
        <servlet-name>Startup</servlet-name>
        <jsp-file>/Startup.jsp</jsp-file>
        <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet>
        <servlet-name>index</servlet-name>
        <jsp-file>/index.jsp</jsp-file>
        <init-param>
          <param-name>initParam</param-name>
          <param-value>index.jsp init param</param-value>
        </init-param>
      </servlet>
      <servlet-mapping>
        <servlet-name>Startup</servlet-name>
        <url-pattern>/Startup</url-pattern>
      </servlet-mapping>
      <servlet-mapping>
        <servlet-name>index</servlet-name>
        <url-pattern>/index.jsp</url-pattern>
      </servlet-mapping>
      <session-config>
        <session-timeout>
          30
        </session-timeout>
      </session-config>
      <welcome-file-list>
        <welcome-file>Startup</welcome-file>
      </welcome-file-list>
      <context-param>
        <param-name>welcome</param-name>
        <param-value>Welcome text</param-value>
      </context-param>
    </web-app>
  • This file starts with the normal xml tag for a XML file and the root tag for the deployment descriptor is web-app. Every ting inside the last tag is to tell the server about our application, which in this case is a JSP file.
  • For each servlet tag we give the JSP file a servlet name, which is used in the servlet-mapping tag to specify a url for the JSP file.
  • For the index.jsp we have registered a <param-name> tag to demonstrate use of servlet initial data.
  • In this way we can have many urls for the same JSP file.
  • If no session-timeout (the server ends the service of the application after this time) is given a standard timeout for the server is used as timeout for the application.
  • The welcome-file tag specifies the startup for our application, which in this case and our application is the welcome file with url Startup. Reorganize the welcome-file-list to what is shown above.
  • At the end of the file we have registered a <param-name> tag to demonstrate use of application initial data.

Creating Web-server Deployment descriptor.

  • The context-root (in example /EL_Objects) for the application will in most cases be specified by a server vendor deployment descriptor.
    The browser will display:
© 2010 by Finnesand Data. All rights reserved.
This site aims to provide FREE programming training and technics.
Finnesand Data as site owner gives no warranty for the correctness in the pages or source codes.
The risk of using this web-site pages or any program codes from this website is entirely at the individual user.