Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

StringEscapeUtils escapeHtml

Is there a difference between org.apache.commons.lang.StringEscapeUtils.escapeHtml and org.apache.commons.lang3.StringEscapeUtils.escapeHtml4 ?

like image 905
mathematician Avatar asked Feb 28 '13 15:02

mathematician


People also ask

What is StringEscapeUtils escapeHtml?

StringEscapeUtils.escapeHtml("I'm coder") Escapes the characters in a String using HTML entities. For example: "bread" & "butter"

What does StringEscapeUtils escapeJava do?

escapeJava. Escapes the characters in a String using Java String rules. Deals correctly with quotes and control-chars (tab, backslash, cr, ff, etc.) So a tab becomes the characters '\\' and 't' .

How do you escape HTML tags in Java?

In Java, we can use Apache commons-text , StringEscapeUtils. escapeHtml4(str) to escape HTML characters. In the old days, we usually use the Apache commons-lang3 , StringEscapeUtils class to escape HTML, but this class is deprecated as of 3.6.


2 Answers

they come from different packages in fact

escapeHtml comes from org.commons.lang (lang 2.5 API) and let's you use a writer object. It can also escape string from an SQL source

escapeHtml4 is from org.commons.lang3 (lang 3.1 API) is specialy use to escape character from a HTML4 source. Nothing more, nothing less

They do the same job but i would recommend using "escapeHtml4" since its from a newer package

See : EscapeHtml4 and escapehtml

like image 188
P.Nichols Avatar answered Sep 20 '22 23:09

P.Nichols


Note that StringEscapeUtils.escapeHtml() has issues with apostrophes

like image 29
fmpdmb Avatar answered Sep 22 '22 23:09

fmpdmb