Skip to content

Instantly share code, notes, and snippets.

@OsandaMalith
Created August 22, 2015 10:28
Show Gist options
  • Save OsandaMalith/1bb872aa3faad7ee650b to your computer and use it in GitHub Desktop.
Save OsandaMalith/1bb872aa3faad7ee650b to your computer and use it in GitHub Desktop.
HTML Encode function for Java
public class HTMLEncode {
public static void main(String[] args) {
System.out.println(HTMLEncode("Hi osanda \"><h1>XSS</h1>"));
}
public static String HTMLEncode(String s) {
StringBuilder out = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
out.append(c > 0x7f || c == '"' || c == '&' || c == '<' || c == '>' ? "&#" + (int) c + ";" : c);
}
return out.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment