In this post, you will learn how to use the <c:out> tag in the JSTL core tags library with code examples.

The most commonly used JSTL tag which is used to display the result of the expressions. This tag is equivalent to JSP’s expression <%= %>.

 

JSTL <c:out> Syntax:

<c:out value="<string>" default="<string>" escapeXml="<true|false>"/>

 

Attributes:

Name

Required

Type

Description

value

True

java.lang.String

Information to show or an expression to evaluate.

default

False

java.lang.String

If the result of the expression is null, default value to show.

escapeXml

False

java.lang.String

Defaults to true. By default if resulting content has any XML or HTML or any other markup language tags, the content will be shown with raw XML or HTML or markup language (we can see the tags as well). If set to false, the XML or HTML will be evaluated and shown.

 

JSTL <c:out> Example:

The below example takes user input string value and outputs using the <c:out> tag:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
      "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>&lt;c:out&gt; Demo</title>
    </head>
    <body>
        <h1>&lt;c:out&gt; Demo</h1>
        <form name="outForm" 
             action="${pageContext.request.contextPath}/tag-types/core/out.jsp" 
             method="POST">
            Please enter some text (HTML allowed): <br/>
             <input type="text" name="someText"/>
             <input type="submit" value="submit"/> 
       </form>
      <br/>
      <br/>
      You just entered:<br/>
     <c:out value="${param.someText}" default="Nothing!" escapeXml="false"/>
    </body>
</html>
The below <c:out> tag displays the value of the variable content. If the value of the content is turned out to be null, the default text Empty will be displayed.

<c:out value="${content}" default=”Empty”/>

The below <c:out> tag displays the value of the variable content. Here we specified attribute escapeXml as true. Although true is the default for this attribute, this has been shown to make the point clear. For example: if the variable content has text <b>This is text</b>, the below code snippet will show the content as it is <b>This is text</b>.

<c:out value="${content}" escapeXml="true"/>

The below <c:out> tag displays the value of the variable content with escapeXml set to false. This means, the markup tags in the content will be evaluated and shown as part of the content. : if the variable content has text <b>This is text</b>, the below code snippet will show the content as This is text. Because the escapeXml has been set to false.

<c:out value="${content}" escapeXml="false"/>
 

Output:

out-demo

The above two sample runs demonstrates the use of <c:out> tag. The first example simply outputs the user entered text. The second example has HTML tags (heading 1 tag) as part of the user input. Since we specified attribute escapeXml to false the output is HTML enabled showing us the user entered text in the heading 1 format.



 

Recommended Usage of JSTL <c:out> tag:

This is the tag we use to display any information in the output. If we want the HTML which is part of the content to be evaluated then we can specify escapeXml as false. The attribute default is used to display any default information if the value specified is evaluated to null by any chance.

 

Other JSTL Core Tags:

if |  catch  |  choose   |  forEach  |  forTokens   |  import   |  param  |  redirect  |  remove   |  set  |  url


About the Author:

is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.



Add comment

   


Comments 

#3hello2014-09-09 06:48
Thank So much!
Quote
#2Nam2014-06-06 09:57
Hi Prateek Ashtikar,
Did you set the escapeXml attribute of the tag to false? You can freely use JSTL in your Spring MVC application, by setting a proper directive at the beginning of the JSP file, then use the appropriate tags.
Quote
#1Prateek Ashtikar2014-06-05 15:17
Hello,
Thanks for nice post. I would like to get some output in a different case.
In your 2nd case, you're entering text

Code java

, in this I would like to get output only as Code java (It should not be a bold case) i.e it should escape html tags.
Also I would like to perform this in jsp and Spring MVC. Could anyone please help me in this ASAP?
Quote