JSP EL Other Operators.

JSP El Other Operators

  • These El other operators are primarily related to objects in JSP.

EL Square brackets operator.

  • You can use square brackets "[ ]" to access properties of a named variable instead of "." notation.
    On Object example:
    ${presidentControl['presidents']} is the same as ${presidentControl.presidents}
    This syntax is also used to access the items within:
    1. maps
    2. lists
    3. or arrays
    On Map example:
    ${myColorMap['blue']} is the same as ${myColorMap.blue}

EL Empty operator.

  • The empty operator is a prefix operator that can be used to determine if a value is null or empty.
    Example:
    ${empty  Object}
  • If the empty operator in the expression finds an object to be null, the expression will evaluates to true.
  • If the object is an empty string, array, map, or an empty list, then the expression also evaluates to true.
    Example:
    ${empty  publisherControl.publishers}
    // publishers is an array of publisher objects
    // if this array has no object the expression will return true
  • Each operator has a precedent that determines the order of evaluation. You'll find this precedent here.

Example of JSP El Other operators.

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_Others).

In this example, we will add two files, header.jsp and footer.jsp.

  • It is customary to place all include files, which are used several times in the WEB-INF folder. Some would also like the include files should have the extension .jspf, but this is not a requirement.
    Here is the file we want to include at the top of all pages:
    <h2 class="siteblue">JSP EL Other Operators</h2>
    <hr>

    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 Header.jsp and folder should be WEB-INF).

    Here is the file we want to include at the bottom of all pages:
    <hr >
    <jsp:useBean id="today" class="java.util.Date" scope="page" />
    <span class="siteblue">Listing date ${today}</span>

    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 Footer.jsp and folder should be WEB-INF).

In this example, we will add two java files, Publisher.java and PublisherControl.java.

  • We need a java class with information about each Publisher that we want to list through a PublisherControl class.
    Here is the Publisher.java:
    package app;
    
    public class Publisher {
    
      private String pub_id;
      private String pub_name;
      private String address;
      private String city;
      private String state;
    
      public Publisher(String pub_id, String pub_name, String address, String city, String state) {
        this.pub_id = pub_id;
        this.pub_name = pub_name;
        this.address = address;
        this.city = city;
        this.state = state;
      }
    
      public String getPub_id() {
        return pub_id;
      }
    
      public String getPub_name() {
        return pub_name;
      }
    
      public String getAddress() {
        return address;
      }
    
      public String getCity() {
        return city;
      }
    
      public String getState() {
        return state;
      }
      
    }

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

  • We want to list some related informations of each publisher with help of the PublisherControl class.
  • We need a Java class, PublisherControl.java, that handles the existence of each Publisher in a list and can give us the list when we need it in our JSP.
    Here is the PublisherControl.java:
    package app;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class PublisherControl {
        private List<Publisher>  publishers= new ArrayList<Publisher>();
        private List<Publisher>  newpublishers= new ArrayList<Publisher>();
    
      public PublisherControl() {
        publishers.add(new Publisher("0736","New Age Books","1 1st St",
                "Boston","MA"));
        publishers.add(new Publisher("0877","Binnet & Hardley","2 2nd Ave.",
                "Washington","DC"));
        publishers.add(new Publisher("1389","Algodata Infosystems","3 3rd Dr.",
                "Berkeley","CA"));
      }
    
      public List<Publisher> getPublishers() {
        return publishers;
      }
    
      public List<Publisher> getNewpublishers() {
        return newpublishers;
      }
      
    }

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

  • In this class we have a member variable, publishers, which shall contain all the registered publishers at current time. This is instantiated when an instance of the class is created. We have also added some publishers in the default constructor.
  • We have created a get method that returns a list of all the registered publishers at current time. This is meant to be used by JSP EL.
  • The same is done for a list of new publishers that is empty (this is created here to show the effect of the EL empty operator).

We need a JSP file(s) to demonstrate use of JSP EL Other Operators.

  • <%@page contentType="text/html" pageEncoding="UTF-8" import="app.*"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    
    <!DOCTYPE html>
    <html>
      <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Country Liste</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(even) { 
            background: #eeeeee; 
          }
          table { 
            border-color: #aad5ff;
            border-style: solid;
            border-width: 0 0 1px 1px;
            border-spacing: 0;
            border-collapse: collapse;
          }      
          tr,th,td {
            border-color: #aad5ff;
            border-style: solid;
            padding: 2px;
            border-width: 1px 1px 0 0;
            vertical-align: central;  
          }
          table tr th {
            color: blue;
          }
          hr { background-color: #3366ff; height: 1px; border: 0; }
          .siteblue {
            color: #3366ff;
          }
    
        </style>
      </head>
      <body>
        <div style="max-width:530px; ">
          <%@ include file="WEB-INF/header.jsp" %>
          <jsp:useBean id="publisherControl" scope="page" class="app.PublisherControl"/>
          <c:if test="${empty publisherControl['newpublishers']}">
            <p>There are no new publishers yet.</p>
            <hr >
          </c:if>
          <c:if test="${!(empty publisherControl['publishers'])}">
            <table >
              <tr class="gradientdown">
                <th style="width:50px;">Id</th>
                <th style="width:160px;">Name</th>
                <th style="width:60px;">Address</th>
                <th style="width:100px;">City</th>
                <th style="width:100px;">State</th>
              </tr>  
              <c:forEach items="${publisherControl['publishers']}" var="publisher" >
                <tr class="data">
                  <td style="text-align: center;">${publisher["pub_id"]}</td>
                  <td >${publisher["pub_name"]}</td>
                  <td >${publisher["address"]}</td>
                  <td >${publisher["city"]}</td>
                  <td>${publisher["state"]}</td>
                </tr>
              </c:forEach>
            </table>
          </c:if>
          <%@ include file="WEB-INF/footer.jsp" %>
        </div>
      </body>
    </html>

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.
  • With a 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.
  • 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.

Creating Web-server Deployment descriptor.

  • The context-root (in example /EL_Others) 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.