JSTL Core Tag c:out Example
- Details
- Written by Nam Ha Minh
- Last Updated on 31 August 2019   |   Print Email
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><c:out> Demo</title> </head> <body> <h1><c:out> 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:
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
Comments
Did you set the escapeXml attribute of the
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?