Before I go and create a custom tag or Java method to do it, what is the standard way to escape HTML characters in JSP?
I have a String object and I want to display it in the HTML so that it appears to the user as is.
For example:
String a = "Hello < World";
Would become:
Hello < World
You must keep in mind that inside the <> is also html. In that case skipping > will break. If you're only escaping for between tags then you probably only need escape < and &.
Escape CharactersUse the backslash character to escape a single character or symbol. Only the character immediately following the backslash is escaped. Note: If you use braces to escape an individual character within a word, the character is escaped, but the word is broken into three tokens.
Short answer:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <c:out value="${myString}"/>
there is another option:
<%@taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %> ${fn:escapeXml(myString)}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With