Saving and Emitting HTML Fragments Using JSTL in a JSP Page

When saving text with the special characters <>'"&, the special characters are save as is; there is no translation that takes place. However, when emitting text with <c:out>, these special characters are translated to the equivalent XML entities. In particular, < becomes &lt;, > becomes &gt;, ' becomes &#039;, " becomes &#034;, and & becomes &amp;. This means that a value such as <b> will be emitted as &lt;b&gt; and therefore not be interpreted as the HTML b tag.

To prevent this translation, the escapeXml attribute must be specified and set to false:

<%-- Declare the core library --%> <%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c" %> <%-- Save data with html tags --%> <c:set var="msg" value="hi <b>John</b>!" scope="page" /> <%-- Show the value after translating special characters --%> <c:out value='${msg}' /> <%-- Show the value without translating special characters --%> <c:out value='${msg}' escapeXml="false" />

Comments

5 May 2010 - 1:25am by Bharat Kumar Kona (not verified)

This info is very very useful. I could resolve the issue in seconds. Thanks a lot!!!

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.