Presentation is loading. Please wait.

Presentation is loading. Please wait.

JSP Expression Language (EL) 25-Nov-15. JSP - E XPRESSION L ANGUAGE (EL) Introduction Expression Language was first introduced in JSTL 1.0 (JSP Standard.

Similar presentations


Presentation on theme: "JSP Expression Language (EL) 25-Nov-15. JSP - E XPRESSION L ANGUAGE (EL) Introduction Expression Language was first introduced in JSTL 1.0 (JSP Standard."— Presentation transcript:

1 JSP Expression Language (EL) 25-Nov-15

2 JSP - E XPRESSION L ANGUAGE (EL) Introduction Expression Language was first introduced in JSTL 1.0 (JSP Standard Tag Library ). Before the introduction of JSTL, scriptlets were used to manipulate application data. JSTL introduced the concept of an expression language (EL) which simplified the page development by providing standard tag libraries. These tag libraries provide support for common, structural tasks, such as: iteration and conditionals, processing XML documents, internationalization and database access using the Structured Query Language (SQL). The Expression Language introduced in JSTL 1.0 is now incorporated in JavaServer Pages specification(JSP 2.0). This presentation gives some idea about what is Expression Language and how to simplify the maintenance for JSP applications by avoiding scripting elements.

3 JSP - E XPRESSION L ANGUAGE (EL) A primary feature of JSP technology version 2.0 is its support for an expression language (EL). An expression language makes it possible to easily access application data stored in JavaBeans components. For example, the JSP expression language allows a page author to access a bean using simple syntax such as ${name} for a simple variable or ${name.foo.bar} for a nested property. 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.

4 S IMPLE S YNTAX : Typically, when you specify an attribute value in a JSP tag, you simply use a string. For example: ${expr} JSP EL allows you to specify an expression for any of these attribute values. A simple syntax for JSP EL is as follows: 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 above syntax 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.

5 S IMPLE S YNTAX : You can also use JSP EL expressions within template text for a tag. For example, the tag simply inserts its content within the body of a JSP. The following declaration inserts Hello JSP! into the JSP output: You can include a JSP EL expression in the body of a tag (or any other tag) with the same ${} syntax you use for attributes. For example: EL expressions can use parentheses to group sub expressions. For example, ${(1 + 2) * 3} equals 9, but ${1 + (2 * 3)} equals 7. Hello JSP! Box Perimeter is: ${2*box.width + 2*box.height}

6 The test attribute of the following conditional tag is supplied with an EL expression that compares the number of items in the session-scoped bean named cart with 0: 0}">... The JSP expression evaluator is responsible for handling EL expressions, which are enclosed by the ${ } characters and can include literals. Here's an example:... Any value that does not begin with ${ is treated as a literal and is parsed to the expected type using the PropertyEditor for the type:... Literal values that contain the ${ characters must be escaped as follows:

7 D EACTIVATING E XPRESSION E VALUATION Because the pattern that identifies EL expressions--${ }--was not reserved in the JSP specifications before JSP 2.0, there may be applications where such a pattern is intended to pass through verbatim. To prevent the pattern from being evaluated, you can deactivate EL evaluation. To deactivate the evaluation of EL expressions, you specify the isELIgnored attribute of the page directive: 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. The default value varies depending on the version of the web application deployment descriptor. The default mode for JSP pages delivered using a Servlet 2.3 or earlier descriptor is to ignore EL expressions; this provides backward compatibility. The default mode for JSP pages delivered with a Servlet 2.4 descriptor is to evaluate EL expressions; this automatically provides the default that most applications want.

8 B ASIC O PERATORS IN EL: JSP Expression Language (EL) supports most of the arithmetic and logical operators supported by Java. Below is the list of most frequently used operators: OperatorDescription.Access a bean property or Map entry []Access an array or List element ( )Group a subexpression to change the evaluation order +Addition -Subtraction or negation of a value *Multiplication / or divDivision % or modModulo (remainder) == or eqTest for equality != or neTest for inequality < or ltTest for less than > or gtTest for greater than <= or leTest for less than or equal >= or gtTest for greater than or equal && or andTest for logical AND || or orTest for logical OR ! or notUnary Boolean complement emptyTest for null, empty String, array or Collection. func(args)A function call

