Skip to Content
JavaServer Pages, 3rd Edition
book

JavaServer Pages, 3rd Edition

by Hans Bergsten
December 2003
Intermediate to advanced
764 pages
24h 58m
English
O'Reilly Media, Inc.
Content preview from JavaServer Pages, 3rd Edition

Encoding Non-XML Data and Special Characters

Since a JSP Document is a formal XML document, all template data must be well- formed XML. Example 17-10 is well-formed because it contains XHTML elements, such as the <ul> and <li> elements, represented by opening and closing tags, and an empty <br/> tag.

If you use a JSP Document to generate a response in a format that is not XML-based, you can embed the template data that is not well-formed in XML CDATA sections. For instance, if the response must be accessible to older browsers that only understand HTML 3.2, you can’t use the XHTML <br/> empty tag syntax, but using HTML 3.2 syntax with a single opening <br> tag violates the well-formedness requirement. Embedding the HTML 3.2 single opening <br> tag in a CDATA section solves the problem; it satisfies the container’s well-formed XML requirement and feeds the browser the HTML 3.2 tag it understands:

<ul> 
  <c:forEach items="${paramValues}" var="current"> 
    <li> 
      ${current.key}:
      <c:forEach items="${current.value}" var="parValue">
        <![CDATA[<br>]]>${parValue}
      </c:forEach>
    </li> 
  </c:forEach>

In an XML document, special characters such as greater-than and less-than in an element body (or attribute) must be replaced with character entity codes, for example, the < character must be replaced with &lt; and > must be replaced with &gt;. These characters are common in Java code, so if you need to use scripting elements in a JSP Document, you can use a CDATA section to work around this issue as well:

                  
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

More Servlets and JavaServer Pages™

More Servlets and JavaServer Pages™

Marty Hall

Publisher Resources

ISBN: 0596005636Errata Page