Expression Language (EL) in JSP

Last updated on May 31 2022
Mohnish Patil

Table of Contents

Expression Language (EL) in JSP

JSP Expression Language (EL) makes it possible to easily access application data stored in JavaBeans components. JSP EL allows you to create expressions both (a) arithmetic and (b) logical. Within a JSP EL expression, you can use integers, floating point numbers, strings, the built-in constants true and false for boolean values, and null.

Simple Syntax

Typically, when you specify an attribute value in a JSP tag, you simply use a string. For example −

<jsp:setProperty name = "box" property = "perimeter" value = "100"/>

JSP EL allows you to specify an expression for any of these attribute values. A simple syntax for JSP EL is as follows −

${expr}

Here expr specifies the expression itself. The most common operators in JSP EL are . and []. These two operators allow you to access various attributes of Java Beans and built-in JSP objects.

For example, the above syntax <jsp:setProperty> tag can be written with an expression like −

<jsp:setProperty name = "box" property = "perimeter"

   value = "${2*box.width+2*box.height}"/>

When the JSP compiler sees the ${} form in an attribute, it generates code to evaluate the expression and substitues the value of expresson.

You can also use the JSP EL expressions within template text for a tag. For example, the <jsp:text> tag simply inserts its content within the body of a JSP. The following <jsp:text> declaration inserts <h1>Hello JSP!</h1> into the JSP output −

<jsp:text>

   <h1>Hello JSP!</h1>

</jsp:text>

You can now include a JSP EL expression in the body of a <jsp:text> tag (or any other tag) with the same ${} syntax you use for attributes. For example −

<jsp:text>

   Box Perimeter is: ${2*box.width + 2*box.height}

</jsp:text>

EL expressions can use parentheses to group subexpressions. For example, ${(1 + 2) * 3} equals 9, but ${1 + (2 * 3)} equals 7.

To deactivate the evaluation of EL expressions, we specify the isELIgnored attribute of the page directive as below −

<%@ page isELIgnored = "true|false" %>

The valid values of this attribute are true and false. If it is true, EL expressions are ignored when they appear in static text or tag attributes. If it is false, EL expressions are evaluated by the container.

Basic Operators in EL

JSP Expression Language (EL) supports most of the arithmetic and logical operators supported by Java. Following table lists out the most frequently used operators −

S.No. Operator & Description
1 .

Access a bean property or Map entry

2 []

Access an array or List element

3 ( )

Group a subexpression to change the evaluation order

4 +

Addition

5

Subtraction or negation of a value

6 *

Multiplication

7 / or div

Division

8 % or mod

Modulo (remainder)

9 == or eq

Test for equality

10 != or ne

Test for inequality

11 < or lt

Test for less than

12 > or gt

Test for greater than

13 <= or le

Test for less than or equal

14 >= or ge

Test for greater than or equal

15 && or and

Test for logical AND

16 || or or

Test for logical OR

17 ! or not

Unary Boolean complement

18 empty

Test for empty variable values

Functions in JSP EL

JSP EL allows you to use functions in expressions as well. These functions must be defined in the custom tag libraries. A function usage has the following syntax −

${ns:func(param1, param2, ...)}

Where ns is the namespace of the function, func is the name of the function and param1 is the first parameter value. For example, the function fn:length, which is part of the JSTL library. This function can be used as follows to get the length of a string.

${fn:length("Get my length")}

To use a function from any tag library (standard or custom), you must install that library on your server and must include the library in your JSP using the <taglib> directive as explained in the JSTL chapter.

JSP EL Implicit Objects

The JSP expression language supports the following implicit objects −

S.No Implicit object & Description
1 pageScope

Scoped variables from page scope

2 requestScope

Scoped variables from request scope

3 sessionScope

Scoped variables from session scope

4 applicationScope

Scoped variables from application scope

5 param

Request parameters as strings

6 paramValues

Request parameters as collections of strings

7 header

HTTP request headers as strings

8 headerValues

HTTP request headers as collections of strings

9 initParam

Context-initialization parameters

10 cookie

Cookie values

11 pageContext

The JSP PageContext object for the current page

You can use these objects in an expression as if they were variables. The examples that follow will help you understand the concepts −

The pageContext Object

The pageContext object gives you access to the pageContext JSP object. Through the pageContext object, you can access the request object. For example, to access the incoming query string for a request, you can use the following expression −

${pageContext.request.queryString}

The Scope Objects

The pageScope, requestScope, sessionScope, and applicationScope variables provide access to variables stored at each scope level.

For example, if you need to explicitly access the box variable in the application scope, you can access it through the applicationScope variable as applicationScope.box.

The param and paramValues Objects

The param and paramValues objects give you access to the parameter values normally available through the request.getParameter and request.getParameterValues methods.

For example, to access a parameter named order, use the expression ${param.order} or ${param[“order”]}.

Following is the example to access a request parameter named username −

<%@ page import = "java.io.*,java.util.*" %>

<%String title = "Accessing Request Param";%>




<html>

   <head>

      <title><% out.print(title); %></title>

   </head>

  

   <body>

      <center>

         <h1><% out.print(title); %></h1>

      </center>

     

      <div align = "center">

         <p>${param["username"]}</p>

      </div>

   </body>

</html>

The param object returns single string values, whereas the paramValues object returns string arrays.

header and headerValues Objects

The header and headerValues objects give you access to the header values normally available through the request.getHeader and the request.getHeaders methods.

For example, to access a header named user-agent, use the expression ${header.user-agent} or ${header[“user-agent”]}.

