1

Possible Duplicate:
Recommended method for escaping HTML in Java
Replace characters with HTML entities in java

I have some java code which writes text including angle-brackets to a DB; this text is displayed on a web-page.

Rather than change my java code to be full of entity codes, I wondered if I can keep the constants like "<test>" and use a standard library function to generate the entity-based string "&lt;test&gt;"?

Community
  • 1
  • 1
Mr. Boy
  • 60,845
  • 93
  • 320
  • 589
  • 2
    Related: http://stackoverflow.com/questions/10978098/replace-characters-with-html-entities-in-java – gd1 Jan 07 '13 at 20:21

1 Answers1

5

If you just want to do angle brackets, I'd have a look at Java's String Replace functionality.

But you probably should take a look at StringEscapeUtils if you want a more complete solution

in particular, public static String escapeHtml(String str) looks like a promising function


Judging by that signature, I would guess that

string myEscapedHTML = escapeHtml(myUnescapedHTML);

is the way to use it.