Expression Language in JSP

2,537 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
2,537
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
17
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Expression Language in JSP

  1. 1. Expression Language Cornelius Koo, ST JavaSchool 2005 Jl. Cemara 2/20, Salatiga
  2. 2. Why EL
  3. 3. 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);}
  4. 4. <body><%= ((jsp.example.bean.Human) request.getAttribute("person")).getDog().get Name() %><jsp:useBean id="person" class="jsp.example.bean.Human" scope="request"/> Dogs name is : <jsp:getProperty name="person" property="dog"/></body>
  5. 5. Result• Spike Dogs name is : jsp.example.bean.Dog@1e0e954
  6. 6. EL• Dogs name is : ${person.dog.name}
  7. 7. Result• Dogs name is : Spike
  8. 8. EL Rules
  9. 9. 1st n 2nd• ${ firstThing.secondThing }• firstThing -> EL Implicit Object or attribute• secondThing -> a property
  10. 10. 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
  11. 11. Accessing EL
  12. 12. If the expression has a variable followed by a dot, the left-handvariable MUST be a Map or a bean
  13. 13. The Thing on the right must follow normal Java naming rules foridentifiers
  14. 14. 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.
  15. 15. If the thing inside the brackets is a String literal (i.e., in quotes), itcan be a Map key or a bean property, or an index into a List or array.
  16. 16. • In ServletString[] nameList = { “Aan”, “Sam”, “John”};request.setAttribute(“name”, nameList);
  17. 17. • In JSPName : ${name}Name : ${name[0]}Name : ${name[“0”]}
  18. 18. Using [] for bean
  19. 19. • In ServletMap musicMap = new HashMap();musicMap.put(“Ambient”, “Zero”);request.setAttribute(“musicMap”,musicMap);
  20. 20. • In JSP• ${musicMap.Ambient}• ${musicMap[“Ambient”]}
  21. 21. What About This ?• ${musicMap[Ambient]}• This time with no “ “
  22. 22. • In ServletMap musicMap = new HashMap();musicMap.put(“Ambient”, “Zero”);request.setAttribute(“musicMap”,musicMap);request.setAttribute(“Genre”,”Ambient”);
  23. 23. • 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”
  24. 24. Nesting• ${musicMap[ musicType[0] ]}• ${musicMap[“Ambient”]}• Zero 7
  25. 25. 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}
  26. 26. Request Parameter in EL
  27. 27. • In HTML<form action=“TestBean.jsp”> Name : <input type=“text” name=“name”> ID# : <input type=“text” name=“empID”> <input type=“submit”></form>
  28. 28. • In JSP• ${param.name}• ${param.empID}
  29. 29. Request Method• With expression<%= request.getMethod() %>• With EL${pageContext.request.method}
  30. 30. Getting Request Attribute• In Servletrequest.setAttribute(“person”, p);• Get it with EL${requestScope[“person”].name}
  31. 31. Header Information
  32. 32. • With expression<%= request.getHeader(“host”) %>• With EL${header.host}${header.[“host”]}
  33. 33. Cookie
  34. 34. • 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}
  35. 35. Init Parameter
  36. 36. • 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}
  37. 37. Method in EL
  38. 38. package jsp.example.method;public class DiceRoller { public static int rollDice() { return (int) ((Math.random()*6)+1); }}The method must be public and static
  39. 39. 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>
  40. 40. method.jsp<%@ taglib prefix="method" uri="DiceFunctions" %><html><head><title>EL Method</title></head><body> ${method:rollIt()}</body></html>
  41. 41. ELOperator
  42. 42. Arithmetic• +• -• *• /• %
  43. 43. Logical• && or and• || or or• ! or not
  44. 44. Relational• == or eq• != or ne• < or lt• > or gt• <= or le• >= or ge

×