Following is the example to access a header parameter named user-agent −

<%@ page import = "java.io.*,java.util.*" %>

<%String title = "User Agent Example";%>




<html>

   <head>

      <title><% out.print(title); %></title>

   </head>

  

   <body>

      <center>

         <h1><% out.print(title); %></h1>

      </center>

     

      <div align = "center">

         <p>${header["user-agent"]}</p>

      </div>

   </body>

</html>

The output will somewhat be like the following −

User Agent Example

Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0;

SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729;

Media Center PC 6.0; HPNTDF; .NET4.0C; InfoPath.2)

 

The header object returns single string values, whereas the headerValues object returns string arrays.

So, this brings us to the end of blog. This Tecklearn ‘Expression Language (EL) in JSP’ blog helps you with commonly asked questions if you are looking out for a job in Java Programming. If you wish to learn JSP and build a career Java Programming domain, then check out our interactive, Java and JEE Training, that comes with 24*7 support to guide you throughout your learning period. Please find the link for course details:

https://www.tecklearn.com/course/java-and-jee-training/

Java and JEE Training

About the Course

Java and JEE Certification Training is designed by professionals as per the industrial requirements and demands. This training encompasses comprehensive knowledge on basic and advanced concepts of core Java & J2EE along with popular frameworks like Hibernate, Spring & SOA. In this course, you will gain expertise in concepts like Java Array, Java OOPs, Java Function, Java Loops, Java Collections, Java Thread, Java Servlet, and Web Services using industry use-cases and this will help you to become a certified Java expert.

Why Should you take Java and JEE Training?

  • Java developers are in great demand in the job market. With average pay going between $90,000/- to $120,000/- depending on your experience and the employers.
  • Used by more than 10 Million developers worldwide to develop applications for 15 Billion devices.
  • Java is one of the most popular programming languages in the software world. Rated #1 in TIOBE Popular programming languages index (15th Consecutive Year)

What you will Learn in this Course?

Introduction to Java

  • Java Fundamentals
  • Introduction to Java Basics
  • Features of Java
  • Various components of Java language
  • Benefits of Java over other programming languages
  • Key Benefits of Java

Installation and IDE’s for Java Programming Language

  • Installation of Java
  • Setting up of Eclipse IDE
  • Components of Java Program
  • Editors and IDEs used for Java Programming
  • Writing a Simple Java Program

Data Handling and Functions

  • Data types, Operations, Compilation process, Class files, Loops, Conditions
  • Using Loop Constructs
  • Arrays- Single Dimensional and Multi-Dimensional
  • Functions
  • Functions with Arguments

OOPS in Java: Concept of Object Orientation

  • Object Oriented Programming in Java
  • Implement classes and objects in Java
  • Create Class Constructors
  • Overload Constructors
  • Inheritance
  • Inherit Classes and create sub-classes
  • Implement abstract classes and methods
  • Use static keyword
  • Implement Interfaces and use it

Polymorphism, Packages and String Handling

  • Concept of Static and Run time Polymorphism
  • Function Overloading
  • String Handling –String Class
  • Java Packages

Exception Handling and Multi-Threading

  • Exception handling
  • Various Types of Exception Handling
  • Introduction to multi-threading in Java
  • Extending the thread class
  • Synchronizing the thread

File Handling in Java

  • Input Output Streams
  • io Package
  • File Handling in Java

Java Collections

  • Wrapper Classes and Inner Classes: Integer, Character, Boolean, Float etc
  • Applet Programs: How to write UI programs with Applet, Java.lang, Java.io, Java.util
  • Collections: ArrayList, Vector, HashSet, TreeSet, HashMap, HashTable

Java Database Connectivity (JDBC)

  • Introduction to SQL: Connect, Insert, Update, Delete, Select
  • Introduction to JDBC and Architecture of JDBC
  • Insert/Update/Delete/Select Operations using JDBC
  • Batch Processing Transaction
  • Management: Commit and Rollback

Java Enterprise Edition – Servlets

  • Introduction to J2EE
  • Client Server architecture
  • URL, Port Number, Request, Response
  • Need for servlets
  • Servlet fundamentals
  • Setting up a web project in Eclipse
  • Configuring and running the web app with servlets
  • GET and POST request in web application with demo
  • Servlet lifecycle
  • Servlets Continued
  • Session tracking and filter
  • Forward and include Servlet request dispatchers

Java Server Pages (JSP)

  • Fundamentals of Java Server Page
  • Writing a code using JSP
  • The architecture of JSP
  • JSP Continued
  • JSP elements: Scriptlets, expressions, declaration
  • JSP standard actions
  • JSP directives
  • Introduction to JavaBeans
  • ServletConfig and ServletContext
  • Servlet Chaining
  • Cookies Management
  • Session Management

Hibernate

  • Introduction to Hibernate
  • Introduction to ORM
  • ORM features
  • Hibernate as an ORM framework
  • Hibernate features
  • Setting up a project with Hibernate framework
  • Basic APIs needed to do CRUD operations with Hibernate
  • Hibernate Architecture

POJO (Plain Old Java Object)

  • POJO (Plain Old Java Object)
  • Persistent Objects
  • Lifecycle of Persistent Object

Spring

  • Introduction to Spring
  • Spring Fundamentals
  • Advanced Spring

Got a question for us? Please mention it in the comments section and we will get back to you.

 

 

0 responses on "Expression Language (EL) in JSP"

Leave a Message

Your email address will not be published. Required fields are marked *