SlideShare ist ein Scribd-Unternehmen logo
1 von 44
Downloaden Sie, um offline zu lesen
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

Weitere ähnliche Inhalte

Was ist angesagt? (20)

Java server pages
Java server pagesJava server pages
Java server pages
 
JSP Standard Tag Library
JSP Standard Tag LibraryJSP Standard Tag Library
JSP Standard Tag Library
 
Jsp
JspJsp
Jsp
 
Wt unit 2 ppts client side technology
Wt unit 2 ppts client side technologyWt unit 2 ppts client side technology
Wt unit 2 ppts client side technology
 
Jsp
JspJsp
Jsp
 
Jsp
JspJsp
Jsp
 
JSP - Java Server Page
JSP - Java Server PageJSP - Java Server Page
JSP - Java Server Page
 
Jsp1
Jsp1Jsp1
Jsp1
 
Jsp presentation
Jsp presentationJsp presentation
Jsp presentation
 
KAAccessControl
KAAccessControlKAAccessControl
KAAccessControl
 
Jsp elements
Jsp elementsJsp elements
Jsp elements
 
Jsp element
Jsp elementJsp element
Jsp element
 
Wt unit 4
Wt unit 4Wt unit 4
Wt unit 4
 
Java Server Pages
Java Server PagesJava Server Pages
Java Server Pages
 
Jquery 4
Jquery 4Jquery 4
Jquery 4
 
Unified Expression Language
Unified Expression LanguageUnified Expression Language
Unified Expression Language
 
Servlet and jsp interview questions
Servlet and jsp interview questionsServlet and jsp interview questions
Servlet and jsp interview questions
 
Lap trinh web [Slide jsp]
Lap trinh web [Slide jsp]Lap trinh web [Slide jsp]
Lap trinh web [Slide jsp]
 
jQuery from the very beginning
jQuery from the very beginningjQuery from the very beginning
jQuery from the very beginning
 
ERRest: the Basics
ERRest: the BasicsERRest: the Basics
ERRest: the Basics
 

Andere mochten auch

Andere mochten auch (7)

09 Display
09 Display09 Display
09 Display
 
04 J2ME Wireless Tool Kit
04 J2ME Wireless Tool Kit04 J2ME Wireless Tool Kit
04 J2ME Wireless Tool Kit
 
JSP
JSPJSP
JSP
 
Html Hands On
Html Hands OnHtml Hands On
Html Hands On
 
13 Low Level UI Event Handling
13 Low Level UI Event Handling13 Low Level UI Event Handling
13 Low Level UI Event Handling
 
J2ME
J2MEJ2ME
J2ME
 
J2ME
J2MEJ2ME
J2ME
 

Ähnlich wie Expression Language in JSP

appengine java night #1
appengine java night #1appengine java night #1
appengine java night #1Shinichi Ogawa
 
Introducing CakeEntity
Introducing CakeEntityIntroducing CakeEntity
Introducing CakeEntityBasuke Suzuki
 
Tutorial, Part 3: SharePoint 101: Jump-Starting the Developer by Rob Windsor ...
Tutorial, Part 3: SharePoint 101: Jump-Starting the Developer by Rob Windsor ...Tutorial, Part 3: SharePoint 101: Jump-Starting the Developer by Rob Windsor ...
Tutorial, Part 3: SharePoint 101: Jump-Starting the Developer by Rob Windsor ...SPTechCon
 
Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02Seri Moth
 
JavaScript and the AST
JavaScript and the ASTJavaScript and the AST
JavaScript and the ASTJarrod Overson
 
jQuery Data Manipulate API - A source code dissecting journey
jQuery Data Manipulate API - A source code dissecting journeyjQuery Data Manipulate API - A source code dissecting journey
jQuery Data Manipulate API - A source code dissecting journeyHuiyi Yan
 
Propel sfugmd
Propel sfugmdPropel sfugmd
Propel sfugmdiKlaus
 
Casting for not so strange actors
Casting for not so strange actorsCasting for not so strange actors
Casting for not so strange actorszucaritask
 
jQuery - Introdução
jQuery - IntroduçãojQuery - Introdução
jQuery - IntroduçãoGustavo Dutra
 
JQuery do dia-a-dia Gustavo Dutra
JQuery do dia-a-dia Gustavo DutraJQuery do dia-a-dia Gustavo Dutra
JQuery do dia-a-dia Gustavo DutraTchelinux
 
JavaScript for PHP developers
JavaScript for PHP developersJavaScript for PHP developers
JavaScript for PHP developersStoyan Stefanov
 