9 A RITHMETIC OPERATORS Before JSP2.0/JSTL <%! int k = 0; %> <% k = 10 + 20; out.println("value of k is.."+k); %> Using JSP2.0/JSTL (without using scriptlet) value of k is ${10 + 20} Few more examples on arithmetic operations. EL Expression Result ${10.2 + 20.3} O/P: 30.5 ${-40 - 20}O/P: -60 ${20 * 2}O/P: 40 ${3/4}O/P: 0.75 ${3 div 4}O/P: 0.75 ${10/0}O/P: Infinity ${50%8}O/P: 2 ${50 mod 8}O/P: 2 ${(10==20) ? "true" : "false"}O/P: false

10 COMPARISON OPERATORS Numeric EL Expression Result ${10 < 20} true ${10 lt 20} true ${1 > (4/2)} false ${1 gt (4/2)} false ${4.0 >= 3} true ${4.0 ge 3} true ${40 <= 30} false ${40 le 30} false ${10 == 10} true ${100 eq 100} true ${(5*20) != 100} false ${(5*20) ne 100} false Alphabetic EL ExpressionResult ${'a' < 'b'} true ${'4' > 3} true

11 F UNCTIONS IN JSP EL JSP EL allows you to use functions in expressions as well. These functions must be defined in 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 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 directive as explained in JSTL chapter. Defining Functions: The JSP expression language allows you to define a function that can be invoked in an expression. Functions are defined using the same mechanisms as custom tags.

12 public class MyFunctions { public static String toCaps( String text ) { return text.toUpperCase(); } } Using Functions: XML: testFunctionsURI toCaps mypkg.MyFunctions String toCaps( java.lang.String) JSP: ${myFunctionTest.toCaps("testingCaps")}

13 JSP EL I MPLICIT O BJECTS : The JSP expression language supports the following implicit objects: Implicit objectDescription pageScopeScoped variables from page scope requestScopeScoped variables from request scope sessionScopeScoped variables from session scope applicationScopeScoped variables from application scope paramRequest parameters as strings paramValuesRequest parameters as collections of strings headerHTTP request headers as strings headerValuesHTTP request headers as collections of strings initParamContext-initialization parameters cookieCookie values pageContextThe JSP PageContext object for the current page You can use these objects in an expression as if they were variables.

14 JSP EL I MPLICIT O BJECTS : Note: EL impilcit objects are not as same as the implicit objects available for JSP scripting except for pageContext. JSP and EL implicit objects have only one object in common (pageContext),and pageContext has properties for accessing all of the other eight JSP implicit objects. The first four maps represent the various attribute scopes which can be used to look up identifiers in specific scopes. The next four maps are for fetching the values of request parameters and headers.The cookie implicit object provides access to the cookies set by a request and the final EL implicit object, initParam, is a map storing the names and values of any context initialization parameters associated with the Web application. Lets look at few examples on how to use implicit objects.

15 T HE PAGE C ONTEXT O BJECT : 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 expression: ${pageContext.request.queryString}

16 T HE S COPE O BJECTS : 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.

17 T HE PARAM AND PARAM V ALUES O BJECTS : 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:

18 <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %> T HE PARAM AND PARAM V ALUES O BJECTS : The param object returns single string values, whereas the paramValues object returns string arrays.

19 HEADER AND HEADER V ALUES O BJECTS : The header and headerValues objects give you access to the header values normally available through the request.getHeader and 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:

20 header and headerValues Objects: This would display something as follows:

21 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.

22 Conclusion By using this expression language, page authors could drastically reduce the amount of scripting in their pages, resulting in greater productivity, easier maintenance, and a flatter learning curve in terms of page development. Over the years, the expression language has evolved to include more advanced functionality, while still maintaining its simplicity.

23 THANK YOU…


Download ppt "JSP Expression Language (EL) 25-Nov-15. JSP - E XPRESSION L ANGUAGE (EL) Introduction Expression Language was first introduced in JSTL 1.0 (JSP Standard."

Similar presentations


Ads by Google