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.

Slides:



Advertisements
Similar presentations
9 Copyright © 2005, Oracle. All rights reserved. Modularizing JavaServer Pages Development with Tags.
Advertisements

8 Copyright © 2005, Oracle. All rights reserved. Creating the Web Tier: JavaServer Pages.
JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
© Yaron Kanza Advanced Java Server Pages Written by Dr. Yaron Kanza, Edited by permission from author by Liron Blecher.
Expression Language Lec Umair Javed©2006 Generating Dynamic Contents Technologies available  Servlets  JSP  JavaBeans  Custom Tags  Expression.
 Copyright Wipro Technologies JSP Ver 1.0 Page 1 Talent Transformation Java Server Pages.
Chapter 51 Scripting With JSP Elements JavaServer Pages By Xue Bai.
JSP: JavaServer Pages Juan Cruz Kevin Hessels Ian Moon.
CS320 Web and Internet Programming Java Beans and Expression Language (EL) Chengyu Sun California State University, Los Angeles.
DT211/3 Internet Application Development
DT228/3 Web Development JSP: Directives and Scripting elements.
Introduction to Java web programming Dr Jim Briggs JWP intro1.
 2004 Prentice Hall, Inc. All rights reserved. Chapter 25 – Perl and CGI (Common Gateway Interface) Outline 25.1 Introduction 25.2 Perl 25.3 String Processing.
