views:

207

answers:

4

Hi,

I have code which generates some HTML, but when I try to output this content in a jsp all '<' are replaced with '&lt;' and all '>' with '&gt;'. Here is the piece which renders the result:

<c:out value="${data}"/>

Can someone please explain what causes the character replacement, and how it can be avoided?

PS: I've tried escapeXml='false' property of <c:out/> tag, nothing at all is displayed.

Thanks, Natasha

+1  A: 

What do you want the generated HTML to do, do you want it to be markup used by the browser or do you want it shown on the final page, for example as sample code visible to the user? Without escapeXml='false' it will be output to the browser as HTML and interpreted along with all the other markup. With escapeXml='true' it will be turned into escaped markup which is rendered visible to the end-user. So it all depends on what you're trying to do.

If you want to have < and > characters visible to the end user then they have to appear as &lt; and &gt; in the markup.

Nick

Nicholas Smith
I need this content to be output to the browser as HTML. I should have probably mentioned that I am doing this inside a jquery dialog window. I wonder if this makes any difference?
A: 

there is an escapeXml attribute on the c:out tag that is set to true by default, set it to false and no escaping will take place, i.e. your HTML will be output as is in the browser.

MahdeTo
+1  A: 

The out tag behaves as it should - if you do wish to output the literal value you can directly insert the EL expressen ${data} in your text. That is, in stead of

Some content <c:out value="${data}"/> some more content

you would use

Some content ${data} some more content

in your JSP.

Before you get angry with c:out, please consider that the output often is something a user has put in there - potentially with some unwanted code. Imagine using ${data} on StackOverflow in stead of the (C# version of) c:out :-)

extraneon
A: 

<c:out value="${data}"/> escapes HTML characters while ${data} does not. Take a look at this related question.

Steve Kuo