We can make use of the Apache Commons Text library to escape HTML characters using Java.
Let's take a look at an example using org.apache.commons.text.StringEscapeUtils.
import org.apache.commons.text.StringEscapeUtils;
public class JavaHTMLEscapeExample {
public static void main(String[] args) {
String htmlCode = "<html><head><title>Code2care Examples</title></head><body><h1>Welcome to Code2care</h1></body></html>";
String escapedHtml = StringEscapeUtils.escapeHtml4(htmlCode);
System.out.println(escapedHtml);
}
}
Output:
<html><head><title>Code2care
Examples</title></head><body><h1>Welcome to
Code2care</h1></body></html>
Note: Make sure to add the dependency to your build configuration file.
Add to Maven pom.xml
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
<version>1.10.0</version>
</dependency>
Add to Gradle build.gradle
implementation group: 'org.apache.commons', name: 'commons-text', version: '1.10.0'
-
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Java,
- Convert Java Map Collection Object to JSON String using Jackson
- Java Stream flatmap() Examples
- [Fix] Instant java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Years
- How to run Java Unit Test cases with Apache Maven?
- How to check if Java main thread is alive
- [Fix] java: incompatible types: incompatible parameter types in lambda expression error
- Parsing CSV file using Java code example (Comma Separated File)
- Unhandled exception type InterruptedException : Java Threads
- Native getClass() method from java.lang.Object Class Explained with examples.
- Java Jackson ObjectMapper Class with Examples
- Java 8 Streams map() with examples
- Java 8 - Convert List to Map Examples
- IntelliJ: Error: Could not find or load main class, java.lang.ClassNotFoundException
- Java Stream with Multiple Filters Example
- How to Clear StringJoiner in Java 8
- Spring 5 IoC Example with application Context XML (ClassPathXmlApplicationContext) and Gradle.
- How to get end of line (EOL) or new line character \r \n in Java
- Spring Boot CRUD Examples using JDBCTemplate
- Delete a File in Java with Examples
- Implementing Insertion Sort Algorithm in Java Program
- Java JDBC Batch Update Example with PreparedStatement
- Java JDBC Select Multiple Records from table as List using PreparedStatement
- [Hibernate] The method buildSessionFactory() from the type Configuration is deprecated
- How to fix Java HTTP java.net.UnknownHostException
- Java 8 Display time in 12 hour AM PM format
More Posts:
- MongoDB Hello World! Example - 2022
- How to change Chrome Spell Check from UK English to US English - Chrome
- How to Join Microsoft Teams Meeting Without an Account - Teams
- How to disable Microsoft teams from startup Windows 10 - Teams
- Java SE JDBC: Insert with PreparedStatement Example - Java
- How to Enable Macros on Excel for Mac - MacOS
- How to declare and initialize Array in Java Programming - Java
- Submit html form on dropdown menu value selection or change using javascript - JavaScript