Kazekage Gaara
Kazekage Gaara

Reputation: 15052

print such kind of String in jsp

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

Answers (4)

bezmax
bezmax

Reputation: 26132

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

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

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

Upvotes: 3

BalusC
BalusC

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 &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.

Upvotes: 2

Swift
Swift

Reputation: 13188

Did you try:

out.print("&lt;abx&gt;");
out.print("&lt;xyz&gt;");

Upvotes: 0

James.Xu
James.Xu

Reputation: 8295

try:

&lt;abc&gt;<br/>
&lt;xyz&gt;

Upvotes: 0

Related Questions