Expression Language in JSP

44
Expression Language Cornelius Koo, ST JavaSchool 2005 Jl. Cemara 2/20, Salatiga

Transcript of Expression Language in JSP

Page 1: Expression Language in JSP

Expression Language

Cornelius Koo, ST

JavaSchool

2005

Jl. Cemara 2/20, Salatiga

Page 2: Expression Language in JSP

Why EL

Page 3: Expression Language in JSP

public void doPost( HttpServletRequest req,

HttpServletResponse res) throws ServletException, IOException {

Human p = new Human();

p.setName("Evan");

Dog dog = new Dog();

dog.setName("Spike");

p.setDog(dog);

req.setAttribute("person", p);

RequestDispatcher rd =

req.getRequestDispatcher("withscript.jsp");

rd.forward(req,res);

}

Page 4: Expression Language in JSP

<body>

<%= ((jsp.example.bean.Human)

request.getAttribute("person")).getDog().get

Name() %>

<jsp:useBean id="person"

class="jsp.example.bean.Human"

scope="request"/>

Dog's name is : <jsp:getProperty

name="person" property="dog"/>

</body>

Page 5: Expression Language in JSP

Result

• Spike Dog's name is :

jsp.example.bean.Dog@1e0e954

Page 6: Expression Language in JSP

EL

• Dog's name is : ${person.dog.name}

Page 7: Expression Language in JSP

Result

• Dog's name is : Spike

Page 8: Expression Language in JSP

EL Rules

Page 9: Expression Language in JSP

1st n 2nd

• ${ firstThing.secondThing }

• firstThing -> EL Implicit Object or attribute

• secondThing -> a property

Page 10: Expression Language in JSP

EL Implicit Objects

• pageScope map objects

• requestScope map objects

• sessionScope map objects

• applicationScope map objects

• param map objects

• paramValues map objects

• header map objects

• headerValues map objects

• cookie map objects

• initParam map objects

• pageContext ->real reference to pageContext object

Page 11: Expression Language in JSP

Accessing EL

Page 12: Expression Language in JSP

If the expression has a variable followed by a dot, the left-hand

variable MUST be a Map or a bean

Page 13: Expression Language in JSP

The Thing on the right must follow normal Java naming rules for

identifiers

Page 14: Expression Language in JSP

If the expression has a variable followed by a bracket [ ], the left-

hand variable can be a Map, a bean, a List or an array.

Page 15: Expression Language in JSP

If the thing inside the brackets is a String literal (i.e., in quotes), it

can be a Map key or a bean property, or an index into a List or array.

Page 16: Expression Language in JSP

• In Servlet

String[] nameList = { “Aan”, “Sam”, “John”};

request.setAttribute(“name”, nameList);

Page 17: Expression Language in JSP

• In JSP

Name : ${name}

Name : ${name[0]}

Name : ${name[“0”]}

Page 18: Expression Language in JSP

Using [] for bean

Page 19: Expression Language in JSP

• In Servlet

Map musicMap = new HashMap();

musicMap.put(“Ambient”, “Zero”);

request.setAttribute(“musicMap”,musicMap);

Page 20: Expression Language in JSP

• In JSP

• ${musicMap.Ambient}

• ${musicMap[“Ambient”]}

Page 21: Expression Language in JSP

What About This ?

• ${musicMap[Ambient]}

• This time with no “ “

Page 22: Expression Language in JSP

• In Servlet

Map musicMap = new HashMap();

musicMap.put(“Ambient”, “Zero”);

request.setAttribute(“musicMap”,musicMap);

request.setAttribute(“Genre”,”Ambient”);

Page 23: Expression Language in JSP

• In JSP

• ${musicMap[“Genre”]} -> doesn’t work

• ${musicMap[Genre]} -> evaluated

because there’s an attribute object named

Genre and it has a value named “Ambient”

Page 24: Expression Language in JSP

Nesting

• ${musicMap[ musicType[0] ]}

• ${musicMap[“Ambient”]}

• Zero 7

Page 25: Expression Language in JSP

Don’t try…

• Don’t put everything that are not qualified

as an identifiers behind the dot (.)

operator!

• ${musicMap[“Ambient”]} -> ${musicMap.Ambient}

• ${musicList[“1”]} -> X ${musicList.1}

Page 26: Expression Language in JSP

Request Parameter in EL

Page 27: Expression Language in JSP

• In HTML

<form action=“TestBean.jsp”>

Name : <input type=“text” name=“name”>

ID# : <input type=“text” name=“empID”>

<input type=“submit”>

</form>

Page 28: Expression Language in JSP

• In JSP

• ${param.name}

• ${param.empID}

Page 29: Expression Language in JSP

Request Method

• With expression

<%= request.getMethod() %>

• With EL

${pageContext.request.method}

Page 30: Expression Language in JSP

Getting Request Attribute

• In Servlet

request.setAttribute(“person”, p);

• Get it with EL

${requestScope[“person”].name}

Page 31: Expression Language in JSP

Header Information

Page 32: Expression Language in JSP

• With expression

<%= request.getHeader(“host”) %>

• With EL

${header.host}

${header.[“host”]}

Page 33: Expression Language in JSP

Cookie

Page 34: Expression Language in JSP

• With scriptlet

<% Cookie[] cookies = request.getCookies();

for(int i=0; i < cookie.length; i++) {

if ((cookies[i].getName()).

equals(“username”)) {

out.println(cookies[i].getValue());

}

}

%>

• With EL

${cookie.userName.value}

Page 35: Expression Language in JSP

Init Parameter

Page 36: Expression Language in JSP

• DD

<context-param>

<param-name>name</param-name>

<param-value>Rod Johnson</param-value>

</context-param>

• With expression<%= application.getInitParameter(“name”) %>

• With EL

${initParam.name}

Page 37: Expression Language in JSP

Method in EL

Page 38: Expression Language in JSP

package jsp.example.method;

public class DiceRoller {

public static int rollDice() {

return (int) ((Math.random()*6)+1);

}

}

The method must be public and static

Page 39: Expression Language in JSP

el.tld

<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE taglib PUBLIC

"-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"

"http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">

<taglib>

<tlibversion>1.2</tlibversion>

<jspversion>2.0</jspversion>

<uri>DiceFunctions</uri>

<function>

<name>rollIt</name>

<function-class>jsp.example.method.DiceRoller</function-class>

<function-signature>int rollDice()</function-signature>

</function>

</taglib>

Page 40: Expression Language in JSP

method.jsp

<%@ taglib prefix="method" uri="DiceFunctions" %>

<html>

<head>

<title>EL Method</title>

</head>

<body>

${method:rollIt()}

</body>

</html>

Page 41: Expression Language in JSP

ELOperator

Page 42: Expression Language in JSP

Arithmetic

• +

• -

• *

• /

• %

Page 43: Expression Language in JSP

Logical

• && or and

• || or or

• ! or not

Page 44: Expression Language in JSP

Relational

• == or eq

• != or ne

• < or lt

• > or gt

• <= or le

• >= or ge