I would appreciate providing me with a set of clear guidelines or ruling for handling escaping strings. What I use for escaping strings is the apache commons-lang-x.x.jar library. Specifically the StringEscapeUtils.escapeHtml(String toEscape)
method.
I need to know:
(1) Where is it better to escape strings, on the JSP page or in the Servlet?
(2) What do you recommend StringEscapeUtils.escapeHtml(..) or <c:out> from JSTL
(3) Handling multiline strings, which is better, use <br> directly in the string, or \n and a nl2br() method:
String strError = "Invalid username.\nPlease try again.";
or
String strError = "Invalid username.<br>Please try again.";
(4) How would I go escaping strings that receive wild cards, example:
String strError = "Invalid user [%s].<br>Please specify another user."
(5) Since javascript escape characters are different. What should I use to escape Java strings that are to be rendered inside the javascript sections of the JSP page (eg. var name = "<%=javaStringHoldingName%>"
).
Should contain alphanumeric only.". And lets say the value is "1..<5". How would this be handled in escaping? Please note that the template string contains a
because it is intended to be displayed on two lines. – Basil Musa Feb 10 '11 at 22:06