Search code examples
jsphtml-escape-characters

StringEscapeUtils.escapeHtml


I'm working in JSP, and I'm still fairly new at this. I need to change single aposthropy into ' or ', and double aposthropy into " or " using StringEscapeUtils.escapeHtml. It compiled successfully. But it didn't work.

Here's my exact testing code.

<%= StringEscapeUtils.escapeHtml("cool 4' product") %>

In view page source, the result is:

cool 4' product

not

cool 4&#39; product.

What's wrong? And how can I fixed it? Thanks.


Solution

  • to quote the StringEscapeUtils.escapeHtml javadocs, as in https://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/StringEscapeUtils.html#escapeHtml%28java.lang.String%29

    Supports all known HTML 4.0 entities, including funky accents. Note that the commonly used apostrophe escape character (') is not a legal entity and so is not supported).

    So you have to do that manually.