Overview of JSP Technology. The need of JSP With servlets, it is easy to – Read form data – Read HTTP request headers – Set HTTP status codes and response.
1 CIS336 Website design, implementation and management (also Semester 2 of CIS219, CIS221 and IT226) Lecture 9 JavaServer Pages (JSP) (Based on Møller.
JSP Standard Tag Library
PHP Overview CS PHP PHP = PHP: Hypertext Preprocessor Server-side scripting language that may be embedded into HTML One goal is to get PHP files.
Intermediate JSP Matt Wheeler. Notes This is a training NOT a presentation If you have questions please ask them Prerequisites – Introduction to Java.
JavaScript, Fifth Edition Chapter 1 Introduction to JavaScript.
CSC 2720 Building Web Applications Using Java Beans, Custom Tags and Tag Libraries in JSP pages.
Using JavaBeans and Custom Tags in JSP Lesson 3B / Slide 1 of 37 J2EE Web Components Pre-assessment Questions 1.The _____________ attribute of a JSP page.
JavaServer Faces Jeff Schmitt October 5, Introduction to JSF Presents a standard framework for building presentation tiers for web applications.
Introduction to Java Server Pages (JSPs) Robert Thornton.
Introduction to JavaServer Pages (JSP) Slides from Dr. Mark Llewellyn.
Jordan Anastasiade. All rights reserved.
Organization of the platform Your application Java language Java Servlet API JavaServer Pages (JSP) JSTL Your web pages.
® IBM Software Group © 2007 IBM Corporation JSP Expression Language
JAVA SERVER PAGES. 2 SERVLETS The purpose of a servlet is to create a Web page in response to a client request Servlets are written in Java, with a little.
Stanisław Osiński, 2002JSP – A technology for serving dynamic web content Java Server Pages™ A technology for serving dynamic web content Stanisław Osiński,
JAVA SERVER PAGES CREATING DYNAMIC WEB PAGES USING JAVA James Faeldon CS 119 Enterprise Systems Programming.
16-Oct-15 JSP Implicit Objects. 2 JSP Implicit Objects are the Java objects that the JSP Container makes available to developers in each page and developer.
JSTL: The JavaServer Pages Standard Tag Library Mark A. Kolb Security Broadband, Austin, TX
JSTL Lec Umair©2006, All rights reserved JSTL (ni) Acronym of  JavaServer Pages Standard Tag Library JSTL (like JSP) is a specification, not an.
Fall 2007cs4201 Advanced Java Programming Umar Kalim Dept. of Communication Systems Engineering
Introduction to JavaServer Pages. 2 JSP and Servlet Limitations of servlet  It’s inaccessible to non-programmers JSP is a complement to servlet  focuses.
CS320 Web and Internet Programming Java Beans and Expression Language (EL) Chengyu Sun California State University, Los Angeles.
CSC 2720 Building Web Applications JavaServer Pages (JSP) The Basics.
JSP Tag Libraries Lec Last Lecture Example We incorporated JavaBeans in “Course Outline” Example But still have to write java code inside java.jsp.
COMP 321 Week 10. Overview Using Beans in JSP Expression Language JSTL Lab 10-1 Introduction Exam Review.
Copyright © 2002 ProsoftTraining. All rights reserved. JavaServer Pages.
JSTL The JavaServer Pages Standard Tag Library (JSTL) is a collection of useful JSP tags which encapsulates core functionality common to many JSP applications.
Chapter 11 Invoking Java Code with JSP Scripting Elements.
JSP Custom Tags. Prerequisites Servlet API Mapping to JSP implicit objects JavaServer Pages Basic syntax Implementation via servlet API XML.
Fall 2007cs4201 Advanced Java Programming Umar Kalim Dept. of Communication Systems Engineering
JSP Pages. What and Why of JSP? JSP = Java code imbedded in HTML or XML –Static portion of the page is HTML –Dynamic portion is Java Easy way to develop.
EL and JSTL. JSTL Sources
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
1 Web Programming with Servlets & JSP ASSIGNMENT GUIDELINE.
JAVA BEANS JSP - Standard Tag Library (JSTL) JAVA Enterprise Edition.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 JSP Application Models.
Basic JSP Celsina Bignoli Problems with Servlets Servlets contain –request processing, –business logic –response generation all lumped.
COMP9321 Web Application Engineering Semester 2, 2015 Dr. Amin Beheshti Service Oriented Computing Group, CSE, UNSW Australia Week 3 1COMP9321, 15s2, Week.
1 Java Server Pages A Java Server Page is a file consisting of HTML or XML markup into which special tags and code blocks are inserted When the page is.
Chapter 6 Chapter 6 Server Side Programming (JSP) Part 1 1 (IS 203) WebProgramming (IS 203) Web Programming.
Intermediate JSP Matt Wheeler. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Java Stack – Basic Java.
Java Programming: Advanced Topics 1 Building Web Applications Chapter 13.
Java Server Pages. 2 Servlets The purpose of a servlet is to create a Web page in response to a client request Servlets are written in Java, with a little.
Chapter 14 Using JavaBeans Components in JSP Documents.
10 Copyright © 2004, Oracle. All rights reserved. Building ADF View Components.
JSP Elements Chapter 2.
The Expression Language Syntax
CS520 Web Programming Servlet and JSP Review
JSP: Actions elements and JSTL
Knowledge Byte In this section, you will learn about:
Invoking Java Code from JSP
JSP Standard Tag Library
CS320 Web and Internet Programming Expression Language (EL)
CS3220 Web and Internet Programming Expression Language (EL)
CS3220 Web and Internet Programming Expression Language (EL)
Presentation transcript:

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

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.

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.

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}

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:

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.

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

A RITHMETIC OPERATORS Before JSP2.0/JSTL <%! int k = 0; %> <% k = ; out.println("value of k is.."+k); %> Using JSP2.0/JSTL (without using scriptlet) value of k is ${ } Few more examples on arithmetic operations. EL Expression Result ${ } O/P: 30.5 ${ }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

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

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.

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")}

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.

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.

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}

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.

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:

taglib prefix="c" uri=" %> T HE PARAM AND PARAM V ALUES O BJECTS : The param object returns single string values, whereas the paramValues object returns string arrays.

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:

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

User Agent Example Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2;.NET CLR ;.NET CLR ;.NET CLR ; Media Center PC 6.0; HPNTDF;.NET4.0C; InfoPath.2) The header object returns single string values, whereas the headerValues object returns string arrays.

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.

THANK YOU…