Share on Google+Share on Google+

Using JSP in pure XML generating conforming XHTML

This JSP example describes how JSP tags for XML can be used in XML generation conforming XHTML.

Using JSP in pure XML  generating conforming XHTML


Example program to demonstrate using JSP in pure XML generating conforming XHTML

This JSP example describes how JSP tags for XML can be used in XML generation conforming  XHTML. XHTML conformation is done by including the following code:





<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

Some of the important tags used in JSP files in their XML format is given as below:

  • <jsp:root>
  • <>
  • <jsp:declaration>
  • <jsp:scriptlet>
  • <jsp:expression>


<jsp:root> tag provides standard JSP elements and namespace attributes of tag libraries.

XML Syntax for <jsp:root> tag

 [ xmlns:taglibPrefix="URI" ]+ version="1.2 | 2.0">
  JSP Page

This <jsp:root> can be used in JSP page as:

<jsp:root xmlns:jsp=""   version="1.2">
JSP page content


JSP page in pure XML syntax have <jsp:root> as its root element, but it is not required. "version" attributes is mandatory.


It defines attributes to be applied for whole JSP page and to only static included files but not for dynamic file inclusion.

XML Syntax for <>(Page directive)

< listofPageAttributes />

In this example we are using two more tags <jsp:scriptlet> and <jsp:expression> to show content on a JSP page. Since in XHTML format JSP's sciptlet(<% code %>), expression(<%= expression %>), declaration(<%! declarationCode %>) do not work so their XML form is required.

Full code for XML-XHTML.JSP page is as follows:


<?xml version="1.0"?>
<jsp:root xmlns:jsp="" version="1.2">
< contentType="text/html"/>
&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  <title>JSP in pure XML- XHTML</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<body bgcolor="#ffffcc">
  <h1>XML-XHTML example</h1>
  <b><font color="red" size="5">
   <jsp:declaration> String fname="amit";</jsp:declaration>
   <jsp:declaration> String midname="kumar";</jsp:declaration>
   <jsp:declaration> String lname="raghuwanshi";</jsp:declaration>
   <jsp:declaration> int age=21;</jsp:declaration>
   <table border="1">
  <td>First Name</td><td><jsp:expression> fname</jsp:expression></td>
  <td>Mid Name</td><td><jsp:expression> midname</jsp:expression></td>
  <td>Last Name</td><td><jsp:expression> lname</jsp:expression></td>
   <td>Age</td><td><jsp:expression> age</jsp:expression></td>


When we run tomcat server and execute this JSP page then output will be like as follows:

Download Source Code



Posted on: July 5, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: Using JSP in pure XML generating conforming XHTML  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image