Expression Language in JSP
-
Upload
corneliuskoo -
Category
Documents
-
view
2.181 -
download
0
Transcript of Expression Language in JSP
Expression Language
Cornelius Koo, ST
JavaSchool
2005
Jl. Cemara 2/20, Salatiga
Why EL
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);
}
<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>
Result
• Spike Dog's name is :
jsp.example.bean.Dog@1e0e954
EL
• Dog's name is : ${person.dog.name}
Result
• Dog's name is : Spike
EL Rules
1st n 2nd
• ${ firstThing.secondThing }
• firstThing -> EL Implicit Object or attribute
• secondThing -> a property
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
Accessing EL
If the expression has a variable followed by a dot, the left-hand
variable MUST be a Map or a bean
The Thing on the right must follow normal Java naming rules for
identifiers
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.
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.
• In Servlet
String[] nameList = { “Aan”, “Sam”, “John”};
request.setAttribute(“name”, nameList);
• In JSP
Name : ${name}
Name : ${name[0]}
Name : ${name[“0”]}
Using [] for bean
• In Servlet
Map musicMap = new HashMap();
musicMap.put(“Ambient”, “Zero”);
request.setAttribute(“musicMap”,musicMap);
• In JSP
• ${musicMap.Ambient}
• ${musicMap[“Ambient”]}
What About This ?
• ${musicMap[Ambient]}
• This time with no “ “
• In Servlet
Map musicMap = new HashMap();
musicMap.put(“Ambient”, “Zero”);
request.setAttribute(“musicMap”,musicMap);
request.setAttribute(“Genre”,”Ambient”);
• 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”
Nesting
• ${musicMap[ musicType[0] ]}
• ${musicMap[“Ambient”]}
• Zero 7
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}
Request Parameter in EL
• In HTML
<form action=“TestBean.jsp”>
Name : <input type=“text” name=“name”>
ID# : <input type=“text” name=“empID”>
<input type=“submit”>
</form>
• In JSP
• ${param.name}
• ${param.empID}
Request Method
• With expression
<%= request.getMethod() %>
• With EL
${pageContext.request.method}
Getting Request Attribute
• In Servlet
request.setAttribute(“person”, p);
• Get it with EL
${requestScope[“person”].name}
Header Information
• With expression
<%= request.getHeader(“host”) %>
• With EL
${header.host}
${header.[“host”]}
Cookie
• 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}
Init Parameter
• 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}
Method in EL
package jsp.example.method;
public class DiceRoller {
public static int rollDice() {
return (int) ((Math.random()*6)+1);
}
}
The method must be public and static
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>
method.jsp
<%@ taglib prefix="method" uri="DiceFunctions" %>
<html>
<head>
<title>EL Method</title>
</head>
<body>
${method:rollIt()}
</body>
</html>
ELOperator
Arithmetic
• +
• -
• *
• /
• %
Logical
• && or and
• || or or
• ! or not
Relational
• == or eq
• != or ne
• < or lt
• > or gt
• <= or le
• >= or ge