Since most solutions reference a deprecated Apache class, here's one I've adapted from https://stackoverflow.com/a/16947646/3196753.
public class StringUtilities {
public static final String[] HTML_ENTITIES = {"&", "<", ">", "\"", "'", "/"};
public static final String[] HTML_REPLACED = {"&", "<", ">", """, "'", "/"};
public static String escapeHtmlEntities(String text) {
return StringUtils.replaceEach(text, HTML_ENTITIES, HTML_REPLACED);
}
}
Note: This is not a comprehensive solution (it's not context-aware -- may be too aggressive) but I needed a quick, effective solution.