Escape Characters

Because certain character sequences are used to represent start and stop tags, you sometimes need to escape a character so the container doesn’t interpret it as part of a special character sequence.

In a scripting element, if you need to use the characters %> literally, you must escape the greater-than character with a backslash:

<% String msg = "Literal %\> must be escaped"; %>

To avoid the character sequence <% in template text to be interpreted as the start of a scripting element, you must escape the percent sign:

This is template text and <\% is not a start of a scriptlet.

Similarly, the dollar sign that start an EL expression must be escaped in a page where EL evaluation is enabled:

This is template text and \${this is not an EL expression}.

In an attribute value, you must use the following escapes:

attr='a value with an escaped \' single quote'
attr="a value with an escaped \" double quote"
attr="a value with an escaped \\ backslash"
attr="a value with an escaped %\> scripting end tag"
attr="a value with an escaped <\% scripting start tag"
attr="a value with an escaped \$ dollar sign"

As an alternative to escaping quote characters, you can use the &apos; and &quot; character entities.

Get JavaServer Pages, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.