0% found this document useful (0 votes)
31 views

Expression Language: Advance Java

Here is the User bean class: package com.catp.beans; public class User { private String name; private String email; private String mobile; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getMobile() { return mobile; } public void setMobile(String mobile) { this.mobile = mobile; } } 25 Lend a Hand : RegistrationServlet
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Expression Language: Advance Java

Here is the User bean class: package com.catp.beans; public class User { private String name; private String email; private String mobile; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getMobile() { return mobile; } public void setMobile(String mobile) { this.mobile = mobile; } } 25 Lend a Hand : RegistrationServlet
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

Advance Java

Expression Language

LEVEL – PRACTITIONER
About the Author

Created By: Renjith(t-renjith)/ Shanmu (105110)

Credential Trainer / Sr Architect


Information:

Version and 1.0, January 17’th 2012


Date:

2
Icons Used

Hands on
Questions Tools Exercise

Coding Test Your Case Study


Standards Understanding

Best Practices
Demonstration & Industry
Workshop
Standards

3
Objectives

After completing this chapter you will be able to


understand:
What is an Expression Language(EL)?
Implicit objects in EL?
EL operators ?

4
Expression Language (EL)

 Expression Language (EL) is a simple language for accessing data stored


in java beans.

 This was introduced with the JSP 2.0 specification.

 It can also be used to access the values from implicit objects like page
context , header , cookie etc.

 Expression languages are always specified within curly braces and prefixed
with a dollar sign.
Example : ${person.name}

5
Advantages of EL

 Concise access to stored objects : Easy to access the attributes stored in session,
request or application scope.
Example :${name} search the PageContext, HttpServletRequest, HttpSession, and
ServletContext (in that order) for an attribute named name.
Equivalent to <%= pageContext.findAttribute("name") %>
 Short hand notation for bean properties : Easy to access bean properties.
Example : To print the property phoneNumber of a userBean we can use $
{user.phoneNumber} instead of <%=user.getPhoneNumber()%>
 Simple access to collection elements To access an element of an array, List, or Map, you
use ${variable[indexOrKey]}.
Example: Consider an arraylist myList
${myList[0]} , Gets the first element in the list and prints it.

6
Advantages of EL (Cont)

 Easy access to request parameters, cookies, and other request data.


To access the standard types of request data, you can use one of
several predefined implicit objects.
 Conditional output :To choose among output options, we do not have to
resort to Java coding elements. Instead, you can use
${test ? option1 : option2}

There are lots of such advantages. You will


come to know when you use it.

7
EL Implicit Objects in a nutshell

Object Description
pageContext The pageContext object refers to the PageContext of the current page.
param To access a single value request parameter.
paramValues Contains the array of parameter values
header Contains the header values as a map
headerValues Contains complete header values
cookie Returns the incoming cookies as a map
initParam For easy access of initialization parameters as a map
pageScope Contains the page scope values as a map
requestScope Contains the request scope values as a map
sessionScope Contains the session scope values as a map
applicationScope Contains the application scope values as a map

You Will learn about few of these in this session


8
What is Period (.) operator?

1. To access Implicit object’s attributes:


For example to read an attribute message in sessionScope we use
${sessionScope.message}
2. To access bean properties:
Consider a bean named person having a property name which can be
accessed as
${person.name}
3. To access map values:
Consider a map named country having a key named ”ind” . Value of the
key can be accessed as
${country. ind}
9
[] Operator

The [ ] operator can be used instead of the period operator, lets look at few
examples,

1.To access beans: ${person[“name”]} reads the property named name


in the person bean. Equivalent to ${person.name}

2.To access Map Values: ${country[“ind”]} retrieves the value for the
key ind in the country map. Equivalent to ${country. Ind}

3.To access elements from arrays and list: ${fruits[1]} retrieves the
value at index 1 in the fruits array (list).

10
EL – Arithmetic Operators

11
EL – Logical Operators

12
EL – Relational Operators

13
How to Read Request Parameter Values ?

Request parameter values can be read as shown

${param.userName} or ${param[“userName”]}

Reads the value of the parameter named userName set in the request

 If a single request parameter name contains a array of values, example


in the case of Check box parameter, we use

