Expression Language in JSP
Upcoming SlideShare
Loading in...5

Expression Language in JSP






Total Views
Views on SlideShare
Embed Views



0 Embeds 0

No embeds



Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment

Expression Language in JSP Expression Language in JSP Presentation Transcript

  • 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"/> Dogs name is : <jsp:getProperty name="person" property="dog"/></body>
  • Result• Spike Dogs name is : jsp.example.bean.Dog@1e0e954
  • EL• Dogs name is : ${}
  • Result• Dogs 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-handvariable MUST be a Map or a bean
  • The Thing on the right must follow normal Java naming rules foridentifiers
  • 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), itcan be a Map key or a bean property, or an index into a List or array.
  • • In ServletString[] nameList = { “Aan”, “Sam”, “John”};request.setAttribute(“name”, nameList);
  • • In JSPName : ${name}Name : ${name[0]}Name : ${name[“0”]}
  • Using [] for bean
  • • In ServletMap 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 ServletMap 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.empID}
  • Request Method• With expression<%= request.getMethod() %>• With EL${pageContext.request.method}
  • Getting Request Attribute• In Servletrequest.setAttribute(“person”, p);• Get it with EL${requestScope[“person”].name}
  • Header Information
  • • With expression<%= request.getHeader(“host”) %>• With EL${}${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${}
  • 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" ""><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