Outputting with c:out Action

In this tutorial, you will learn how to use action c:out to output variables and expressions on a web page.

<c:out> action is used to evaluate a variable or expression and output it. <c:out> is similar to the expression <%=  expression%>. The usage of <c:out> action is as follows:

<c:out value="variable" default="default value"/>
<c:out value="expression" default="default value" />Code language: HTML, XML (xml)

There are two attributes:

  1. Value:  You can put a variable or expression here
  2. default value: if the variable or expression is evaluated as null, the default value will be used for output.

The other form of the <c:out> action is as follows:

<c:out value="expression">
default value
</c:out>Code language: HTML, XML (xml)

If the expression is evaluated as null, the body between the opening and closing tag will be used for output.

Let’s take a look at an example of using <c:out> action:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core"
          prefix="c" %>
<html>
    <head>
        <title>c:out Action Demo</title>
    </head>
    <body>
        <c:out value="${x}" default="This is default value" />
        <c:set var="i" value="10" />
        <c:out value="${i}" />
        <c:out value="${i + 10}" />
    </body>
</html>Code language: HTML, XML (xml)

First we use <c:out> to output the variable x. Because variable x does not exist so the output is a string specified in the default value.

Second we use <c:set> action to declare a variable called i. You will learn more about the c:set action is the next tutorial. then we output the variable i, at this time we get the value of i displaying which is 10.

Last we use an expression which is (i + 10) so we get 20 displaying.

Here is the screenshot of the output:

JSTL c:out