${paramValues.hobbies[0]} or ${paramValues.hobbies[0]}

Reads the first value of the check box hobbies.

14
How to read attributes ?

 Consider a scenario in which you need to access an attribute with name


Username stored in some scope we use
${Username}
Searches the PageContext, HttpServletRequest, HttpSession, and
ServletContext (in that order) for an attribute named Username.
 Suppose we if want to get the attribute with name name in a specific scope
say session , we use

${sessionScope.name}

NOTE: Similiarly if you want to access a application scope parameter use


the key word, “applicationScope”

15
How to Access Bean Properties ?

To read a property named firstName in a bean named customer we


use

${customer.firstName}

16
How to access Collection Objects?

Consider attributeName is a scoped variable referring to an array, List or Map, an entry


in the collection can be accessed by

${attributeName[entryName]}

Example 1: consider and a String array customerNames

${customerNames[0]} reads the first element in the array

Example 2: If customerNames is a List object

${customerNames[0]} reads the first element in the list

Example 3: If stateCapitals is a Map object containing the states of


India as key and their capitals as value

${stateCapitals[“Rajasthan”]} returns the capital of Rajasthan.

17
Accessing Implicit Objects using EL in a Nutshell.

18
Deactivating Expression Evaluation

If your application requires EL to be ignored it can be done by setting the


isELIgnoredAttribute to true in the page directive.

<%@ page isELIgnored ="true" %>

Setting this parameter to “True” , EL tags will not be interpreted by the container.
Will print the EL tags as it is in the JSP page.

19
When to Use EL?

EL should be used for accessing and presenting data from implicit objects.

Don’t use EL operators and conditions for performing business logic


which should be strictly done in business components.

Business components: Here refers to the Java classes which is developed


with business logic and are invoked by controllers (Typically servlets)

20
A Simple Lend A Hand On EL.

This is a demo to get familiarized with basic usage of EL .


Objective: you will get familiar with,

1. How to access Implicit object such as params , header , requestScope ,


sessionScope , applicationScope

2. How to access bean properties?

3. Usage of period operator.

4. Usage of [ ] Operator.

21
Lend a Hand - Scenario

Scenario: Building a registration application, let us build a registration form


where the user registers himself. The registered details is then displayed to the
user.

Components to be developed:
1. Registration.jsp : Renders the registration form page
2. RegistrationServlet : Handles the registration process and forwards the
request to success.jsp.
3. User Bean - To hold the user registration details.
4. success.jsp : Prints the user Registration details.
NOTE: For the demo purpose some values irrelevant to the problem statement is
also printed such as the user-agent . This is purely for the associates to
understand.

22
Lend a Hand : Registration page design

The registration page design is as follows.

23
Lend a Hand : registration.jsp code

Develop the registration page.

24
Lend a Hand : User – Bean Class

Create the User bean in a package named com.catp.beans as mentioned


below.

25
Lend a Hand : Develop RegistrationServlet

Develop the registration servlet and set a count variable in request, session and
application context.

Sets a attribute named count to request

Sets a attribute named count to session

Sets a attribute named count to context

Creates user bean object , loads with


request parameter values and sets this
as a attribute to request

26
Lend a Hand : Develop Success.jsp

Reads the user agent value in


the header.

Reads an attribute named count

Reads attribute named count in the various


scope

Reads the request parameter values

Reads values from user bean object

27
Lend a Hand : Success.jsp

User agent value in the header . Can be


printed using ${header["user-agent"]}

Count attribute printed using ${count} . The


value in the request object will be printed
since the order is request-session-application

The values is the application , session and


context scopes printed using
${applicationScope.count}
${sessionScope.count}
${requestScope.count}

Parameter values printed using param object


using ${param.fname}

Parameter values printed using bean object


using ${userBean.fName}
Lend a Hand – Deploy and Run

Step 1 : Deploy and run the application


Step 2 : Invoke registration.jsp from the browser
http://localhost:8080/ELDemo/registration.jsp
Step 2 : Look at the details displayed in the success page,
•Details displayed from the header implicit object.
•Details printed from request object.
•Details displayed from session object.
•Details displayed from application object.
•Details displayed from user bean object.

29
Advance Java

You have successfully completed –


Expression Language

You might also like