JSP Expression Language, or JSP EL, can easily access data stored in JavaBeans components.
JSP EL can create arithmetic and logical expressions.
Inside a JSP EL expression, we can use numbers, strings, boolean values, and null.
The following code shows how to use a string to set an attribute value in a JSP tag.
<jsp:setProperty name="box" property="height" value="100"/>
value="100"
sets the constant value 100 to box's height property.
With JSP EL we can put an expression in the value attribute for jsp:setProperty
in the
following syntax.
${expr}
expr
specifies the expression.
The following code uses the JSP EL to in <jsp:setProperty> tag to double the width as the height.
<jsp:setProperty name="box" property="height" value="${2*box.width}"/>
You can use JSP EL expressions within template text for a tag. The following <jsp:text> declaration inserts <h1>Hello JSP!</h1> into the JSP output:
<jsp:text> <h1>Hello JSP!</h1> </jsp:text>
You can include a JSP EL expression in the body of a <jsp:text> tag or any other tag. For example:
<jsp:text> Box heightis: ${2*box.width} </jsp:text>
To deactivate the evaluation of EL expressions, we specify the isELIgnored
attribute of the page directive as below:
<%@ page isELIgnored ="true|false" %>
JSP Expression Language supports most of the arithmetic and logical operators supported by Java.
Operator | Description |
---|---|
. | Access a bean property |
[] | Access an array or List element |
( ) | Group a expression to change the evaluation order |
+ | Add |
- | Subtract |
* | Multiply |
/ or div | Division |
% or mod | Modulo |
== or eq | equality |
!= or ne | Not equality |
< or lt | less than |
> or gt | greater than |
<= or le | less than or equal |
>= or gt | greater than or equal |
&& or and | logical AND |
|| or or | logical OR |
! or not | Unary Boolean complement |
empty | Test for empty variable values |
We can define functions in JSP EL with the following syntax:
${ns:func(param1, param2, ...)}
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 can be used as follows to get the the length of a string.
${fn:length("hello")}
To use a function from tag libraries, install the libraries on your server and include the libraries in your JSP using <taglib> directive.
The JSP expression language supports the following implicit objects:
Implicit object | Description |
---|---|
pageScope | page scoped variables |
requestScope | request scoped variables |
sessionScope | session scoped variables |
applicationScope | application scoped variables |
param | Request parameters as strings |
paramValues | Request parameters as collections of strings |
header | HTTP request headers as strings |
headerValues | HTTP request headers as collections of strings |
initParam | Context-initialization parameters |
cookie | Cookie values |
pageContext | The JSP PageContext object for the current page |
The pageContext object can access pageContext JSP object. For example, to access the incoming query string for a request, use the expression:
${pageContext.request.queryString}
To explicitly access the aVariable
variable in the application scope,
access it through the applicationScope
variable as applicationScope.aVariable
.
The param and paramValues objects can access the parameter values available through the request.getParameter and request.getParameterValues methods.
To access a parameter named myVariable, use the expression ${param.myVariable} or ${param["myVariable"]}.
The following code shows how to access a request parameter named myVariable:
<%@ page import="java.io.*,java.util.*" %> <% String title = "Accessing Request Param"; %> <html> <body> <h1><% out.print(title); %></h1> <p>${param["myVariable"]}</p> </body> </html>
The header and headerValues objects can access the header values available through the request.getHeader and request.getHeaders methods respectively.
To access a header named user-agent, use the expression ${header.user-agent} or ${header["user-agent"]}.
<%@ page import="java.io.*,java.util.*" %> <html> <body> <p>${header["user-agent"]}</p> </body> </html>