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:
1 2 | <c:out value="variable" default="default value"/> <c:out value="expression" default="default value" /> |
There are two attributes:
- Value: You can put a variable or expression here
- 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:
1 2 3 | <c:out value="expression"> default value </c:out> |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <%@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> |
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: