O'Reilly logo

Java Servlet & JSP Cookbook by Bruce W. Perry

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

5.6. Generating an XML View from a JSP

Problem

You want to automatically generate an XML view from a JSP page.

Solution

Create a custom tag and a TagLibraryValidator class, from which you can output the XML view of a JSP to a file.

Discussion

An XML view is an XML form of a JSP page that the JSP container generates during the translation phase, an intermediary stage before the container converts the JSP to its page implementation class (a servlet). A TagLibraryValidator class can use the XML view to validate the use of custom tags in the JSP prior to the JSP's conversion to a servlet. An XML view is very similar to a JSP document, which is an XML form of a JSP page that JSP developers can write and add to their web applications. The differences between the two XML files according to the JSP specification v2.0 are:

  • An XML view expands any include directives that a JSP document contains into their corresponding JSP fragments.

  • An XML view provides each XML element with a jsp:id attribute.

  • An XML view adds a jsp:root element as the root element of the document if the document does not already have a jsp:root element.

  • An XML view adds a jsp:directive.page element and pageEncoding attribute if they do not already exist, and sets the value of pageEncoding to "UTF-8".

  • An XML view adds a jsp:directive.page element and contentType attribute if they do not already exist, and sets the value of contentType according to Chapter JSP.4.2, "Response Character Encoding," of the JSP 2.0 specification ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required