The Solution for your problem is not encoding but to rendere Safe HTMl.. below is the solution
import org.owasp.validator.html.*; // Import AntiSamy
String POLICY_FILE_LOCATION = "antisamy-1.4.1.xml"; // Path to policy file
String dirtyInput = "<div><script>alert(1);</script></div>"; // Your HTML RESPONSE
Policy policy = Policy.getInstance(POLICY_FILE_LOCATION); // Create Policy object
AntiSamy as = new AntiSamy(); // Create AntiSamy object
CleanResults cr = as.scan(dirtyInput, policy, AntiSamy.SAX); // Scan dirtyInput
System.out.println(cr.getCleanHTML()); // Do something with your clean output!
Before you write this code ensure that you have following: antisamy.jar
.
This jar needs below dependent jars:
- xercesImpl.jar
- batik.jar
- nekohtml.jar
You will also need policy.xml
file.