Generating
XML Document Using JSP

JSP,
one of the important and powerful technologies for building web application
and can be explained as a combination of java (inside JSP tags) and HTML
tags in such a way which leads to the creation of dynamic HTML pages.

In addition
to the creation of dynamic HTML content, JSP can also be used for generating
dynamic XML document. This is done by sending XML tags in the form of
strings as a output of JSP page. You will understand better on how to
accomplish this with the help of following example:

As you know
the output of a JSP page is the whole content leaving JSP directives and
other constructs. This is the reason you have not used any output method
for passing the XML tags. It is worth to note here that JSP directives
are a type of JSP tags used to perform a specific task in a program and
begin with <@. Some commonly used directives are import include etc.

Looking
into the above example you can see that here its been used as the JSP
directive page and its attribute contentType.
Its value is equated to the string text/xml in order to guide
the browser (which is going to show the output of JSP) about the type
of content.

Generally
the default value of contentType is text/html but here as we are generating
an XML document so it is mandatory to set the contentType as text/xml.
Let us call the above jsp as example.jsp. Now when you run the example.jsp
on server ( or JSP container like Tomcat) we will get the following XML
as output: