Use:
StringEscapeUtils.escapeHtml("I'm coder")
Escapes the characters in a String using HTML entities.
For example:
"bread" & "butter"
Becomes:
Answer from Srikanth Puliroju on Stack Overflow
"bread" & "butter".
Use:
StringEscapeUtils.escapeHtml("I'm coder")
Escapes the characters in a String using HTML entities.
For example:
"bread" & "butter"
Becomes:
"bread" & "butter".
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.
StringEscapeUtils from Apache Commons Lang:
import static org.apache.commons.lang.StringEscapeUtils.escapeHtml;
// ...
String source = "The less than sign (<) and ampersand (&) must be escaped before using them in HTML";
String escaped = escapeHtml(source);
For version 3:
import static org.apache.commons.lang3.StringEscapeUtils.escapeHtml4;
// ...
String escaped = escapeHtml4(source);
An alternative to Apache Commons: Use Spring's HtmlUtils.htmlEscape(String input) method.