What Is the Recommended Way to Escape HTML Symbols in Plain Java

How can I escape special HTML characters in JSP?

Short answer:

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

there is another option:

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

How to encode a string to UTF8 and then display it correctly in html?

Try StringEscapeUtils to escape your String for html. This did a good job for me till now.

How to unescape HTML character entities in Java?

I have used the Apache Commons StringEscapeUtils.unescapeHtml4() for this:

Unescapes a string containing entity
escapes to a string containing the
actual Unicode characters
corresponding to the escapes. Supports
HTML 4.0 entities.



Related Topics



Leave a reply



Submit