
Sometime it’s absolutely necessary to escape HTML content before you proceed over to Database operations or sending it as a POST call payload.
Sometime back I’ve written a tutorial on Escape Character Utility for URL and JSON data – Kindly visit below tutorial for the same.
Do you have any of below questions?
- A method to escape HTML tags as HTML entities.
- Escapes and unescapes Strings for Java, Java Script, HTML and XML.
- How to escape HTML Special characters in JSP and Java?
- StringEscapeUtils.escapeHtml4() tutorial.
If yes, then you are at right place.
First thing first:
- Add pom.xml dependency:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
<version>1.9</version>
</dependency>
Let’s get started:
- Create class CrunchifyEscapeHTMLTutorial,java
- Copy and paste below code into it.
package crunchify.com.java.tutorials;
import org.apache.commons.text.StringEscapeUtils;
/**
* @author Crunchify.com
* Program: In Java How to Escape HTML? Simple Tutorial using Apache Commons Library.
*/
public class CrunchifyEscapeHTMLTutorial {
public static void main(String[] args) {
// StringEscapeUtils: Escapes and unescapes Strings for Java, Java Script, HTML and XML.
// #ThreadSafe#
// This code has been adapted from Apache Commons Lang 3.5.
String crunchifyHTML = "<!-- wp:paragraph -->\n" +
"<p>My name is <strong>App Shah</strong> and I’m a founder of <em><strong>Crunchify, LLC</strong></em>.</p>\n" +
"<!-- /wp:paragraph -->\n" +
"\n" +
"<!-- wp:paragraph -->\n" +
"<p>I started my blogging in <strong>2012</strong> as a hobby and now working as a <strong>full-time</strong> employee.<br>Our site is most popular among <code>bloggers</code> who are ready to learn the tricks of the trade to turning a blog into a successful <em>Tech Professionals</em> and <em>Niche Bloggers</em>.</p>\n" +
"<!-- /wp:paragraph -->";
// escapeHtml4: Escapes the characters in a String using HTML entities.
// For example:
// "bread" & "butter"
// becomes: &quot;bread&quot; &amp; &quot;butter&quot;.
// 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).
String crunchifyResult = StringEscapeUtils.escapeHtml4(crunchifyHTML);
crunchifyPrintUtils(crunchifyResult);
}
private static void crunchifyPrintUtils(String crunchifyResult) {
System.out.println(crunchifyResult);
}
}

IntelliJ IDEA console result:
Run above program as a Java Application and you will result as below. Here of the HTML tags are converted to it’s ASCII characters.
<!-- wp:paragraph --> <p>My name is <strong>App Shah</strong> and I’m a founder of <em><strong>Crunchify, LLC</strong></em>.</p> <!-- /wp:paragraph --> <!-- wp:paragraph --> <p>I started my blogging in <strong>2012</strong> as a hobby and now working as a <strong>full-time</strong> employee.<br>Our site is most popular among <code>bloggers</code> who are ready to learn the tricks of the trade to turning a blog into a successful <em>Tech Professionals</em> and <em>Niche Bloggers</em>.</p> <!-- /wp:paragraph --> Process finished with exit code 0
Let me know if you face any issue running above java program.
