Share on Google+Share on Google+

How you can escape a String for HTML using the StringEscapeUtils

Advertisement
This tutorial demonstrate how to escape a String for HTML using the StringEscaptUtils

Description:

This tutorial demonstrate how you can escape a String for HTML using the StringEscapeUtils Here in this sample program it read the string variable and substitutes HTML entities using the StringEscapeUtils.escapeHtml() method. Its output will be seen at the console.

Configuration before running this example

Before running this program you need to set the class path. You can set the path of jar files in the batch file. Here it is written in pathset.bat file and the statement written inside is set classpath=.;lib/commons-io-1.4.jar;lib/commons-lang-2.4.jar

The directory structure of the files and lib folder containing commons-io-1.4.jar ,commons-lang-2.4.jar files shown below:

Code:

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringEscapeUtils;

class HtmlEscapeExample {
  public static void main(String[] argsthrows Exception {
    String str = "<sentence> Here is the few words sentence +
    & in the output, you will see how to escape a String for +
    HTML using StringEscapeUtils. </sentence>"
;
    String results = StringEscapeUtils.escapeHtml(str);
    System.out.println(results);
  }
}
 
 

Output:

Advertisements

Posted on: April 10, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.