XML and JSP

If you’re developing web sites for a living, you’ve surely encountered the Extensible Markup Language (XML). XML is a set of syntax rules for how to represent structured data using markup elements represented by a start tag (optionally with attributes), a body, and an end tag:

<employee id="123">
  <first-name>Hans</first-name>
  <last-name>Bergsten</last-name>
  <telephone>310-555-1212</telephone>
</employee>

This XML example contains four elements: <employee>, <first-name>, <last-name>, and <telephone>.

By selecting sensible element names, an XML file may be understandable to a human, but to make sense to a program it must use only a restricted set of elements in which each element has a well-defined meaning. This is known as an XML application (the XML syntax applied to a certain application domain). A couple of examples are the Wireless Markup Language (WML), used for browsers in cellular phones and other small devices, and XHTML, which is HTML 4.0 reformulated as an XML application. Other examples are JSP action elements and the Web Application Deployment Descriptor elements introduced in Chapter 2.

Generating an XML Document

As we discussed in Chapter 3 and Chapter 5, everything in a JSP page that is not a JSP element is template text. In all the examples so far, we have used HTML as the template text. But we can use any text, such as XML elements. Example 12.6 shows a JSP page that sends a simple phone book to a wireless device, using the XML elements defined by the ...

Get Java Server Pages 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.