Soundreader.classpathSoundreader.project Soundre.docx
Soundreader.classpathSoundreader.project  Soundre.docxSoundreader.classpathSoundreader.project  Soundre.docx
Soundreader.classpathSoundreader.project Soundre.docxwhitneyleman54422
 
Appengine Java Night #2a
Appengine Java Night #2aAppengine Java Night #2a
Appengine Java Night #2aShinichi Ogawa
 
Persisting Data on SQLite using Room
Persisting Data on SQLite using RoomPersisting Data on SQLite using Room
Persisting Data on SQLite using RoomNelson Glauber Leal
 
Swift, functional programming, and the future of Objective-C
Swift, functional programming, and the future of Objective-CSwift, functional programming, and the future of Objective-C
Swift, functional programming, and the future of Objective-CAlexis Gallagher
 
Round PEG, Round Hole - Parsing Functionally
Round PEG, Round Hole - Parsing FunctionallyRound PEG, Round Hole - Parsing Functionally
Round PEG, Round Hole - Parsing FunctionallySean Cribbs
 
ESNext for humans - LvivJS 16 August 2014
ESNext for humans - LvivJS 16 August 2014ESNext for humans - LvivJS 16 August 2014
ESNext for humans - LvivJS 16 August 2014Jan Jongboom
 
PHP Language Trivia
PHP Language TriviaPHP Language Trivia
PHP Language TriviaNikita Popov
 
8時間耐久CakePHP2 勉強会
8時間耐久CakePHP2 勉強会8時間耐久CakePHP2 勉強会
8時間耐久CakePHP2 勉強会Yusuke Ando
 

Ähnlich wie Expression Language in JSP (20)

appengine java night #1
appengine java night #1appengine java night #1
appengine java night #1
 
Introducing CakeEntity
Introducing CakeEntityIntroducing CakeEntity
Introducing CakeEntity
 
Tutorial, Part 3: SharePoint 101: Jump-Starting the Developer by Rob Windsor ...
Tutorial, Part 3: SharePoint 101: Jump-Starting the Developer by Rob Windsor ...Tutorial, Part 3: SharePoint 101: Jump-Starting the Developer by Rob Windsor ...
Tutorial, Part 3: SharePoint 101: Jump-Starting the Developer by Rob Windsor ...
 
Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02
 
JavaScript and the AST
JavaScript and the ASTJavaScript and the AST
JavaScript and the AST
 
jQuery Data Manipulate API - A source code dissecting journey
jQuery Data Manipulate API - A source code dissecting journeyjQuery Data Manipulate API - A source code dissecting journey
jQuery Data Manipulate API - A source code dissecting journey
 
Propel sfugmd
Propel sfugmdPropel sfugmd
Propel sfugmd
 
Casting for not so strange actors
Casting for not so strange actorsCasting for not so strange actors
Casting for not so strange actors
 
jQuery - Introdução
jQuery - IntroduçãojQuery - Introdução
jQuery - Introdução
 
JQuery do dia-a-dia Gustavo Dutra
JQuery do dia-a-dia Gustavo DutraJQuery do dia-a-dia Gustavo Dutra
JQuery do dia-a-dia Gustavo Dutra
 
JavaScript for PHP developers
JavaScript for PHP developersJavaScript for PHP developers
JavaScript for PHP developers
 
Soundreader.classpathSoundreader.project Soundre.docx
Soundreader.classpathSoundreader.project  Soundre.docxSoundreader.classpathSoundreader.project  Soundre.docx
Soundreader.classpathSoundreader.project Soundre.docx
 
Appengine Java Night #2a
Appengine Java Night #2aAppengine Java Night #2a
Appengine Java Night #2a
 
Persisting Data on SQLite using Room
Persisting Data on SQLite using RoomPersisting Data on SQLite using Room
Persisting Data on SQLite using Room
 
Swift, functional programming, and the future of Objective-C
Swift, functional programming, and the future of Objective-CSwift, functional programming, and the future of Objective-C
Swift, functional programming, and the future of Objective-C
 
Round PEG, Round Hole - Parsing Functionally
Round PEG, Round Hole - Parsing FunctionallyRound PEG, Round Hole - Parsing Functionally
Round PEG, Round Hole - Parsing Functionally
 
ESNext for humans - LvivJS 16 August 2014
ESNext for humans - LvivJS 16 August 2014ESNext for humans - LvivJS 16 August 2014
ESNext for humans - LvivJS 16 August 2014
 
Jersey
JerseyJersey
Jersey
 
PHP Language Trivia
PHP Language TriviaPHP Language Trivia
PHP Language Trivia
 
