StringEscapeUtils.escapeHtml

Chen Li Yong picture Chen Li Yong · Oct 2, 2014 · Viewed 15.1k times · Source

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.

Answer

Srikanth Puliroju picture Srikanth Puliroju · Dec 11, 2015

Use:

StringEscapeUtils.escapeHtml("I'm coder")

Escapes the characters in a String using HTML entities.

For example:

"bread" & "butter"

Becomes:

&quot;bread&quot; &amp; &quot;butter&quot;.