Reputation: 15052
suppose I have a String, say:
<abc>
<xyz>
How do I print this in JSP? I have tried it,but anything between <> is supposed to be a tag and hence is not printed. Plus,to print it in two new lines also.
To make it more specific:
It's a String that I'm fetching from a previous page and then printing the String using out.print();
.
Upvotes: 0
Views: 2122
Reputation: 26132
JSTL: <c:out escapeXml="true" value="${yourVariable}">
JSP: Use StringEscapeUtils: <%=StringEscapeUtils.escapeHtml(yourVariable)%=>
HTML: Use <pre>
: <pre><%=yourVariable%></pre>
Upvotes: 3
Reputation: 1108722
You need to escape predefinied HTML/XML entities when you want to display them as-is in the HTML/XML output. The <
and >
are one of them. You need to escape them by <
and >
respectively.
So,
<abc>
<xyz>
should do.
Or if it is been obtained as a bean property, use JSTL <c:out>
or fn:escapeXml()
.
<c:out value="${bean.property}" />
or
${fn:escapeXml(bean.property)}
It will automatically escape them then.
Upvotes: 2