How to escape HTML in Java

In Java, we can use Apache commons-text, StringEscapeUtils.escapeHtml4(str) to escape HTML characters. pom.xml <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-text</artifactId> <version>1.8</version> </dependency> JavaEscapeHtmlExample.java package com.mkyong.html; // make sure import the correct commons-text package import org.apache.commons.text.StringEscapeUtils; // @deprecated as of 3.6, use commons-text StringEscapeUtils instead //import org.apache.commons.lang3.StringEscapeUtils; public class JavaEscapeHtmlExample { public static void main(String[] args) { String html = …

Read more

How to escape special characters in java?

In Java, we can use Apache commons-text to escape the special characters in HTML entities. Special characters as follow: < > ” & pom.xml <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-text</artifactId> <version>1.8</version> </dependency> EscapeSpecialChar.java package com.mkyong.html; import org.apache.commons.text.StringEscapeUtils; // @deprecated as of 3.6, use commons-text StringEscapeUtils instead //import org.apache.commons.lang3.StringEscapeUtils; public class EscapeSpecialChar { public static void main(String[] args) { …

Read more