c:out Action Tag with EL Expressions

This section provides a tutorial example of using EL expressions in the c:out JSTL Core tag.

The <c:out> action tag is probably the best tag to show how EL (Expression Language) expression can be used in JSTL tags. Here is a JSP page example, ExpExampleTag.jspx:

<?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.3">
<!-- ExpExampleTag.jspx
 - Copyright (c) 2006 HerongYang.com. All Rights Reserved.
-->
<jsp:directive.page contentType="text/html"/>
<html><body>
<p>JSTL EL Expression Examples:</p>
<c:out value="1. ${1+1==2}"/><br/>
<c:out value="2. ${1+1}"/><br/>
<c:out value="3. ${1/3}"/><br/>
<c:out value="4. ${1.0/3.0}"/><br/>
<c:out value="5. ${quantity*price lt 100.0 and country=='USA'}"/><br/>
<c:set var="message" value="Hi there!"/>
<c:out value="6. ${message}"/><br/>
<c:out value="7. ${'message'}"/><br/>
<c:out value="10. ${pageContext.request.method}"/><br/>
<c:out value="11. ${pageContext.request[method]}"/><br/>
<c:out value="12. ${pageContext.request['method']}"/><br/>
<!-- c:out value="13. ${pageContext.request.'method'}"/><br/ -->
<!-- c:out value="14. ${pageContext.'request'.method}"/><br/ -->
<c:out value="15. ${pageContext['request']['method']}"/><br/>
<c:out value="16. ${pageContext['request'].method}"/><br/>
<!-- c:out value="20. ${pageContext.request.cookies.length}"/><br/ -->
<c:out value="21. ${pageContext.request.cookies[0].name}"/><br/>
<c:out value="22. ${pageContext.request.cookies[0].value}"/><br/>
<c:out value="23. ${pageContext.request.cookies[0]}"/><br/>
<!-- c:out value="24. ${pageContext.request.cookies.0}"/><br/ -->
<!-- c:out value="25. ${pageContext.request.cookies.0.name}"/><br/ -->
<c:out value="26. ${pageContext.request.cookies['0']}"/><br/>
<jsp:scriptlet>double pi = 3.14159;</jsp:scriptlet>
<c:out value="30. ${pi}"/><br/>
<jsp:scriptlet>
   double[] list = new double[3];
   list[0] = 9.99;
</jsp:scriptlet>
<c:out value="31. ${list[0]}"/><br/>
<!-- c:out value="32. ${pageContext.request.class.name}"/><br/ -->
<!-- c:out value="33. ${pageContext[request[class[name]]]}"/><br/ -->
<!-- c:out value="34. ${out.class.name}"/><br/ -->
<!-- c:out value="35. ${pageContext.out.class.name}"/><br/ -->
<c:out value="36. ${sessionScope.now}"/><br/>
<jsp:scriptlet>
   session.setAttribute("now", new java.util.Date());
</jsp:scriptlet>
<c:out value="37. ${sessionScope.now.time}"/><br/>
</body></html>
</jsp:root>

Note that:

If you visit the ExpExampleTag.jspx page for the first time, you will get:

JSTL EL Expression Examples:

1. true
2. 2
3. 0.3333333333333333
4. 0.3333333333333333
5. false
6. Hi there!
7. message
10. GET
11.
12. GET
15. GET
16. GET
21.
22.
23.
26.
30.
31.
36.
37. 1353078652164

If you visit the ExpExampleTag.jspx page again, you will get:

JSTL EL Expression Examples:

1. true
2. 2
3. 0.3333333333333333
4. 0.3333333333333333
5. false
6. Hi there!
7. message
10. GET
11.
12. GET
15. GET
16. GET
21. JSESSIONID
22. 669C0B7624702AF8209AF2B672ED9BFA
23. javax.servlet.http.Cookie@4c2b5e
26. javax.servlet.http.Cookie@4c2b5e
30.
31.
36. Sun Jul 1 10:10:52 EST 2012
37. 1152816698019

Note that:

Table of Contents

 About This Book

 JSP (JavaServer Pages) Overview

 Tomcat Installation on Windows Systems

 JSP Scripting Elements

 Java Servlet Introduction

 JSP Implicit Objects

 Syntax of JSP Pages and JSP Documents

 JSP Application Session

 Managing Cookies in JSP Pages

 JavaBean Objects and "useBean" Action Elements

 Managing HTTP Response Header Lines

 Non-ASCII Characters Support in JSP Pages

 Performance of JSP Pages

 EL (Expression Language)

 Overview of JSTL (JSP Standard Tag Libraries)

JSTL Core Library

 What Is JSTL Core Library

 c:out Action Tag

c:out Action Tag with EL Expressions

 c:set Action Tag

 c:if Action Tag

 c:choose Action Tag

 c:forEach Action Tag

 c:forTokens Action Tag

 Example - Dumping EL Implicit Objects

 Example - Calculating Prime Numbers

 JSP Custom Tags

 JSP Java Tag Interface

 Custom Tag Attributes

 Multiple Tags Working Together

 File Upload Test Application

 Using Tomcat on CentOS Systems

 Using Tomcat on macOS Systems

 Connecting to SQL Server from Servlet

 Developing Web Applications with Servlet

 Archived Tutorials

 References

 Full Version in PDF/EPUB