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

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"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>

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

  • <jsp:root>
  • <jsp:directive.page>
  • <jsp:declaration>
  • <jsp:scriptlet>
  • <jsp:expression>

<jsp:root> 

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

XML Syntax for <jsp:root> tag

<jsp:root
 xmlns:jsp="http://java.sun.com/JSP/Page"
 [ xmlns:taglibPrefix="URI" ]+ version="1.2 | 2.0">
  JSP Page
</jsp:root>

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

<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page"   version="1.2">
...................
JSP page content
.......................
..................

</jsp:root>

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

<jsp:directive.page> 

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 <jsp:directive.page>(Page directive)

<jsp:directive.page 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-XHTML.jsp

<?xml version="1.0"?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="1.2">
<jsp:directive.page contentType="text/html"/>
&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
&gt;
  <html>
  <head>
  <title>JSP in pure XML- XHTML</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
   </head>
<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">
<tr>
  <td>First Name</td><td><jsp:expression> fname</jsp:expression></td>
</tr>
<tr>
  <td>Mid Name</td><td><jsp:expression> midname</jsp:expression></td>
</tr>
<tr>
  <td>Last Name</td><td><jsp:expression> lname</jsp:expression></td>
</tr>
<tr>
   <td>Age</td><td><jsp:expression> age</jsp:expression></td>
</tr>
  </table>
   </font></b>
   </body>
</html>
</jsp:root>

Output:

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

Download Source Code