YouTip LogoYouTip

Jsp Expression Language

JSP Expression Language (EL) makes it very easy to access data stored in JavaBeans. JSP EL can be used to create both arithmetic and logical expressions. Within a JSP EL expression, you can use integers, floating-point numbers, strings, the constants true and false, and null. * * * ## A Simple Syntax Typically, when you need to specify an attribute value in a JSP tag, you simply use a string: JSP EL allows you to specify an expression to represent the attribute value. A simple expression syntax is as follows: ${expr} Here, expr refers to the expression. The common operators in JSP EL are . and {} . These two operators allow you to access various JavaBean properties through embedded JSP objects. For example, the tag above can be rewritten using expression language as follows: When the JSP compiler sees the "${}" format in an attribute, it generates code to evaluate this expression and produces a substitute for the expression's value. You can also use expression language in the template text of a tag. For example, the tag simply inserts the text in its body into the JSP output:

Hello JSP!

Now, using an expression in the body of the tag, like this: Box Perimeter is: ${2*box.width + 2*box.height} Parentheses can be used in EL expressions to group sub-expressions. For example, ${(1 + 2) * 3} equals 9, but ${1 + (2 * 3)} equals 7. To disable the evaluation of EL expressions, you need to set the isELIgnored attribute value to true using the page directive: This way, EL expressions will be ignored. If set to false, the container will evaluate the EL expressions. * * * ## Basic Operators in EL EL expressions support most of the arithmetic and logical operators provided by Java: | **Operator** | **Description** | | --- | --- | | . | Access a Bean property or a map entry | | [] | Access an array or list element | | ( ) | Group a sub-expression to change precedence | | + | Addition | | - | Subtraction or negation | | * | Multiplication | | / or div | Division | | % or mod | Modulus | | == or eq | Test for equality | | != or ne | Test for inequality | | or gt | Test for greater than | | = or ge | Test for greater than or equal to | | && or and | Test for logical AND | | || or or | Test for logical OR | | ! or not | Test for logical NOT | | empty | Test for empty value | * * * ## Functions in JSP EL JSP EL allows you to use functions in expressions. These functions must be defined in a custom tag library. The syntax for using a function is as follows: ${ns:func(param1, param2, ...)} ns refers to the namespace, func refers to the function name, param1 refers to the first parameter, param2 refers to the second parameter, and so on. For example, there is a function fn:length defined in the JSTL library, which can be used to get the length of a string like this: ${fn:length("Get my length")} To use functions from any tag library, you need to install these libraries on the server and then include them in the JSP file using the tag. * * * ## JSP EL Implicit Objects JSP EL supports the implicit objects listed in the following table: | **Implicit Object** | **Description** | | --- | --- | | pageScope | Page scope | | requestScope | Request scope | | sessionScope | Session scope | | applicationScope | Application scope | | param | Request object's parameter, String | | paramValues | Request object's parameter, String array | | header | HTTP header, String | | headerValues | HTTP header, String array | | initParam | Context initialization parameter | | cookie | Cookie value | | pageContext | The pageContext object of the current page | You can use these objects in expressions just like variables. A few examples will be given next to better understand this concept. * * * ## The pageContext Object The pageContext object is a reference to the pageContext object in JSP. Through the pageContext object, you can access the request object. For example, to access the query string passed in the request object, like this: ${pageContext.request.queryString} * * * ## Scope Objects The pageScope, requestScope, sessionScope, and applicationScope variables are used to access variables stored at various scope levels. For example, if you need to explicitly access the box variable at the applicationScope level, you can do so like this: applicationScope.box. * * * ## param and paramValues Objects The param and paramValues objects are used to access parameter values, using the request.getParameter method and the request.getParameterValues method. For example, to access a parameter named order, you can use the expression: ${param.order}, or ${param}. The following example demonstrates how to access the username parameter in the request:

${param}

The param object returns a single string, while the paramValues object returns an array of strings. * * * ## header and headerValues Objects The header and headerValues objects are used to access headers, using the request.getHeader method and the request.getHeaders method. For example, to access a header named user-agent, you can use the expression: ${header.user-agent}, or ${header}. The following example demonstrates how to access the user-agent header:

${header}

The result is as follows: ![Image 1: jsp-expression-language](#) The header object returns a single value, while the headerValues object returns an array of strings.
← Jsp Exception HandlingJsp Custom Tags β†’