ChatGPT解决这个技术问题 Extra ChatGPT

JSP : JSTL's <c:out> tag

Writing a JSP page, what exactly does the <c:out> do? I've noticed that the following both has the same result:

<p>The person's name is <c:out value="${person.name}" /></p>
<p>The person's name is ${person.name}</p>

M
Mdhar9e

c:out escapes HTML characters so that you can avoid cross-site scripting.

if person.name = <script>alert("Yo")</script>

the script will be executed in the second case, but not when using c:out


Only if 'escapeXML' it set to true (not sure if it is by default)
I believe it is true by default.
N.B. it escapes XML not HTML. One of the more annoying subtleties of JSTL. I end up always writing my own HTML escape EL fn.
The attribute name is case sensitive so it's escapeXml="true" not escapeXML
I have no idea what this answer's code sample is showing- can someone clarify? It mentions a "second case" but I don't see that and I don't see c:out being used in the code.
j
jpaugh

As said Will Wagner, in old version of jsp you should always use c:out to output dynamic text.

Moreover, using this syntax:

<c:out value="${person.name}">No name</c:out>

you can display the text "No name" when name is null.


Cool! I did not know that.
Agreed, cool. Thanks for teaching and helping. I didn't know that either. Cheers!
or
JSR 52, maintenance release 2, see page 22 "with a body". Link: download.oracle.com/otndocs/jcp/jstl-1.2-mrel2-eval-oth-JSpec
@Barett. Cool. I wonder why this never makes any of the tutorials or examples. A more convenient syntax than the default attribute IMO.
C
Chris Serra

c:out also has an attribute for assigning a default value if the value of person.name happens to be null.

Source: out (TLDDoc Generated Documentation)


G
Greenhorn

You can explicitly enable escaping of Xml entities by using an attribute escapeXml value equals to true. FYI, it's by default "true".


Some example code would really help make this answer complete.
W
Will Wagner

Older versions of JSP did not support the second syntax.