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:
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:
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:
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.
Ashish Kumar Rao says
best for learner ,impressive learning tutorial for freshers…!
that’s great help for me——Ψ
thanks a lot Sir.