XML Declaration Generation

When the container processes a JSP Document, it adds an XML declaration like this to the response in most cases:

<? xml version="1.0" encoding="encodingValue" ?>

The encoding attribute value is the response character encoding defined by a page directive with a contentType attribute containing a character encoding, as described in Chapter 14, or UTF-8 if it’s not defined in the JSP Document.

The XML declaration is generated by default for a JSP Document, unless <jsp:root> is used as the root element. You can use the <jsp:output> standard action to suppress the generation of the declaration, or to force it to be generated for a <jsp:root> document:

<jsp:output omit-xml-declaration="true" />

The omit-xml-declaration attribute accepts one of four values: with the value true or yes, the declaration is not generated; if set to false or no, it is. A reason to omit the declaration is when the generated response is included into another document, for instance using the <jsp:include> action in another JSP Document.

For tag files written in XML syntax, no XML declaration is generated by default, since their output typically gets mixed with the content of the page that invokes them. In the rare event you need one, you can use the <jsp:output> action in the tag file with omit-xml-declaration set to false or no.

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.