Tuesday, January 25, 2011

Escaping special HTML characters in JSP

If you have a string that contains special characters such as '&' and you want to display this in a JSP page, you may get a ' The content of elements must consist of well-formed character data or markup.'

This code will fail if linkParm contains a '&'
<a href="main.do${userBean.linkParm}">
main link
</a>


The short answer is
<a href="main.do<c:out value='${userBean.linkParm}'/>">
main link
</a>


This seems overkill, indeed.

No comments:

Post a Comment