8時間耐久CakePHP2 勉強会
8時間耐久CakePHP2 勉強会8時間耐久CakePHP2 勉強会
8時間耐久CakePHP2 勉強会
 

Mehr von corneliuskoo

12 High Level UI Event Handling
12 High Level UI Event Handling12 High Level UI Event Handling
12 High Level UI Event Handlingcorneliuskoo
 
07 Midlet On The Web
07 Midlet On The Web07 Midlet On The Web
07 Midlet On The Webcorneliuskoo
 
05 J2ME Wtk Command Line
05 J2ME Wtk Command Line05 J2ME Wtk Command Line
05 J2ME Wtk Command Linecorneliuskoo
 
02a cldc property support
02a cldc property support02a cldc property support
02a cldc property supportcorneliuskoo
 
01 java 2 micro edition
01 java 2 micro edition01 java 2 micro edition
01 java 2 micro editioncorneliuskoo
 

Mehr von corneliuskoo (9)

12 High Level UI Event Handling
12 High Level UI Event Handling12 High Level UI Event Handling
12 High Level UI Event Handling
 
08 Midlet Basic
08 Midlet Basic08 Midlet Basic
08 Midlet Basic
 
07 Midlet On The Web
07 Midlet On The Web07 Midlet On The Web
07 Midlet On The Web
 
06 Eclipse ME
06 Eclipse ME06 Eclipse ME
06 Eclipse ME
 
05 J2ME Wtk Command Line
05 J2ME Wtk Command Line05 J2ME Wtk Command Line
05 J2ME Wtk Command Line
 
03 midp
03 midp03 midp
03 midp
 
02a cldc property support
02a cldc property support02a cldc property support
02a cldc property support
 
02 cldc
02 cldc02 cldc
02 cldc
 
01 java 2 micro edition
01 java 2 micro edition01 java 2 micro edition
01 java 2 micro edition
 

Expression Language in JSP

  • 1. Expression Language Cornelius Koo, ST JavaSchool 2005 Jl. Cemara 2/20, Salatiga
  • 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. <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>
  • 5. Result • Spike Dog's name is : jsp.example.bean.Dog@1e0e954
  • 6. EL • Dog's name is : ${person.dog.name}
  • 9. 1st n 2nd • ${ firstThing.secondThing } • firstThing -> EL Implicit Object or attribute • secondThing -> a property
  • 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
  • 12. If the expression has a variable followed by a dot, the left-hand variable MUST be a Map or a bean
  • 13. The Thing on the right must follow normal Java naming rules for identifiers
  • 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. 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.
  • 16. • In Servlet String[] nameList = { “Aan”, “Sam”, “John”}; request.setAttribute(“name”, nameList);
  • 17. • In JSP Name : ${name} Name : ${name[0]} Name : ${name[“0”]}
  • 18. Using [] for bean
  • 19. • In Servlet Map musicMap = new HashMap(); musicMap.put(“Ambient”, “Zero”); request.setAttribute(“musicMap”,musicMap);
  • 20. • In JSP • ${musicMap.Ambient} • ${musicMap[“Ambient”]}
  • 21. What About This ? • ${musicMap[Ambient]} • This time with no “ “
  • 22. • In Servlet Map musicMap = new HashMap(); musicMap.put(“Ambient”, “Zero”); request.setAttribute(“musicMap”,musicMap); request.setAttribute(“Genre”,”Ambient”);
  • 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. Nesting • ${musicMap[ musicType[0] ]} • ${musicMap[“Ambient”]} • Zero 7
  • 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}
  • 27. • In HTML <form action=“TestBean.jsp”> Name : <input type=“text” name=“name”> ID# : <input type=“text” name=“empID”> <input type=“submit”> </form>
  • 28. • In JSP • ${param.name} • ${param.empID}
  • 29. Request Method • With expression <%= request.getMethod() %> • With EL ${pageContext.request.method}
  • 30. Getting Request Attribute • In Servlet request.setAttribute(“person”, p); • Get it with EL ${requestScope[“person”].name}
  • 32. • With expression <%= request.getHeader(“host”) %> • With EL ${header.host} ${header.[“host”]}
  • 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}
  • 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}
  • 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. 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. method.jsp <%@ taglib prefix="method" uri="DiceFunctions" %> <html> <head> <title>EL Method</title> </head> <body> ${method:rollIt()} </body> </html>
  • 42. Arithmetic • + • - • * • / • %
  • 43. Logical • && or and • || or or • ! or not
  • 44. Relational • == or eq • != or ne • < or lt • > or gt • <= or le • >= or ge