0

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(); .

Kazekage Gaara
  • 14,972
  • 14
  • 61
  • 108

4 Answers4

3

JSTL: <c:out escapeXml="true" value="${yourVariable}">

JSP: Use StringEscapeUtils: <%=StringEscapeUtils.escapeHtml(yourVariable)%=>

HTML: Use <pre>: <pre><%=yourVariable%></pre>

bezmax
  • 25,562
  • 10
  • 53
  • 84
2

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 &lt; and &gt; respectively.

So,

&lt;abc&gt;
&lt;xyz&gt;

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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
0

try:

&lt;abc&gt;<br/>
&lt;xyz&gt;
James.Xu
  • 8,249
  • 5
  • 25
  • 36
0

Did you try:

out.print("&lt;abx&gt;");
out.print("&lt;xyz&gt;");
Swift
  • 13,118
  • 5
  • 56
  • 80