BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

JSTL <c:out> Core Tag

Last Updated: November 28, 2013 by Chaitanya Singh | Filed Under: JSTL

<c:out> is a JSTL core tag, which is used for displaying server-side variables and hardcoded values on the browser (client). You may be wondering that a variable’s value and data can be displayed using Expression language(EL)  and out implicit object too then why do we need <c:out> jstl tag? the difference is that the <c:out> tag escapes HTML/XML tags but others don’t, refer the example to understand this.

Tag <c:out> Example

In this example we are displaying a string on the browser, however we are using html tags in the value and we want to see what would be the result and how it is gonna HTML tags.

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>c:out Tag Example</title>
</head>
<body>
<c:out value="${'<b>This is a <c:out> example </b>'}"/>
</body>
</html>

Output:
<b>This is a <c:out> example </b>

c-out-example

escapeXml attribute of <c:out> tag

Let’s say I modify the above code like this – I have just added escapeXML attribute in the tag and marked it false. By default the value of escapeXML attribute is true. Since we have marked it as false it would not escape HTML/XML tags and the tags will work.

<c:out value="${'<b>This is a <c:out> example </b>'}" escapeXml="false"/>

Output:

c-out-example2

Attribute “default” of <c:out> tag

Above we have seen escapeXML attribute of the <c:out> tag. There is another attribute “default” for this tag, which is used to display the fallback or default value in case the value of the <c:out> tag is null. Here is the example where we are trying to print the value of string str using the tag and since the string str value is null, the tag is printing the value set in default attribute.

<%! String str = null; %>
<c:out value="${str}" default="default value of c:out"/>

c-out-example3

Top Related Articles:

  1. fn:endsWith() – JSTL Function
  2. JSTL <c:set> Core Tag
  3. JSTL <c:remove> Core Tag
  4. JSTL <c:redirect> Core Tag
  5. JSTL <c:choose>, <c:when>, <c:otherwise> Core Tags

Tags:

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

JSTL Core Tags

  • c:out
  • c:set
  • c:remove
  • c:if
  • c:choose
  • c:when
  • c:otherwise
  • c:catch
  • c:import
  • c:forEach
  • c:forTokens
  • c:param
  • c:url
  • c:redirect
JSTL Functions
  • fn:contains()
  • fn:containsIgnoreCase()
  • fn:indexOf()
  • fn:escapeXML()
  • fn:join() & fn:split()
  • fn:length()
  • fn:startsWith()
  • fn:endsWith()
  • fn:substring()
  • fn:substringAfter()
  • fn:substringBefore()
  • fn:trim()
  • fn:toUpperCase()
  • fn:toLowerCase()
  • fn:replace()

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap