In my legacy project i can see the usage of escapeHtml before string is sent to browser.
StringEscapeUtils.escapeHtml(stringBody);
I know from
I can think of several possibilities to explain why sometimes a string is not escaped:
EDIT -
The reason for escaping is that special characters like There are several cases that will fail despite the best efforts of the browser to recover from bad markup. If your sample string were an attribute value, escaping the quote marks would be absolutely required. There's no way that a browser is going to correctly handle something like: The general rule is that any character that is not markup but might be confused as markup need to be escaped. Note that there are several contexts in which text can appear within an html document, and they have separate requirements for escaping. The following should be escaped: Finally, aside from the hazard of double-escaping, the cost of escaping all text is minimal: a tiny bit of extra processing and a few extra bytes on the network.&
and <
can end up causing the browser to display something other than what you intended. A bare &
is technically an error in the html. Most browsers try to deal intelligently with such errors and will display them correctly in most cases. (This will almost certainly happen in your example text if the string were text in a
'
or "
, whichever one matches the delimiters used for the attribute value itself) and the ampersand (&
), but not <
&
and <