JSP Tutorials - Herong's Tutorial Examples - Version 4.03, by Dr. Herong Yang
Expression Examples in Static Text
This section provides a tutorial example with different Rvalue expressions embedded in static text: literal data, variables, implicit objects, user defined objects, collection elements, object properties, nested properties.
To check my understanding of the expression language, I wrote the following JSP to show some expression examples embedded in static text:
<?xml version="1.0"?> <jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:c="http://java.sun.com/jsp/jstl/core" version="2.1"> <!-- ExpExample.jspx - Copyright (c) 2012, HerongYang.com, All Rights Reserved. --> <jsp:directive.page contentType="text/html"/> <html><body><pre> EL Expression in Static Text Examples: 1. ${1+1==2} 2. ${1+1} 3. ${1/3} 4. ${1.0/3.0} 5. ${quantity*price lt 100.0 and country=='USA'} <c:set var="message" value="Hi there!"/> 6. ${message} 7. ${'message'} 10. ${pageContext.request.method} 11. ${pageContext.request[method]} 12. ${pageContext.request['method']} <!-- 13. ${pageContext.request.'method'} --> <!-- 14. ${pageContext.'request'.method} --> 15. ${pageContext['request']['method']} 16. ${pageContext['request'].method} <!-- 20. ${pageContext.request.cookies.length} --> 21. ${pageContext.request.cookies[0].name} 22. ${pageContext.request.cookies[0].value} 23. ${pageContext.request.cookies[0]} <!-- 24. ${pageContext.request.cookies.0} --> <!-- 25. ${pageContext.request.cookies.0.name} --> 26. ${pageContext.request.cookies['0']} <jsp:scriptlet>double pi = 3.14159;</jsp:scriptlet> 30. ${pi} <jsp:scriptlet> double[] list = new double[3]; list[0] = 9.99; </jsp:scriptlet> 31. ${list[0]} <!-- 32. ${pageContext.request.class.name} --> <!-- 33. ${pageContext[request[class[name]]]} --> 34. ${empty out} 35. ${empty pageContext.out} 36. ${empty sessionScope.now} <jsp:scriptlet> session.setAttribute("now", new java.util.Date()); </jsp:scriptlet> 37. ${sessionScope.now.time} </pre></body></html> </jsp:root>
The output of this example JSP is presented in the next section. But before reading the output, I want you take a guess on resulting values of my expression examples and compare them with the output.
Last update: 2012.
Table of Contents
JSP (JavaServer Pages) Overview
Tomcat 7 Installation on Windows Systems
Syntax of JSP Pages and JSP Documents
JavaBean Objects and "useBean" Action Elements
Managing HTTP Response Header Lines
Non-ASCII Characters Support in JSP Pages
What is EL (Expression Language)?
Literal Data and Named Variables
Basic Operators and Operations
Collection Elements and Object Properties
►Expression Examples in Static Text
Expression Examples in Static Text - Result
EL Variables Are pageContext Attributes
Overview of JSTL (JSP Standard Tag Libraries)