tags:

views:

626

answers:

5

Hey everyone,

I have a JSP page which accepts SQL queries, performs them then returns the results in a table. A few of the results occasionally have HTML tags in them i.e. - a result will return:

This is the returned result! I have <br> and <hr> tags!

When this gets put through the code to handle the return and place it into a table it will actually "render" the <br> and <hr> tags as HTML, but I want it to simply show the actual <br> and <hr> tags.

Currently the return is printed using <%=colvalue %>

How can I do this?

A: 

I have &lt;br&gt; and &lt;hr&gt; tags!

DigitalRoss
A: 

You can use the JSTL tag. It has an escapeXML parameter that will do what you want to do.

Will Hartung
+1  A: 

If you're using JSTL, the <c:out> tag escapes what you pass to it by default.

E.g.

<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<c:out value="${value}"/>

You can also use the escapeXml el function from the functions taglib

 <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"  %>
 ${fn:escapeXml(someVarWithHtmlInIt)}
jimr
A: 

In PHP you would use htmlentities and htmlspecialchars

jjclarkson
A: 

Check out this page for a simple method which will escape a string you pass into it: http://www.rgagnon.com/javadetails/java-0306.html

grahamrb