beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

JSP Expression Language (EL) – JSP Tutorial

By Chaitanya Singh | Filed Under: JSP tutorial

Expression language (EL) has been introduced in JSP 2.0. The main purpose of it to simplify the process of accessing data from bean properties and from implicit objects. EL includes arithmetic, relational and logical operators too.

Synatx of EL:

${expression}

whatever present inside braces gets evaluated at runtime and being sent to the output stream.

Example 1: Expression language evaluates the expressions

In this example we are evaluating the expressions with the help of EL.

<html> 
<head>
 <title>Expression language example1</title>
</head>
<body> 
${1<2}
${1+2+3}
</body> 
</html>

Output:

EL-example1

Example 2: Value fetch using param variable of expression language

In this example we are prompting user to enter name and roll number. On the other JSP page we are fetching the entered details using param variable of EL.

index.jsp

<html> 
<head>
 <title>Expression language example2</title>
</head>
<body> 
<form action="display.jsp"> 
Student Name: <input type="text" name="stuname" /><br>
Student RollNum:<input type="text" name="rollno" /><br>
<input type="submit" value="Submit Details!!"/> 
</form> 
</body> 
</html>

display.jsp

<html>
<head>
<title>Display Page</title>
</head>
<body>
 Student name is ${ param.stuname } <br>
 Student Roll No is ${ param.rollno }
</body>
</html>

Output:

EL-example2

Expression-lang-example2

Example 3: Getting values from application object.

In this example we have set the attributes using application implicit object and on the display page we have got those attributes using applicationScope of Expression language.

index.jsp

<html>
 <head>
 <title>EL example3</title>
 </head>
 <body>
 <%
 application.setAttribute("author", "Chaitanya");
 application.setAttribute("Site", "BeginnesBook.com");
 %>
 <a href="display.jsp">Click</a>
 </body>
 </html>

display.jsp

<html>
 <head>
 <title>Display Page</title>
 </head>
 <body>
 ${applicationScope.author}<br>
 ${applicationScope.Site}
 </body>
 </html>

Output:

Expression-lang-example3

EL-example3

EL predefined variables:

Similar to implicit objects in JSP we have predefined variables in EL. In the above examples we have used param and applicationScope, they are also the part of these variables.

pageScope: It helps in getting the attribute stored in Page scope.
pageContext: Same as JSP PageContext object.
sessionScope: Fetches attributes from session scope, set by session object.
requestScope: It used for getting the attributes from request scope. The attribute which are set by request implicit object.
param: Similar to ServletRequest.getParameter. Refer Example 2.
applicationScope: Used for getting Applicaton level attributes. Same what we see in Example 3.
header: It helps in getting HTTP request headers as Strings.
headerValues: Used for fetching all the HTTP request headers.
initParam: It links to context initialization parameters.
paramValues: Same as ServletRequest.getParmeterValues.
cookie: It maps to Cookie object.

Enjoyed this post? Try these related posts

  1. Response Implicit Object in JSP with examples
  2. Difference between include directive and include tag in JSP
  3. pageContext Implicit Object in JSP with examples
  4. How to access body of Custom tags in JSP tutorial
  5. Introduction to Java Server Pages – JSP Tutorial
  6. JSP Actions – Java Server Pages

Comments

  1. Ashish Kumar Rao says

    August 31, 2014 at 5:02 PM

    best for learner ,impressive learning tutorial for freshers…!

    that’s great help for me——Ψ

    thanks a lot Sir.

    Reply

Leave a Reply Cancel reply

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

JSP Tutorial

  • Basics of JSP
  • Life cycle of JSP
  • JSP in Eclipse IDE
  • Declaration tag
  • Expression tag
  • Scriptlets
  • Directives
  • Include Directive
  • Param Directive
  • Exception handling
  • Action tags
  • Include action
  • Forward action
  • useBean, setProperty & getProperty
  • Implicit Objects
  • Session implicit object
  • Validate session
  • Request implicit object
  • Response implicit object
  • Out implicit object
  • Application implicit object
  • Config implicit object
  • pageContext implicit object
  • Exception implicit object
  • Expression language
  • Custom Tags
  • Custom tag example
  • JSP Interview Q

Recently Added..

  • JSON Tutorial
  • Java Regular Expressions Tutorial
  • Java Enum Tutorial
  • Java Annotations Tutorial

Copyright © 2012 – 2021 BeginnersBook . Privacy Policy . Sitemap