JSP Expression Language

Describing JSP Expression Language:

The JSP expression language allows a page author to access a bean using simple syntax such as $(name). Before JSP 2.0, we could only use a scriptlet, JSP expression, or a custom tag to include server state in the jsp page output. Expression Language (EL) was first introduced in JSTL 1.0. EL makes it easier to integrate server side state with the presentation output. Now it has its own independent specification document, which states that EL is generally applicable to a variety of  technologies and is not dependent on the JSP specification. It is a simple language for accessing data, it makes it possible to easily access application data stored in JavaBeans components

Types of EL Expressions:

  • Immediate and deferred expression
  • Value expression
  • Method Expression

Immediate and Deferred Expressions: There are two construct to represent the EL expressions: ${expr} and #{expr}. In a JSP page, ${exp} is used for expression that need to be evaluated immediately and #{expr} is used for expression that are used at a later time. Therefore, an EL tag used ${expr} syntax is called immediate expression and the EL expression using the #{exp} is called deferred expression.

Example: <fmt:formNumber values="${sessionScope.cart.sum}"/>

              <h: inputText id="name" value="#{emp.name}"/>

Value Expression: Value Expression are used to refer to objects such as JavaBeans, collections, enumerations, and implicit objects, and their properties. Reference to an object is made using the value expression containing the name of the object.

Example:  <prefix: tag>
                  some text ${expr}

Method Expressions: Method expression are used to call public methods, which return a value or object. It is usually a deferred expressions. JSF html tags represent UI components on a JSF page. These tags use method expressions to call functions that perform operations such as validating a UI component are handling the events generated on a UI component.

Example: <h:form>
                 <h:inputText id="email " value="#{emp.email}" validator="#{emp.validateEmail}"/>
                 <h:commandButton id="submit" action="#{customer.submit}"/>

EL Operators: These are introduced in unified EL API so that developers can perform arithmetic calculations. Now we can create EL expressions performing operations by using operators of EL.

Types of EL Operators:

  • Arithmetic operators
  • Relational and logical operators       
  • The empty operator

Arithmetic Operators: All types of arithmetic operators (+,/,*,%,-) can be used in EL expressions.

Relational and Logical Operators: EL provide a set of relational and logical operators used in many programming languages. These are:{>,>=,<,<=,=,!=}.

The Empty Operator: This is aprefix operator, which means it takes one operand on its right side. It is used to check for empty values, which differ according to the data type of the operand.

Examples of using EL Expression and EL Operators:

EL Expression.jsp:

<title> Use of Expression Language in jsp</title></head>
<%pageContext.setAttribute("pageColor", "#FFFFDF");
<table bgcolor="cyan" border="1"
align="center" width="60%" height="60%">
<b>Welcome to the MCN Software Ltd</b></td>
You appear to be using the following browser:<br>

EL Operators.jsp:

<tltle> Using EL Operators</tltle>
<body bgcolor="green">
<h2>using EL operators</h2>
<h3>Arithmethic Expression</h3>
<b>Sachin score In match is ${50+110-50/2}.</b><br/>
<b>Today temperature of delhi is ${12+16} degree celsius.</b><br/>
<b>Is 1/5 equals to 0.5?${1/2==.5}? "yes" : "No"}</b><br/>
<b>T have ${12 mod 15} MOBILES.</b><br/>
<h3>Comparison operators</h3>
<b> Is 4>3 ? ${4 >3}</b><br/>
<b> Is "a" > "b" ? ${"a" > "b"}</b><br/>
<b> Is 4>=3 ? ${4>=3}</b><br/>
<h3>empty Operator</h3>
<b>empty "" ${empty ""}</b><br/>
<b>empty "string" ${empty "string"}</b><br/>
<b>empty null ${empty null}</b><br/>


EL Expression.jsp:


EL Operators.jsp: