JSP Document - JSP Page in XML Format

This section describes the syntax of a JSP Document, which is a JSP page written in XML format. Tomcat 7 server can process both JSP Page format and JSP Document format.

According to the JSP 2.1 specification, a JSP page can be written in 2 formats:

In previous sections we learned how to write "JSP Pages", now let's see rules on how to write "JSP Documents":

Here is my "Hello world!" example in JSP Document XML format:

<?xml version="1.0"?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1">
<!-- hello_xml.jspx
 - Copyright (c) 2012, HerongYang.com, All Rights Reserved.
-->
<html><body>
<jsp:scriptlet>out.println("Hello world!");</jsp:scriptlet>
</body></html>
</jsp:root>

Save the above document on the Tomcat 7 server at: C:\local\apache-tomcat-7.0.32\webapps\ROOT\hello_xml.jspx.

Then open this JSP Document with a Web browser with this URL: http://localhost:8080/hello_xml.jspx. The follow text shows up in the browser:

- <html>
  <body>Hello world!</body> 
  </html>

What happens here is that the Tomcat server is process this JSP document as an XML file and returns the response with content-type set to text/xml. To fix the problem, we need to use a directive element to set the context-type back to text/html. Here is the modified JSP page, hello_xml_html.jspx:

<?xml version="1.0"?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1">
<!-- hello_xml_html.jspx
 - Copyright (c) 2012, HerongYang.com, All Rights Reserved.
-->
<jsp:directive.page contentType="text/html"/>
<html><body>
<jsp:scriptlet>out.println("Hello world!");</jsp:scriptlet>
</body></html>
</jsp:root>

Save this new JSP document on the Tomcat 7 server and view it again in a browser, you will see the correct output of "Hello world!".

Last update: 2012.

Table of Contents

 About This Book

 JSP (JavaServer Pages) Overview

 Tomcat 7 Installation on Windows Systems

 JSP Scripting Elements

 Java Servlet Introduction

 JSP Implicit Objects

Syntax of JSP Pages and JSP Documents

 Syntactic Elements of a JSP Page

JSP Document - JSP Page in XML Format

 Writing Scriptlet Element in XML Format

 Writing Directive Element in XML Format

 Writing Action Element in XML Format

 "include" Directive and Action Elements

 Execution Result of CurrentTime.jspx

 JSP Application Session

 Managing Cookies in JSP Pages

 JavaBean Objects and "useBean" Action Elements

 Managing HTTP Response Header Lines

 Non-ASCII Characters Support in JSP Pages

 Performance of JSP Pages

 EL (Expression Language)

 Overview of JSTL (JSP Standard Tag Libraries)

 JSTL Core Library

 JSP Custom Tags

 JSP Java Tag Interface

 Custom Tag Attributes

 Multiple Tags Working Together

 File Upload Test Application

 Outdated Tutorials

 References

 PDF Printing Version