Nearby lessons

10 of 30

JSP - Documents (XML Syntax)

📌 What You Will Learn
  • Convert standard JSP syntax to XML-based syntax
  • Know which elements have XML equivalents
  • Understand how web containers identify JSP Documents

JSP Documents use XML-based syntax for writing JSPs. This lesson compares the standard JSP syntax with the XML equivalent for every element type, and explains how web containers identify JSP Documents.

What is a JSP Document?

There are 2 ways to write JSPs:

  • JSP Standard Syntax — uses <% %> style tags (traditional)
  • XML Based Syntax — uses <jsp:xxx> style tags (JSP Documents)

If we develop JSPs using JSP Standard syntax, such JSPs are called JSP Pages.

If we develop JSPs using XML Based syntax, such JSPs are called JSP Documents.

Scriptlet

Standard SyntaxXML Based Syntax
<% Java Code %><jsp:scriptlet> Java Code </jsp:scriptlet>
Example02
Loading editor…

Expression

Standard SyntaxXML Based Syntax
<%= expression %><jsp:expression> expression </jsp:expression>
Example03
Loading editor…

Declaration

Standard SyntaxXML Based Syntax
<%! Java Declarations %><jsp:declaration> Java Declarations </jsp:declaration>
Example04
Loading editor…

Directives

DirectiveStandard SyntaxXML Syntax
page<%@ page import="java.util.*" %><jsp:directive.page import="java.util.*" />
include<%@ include file="header.jsp" %><jsp:directive.include file="header.jsp" />
taglib<%@ taglib prefix="m" uri="..." %>No direct equivalent — use <jsp:root> tag

Standard Actions

There is no difference in standard actions between the two syntaxes:

Example06
Loading editor…

Comments

JSP does not provide a specific XML syntax for comments. Use XML comments:

Example07
Loading editor…

Template Text

XML syntax provides a standard way to write template text using <jsp:text>:

Example08
Loading editor…

How Containers Identify JSP Documents

Web containers identify JSP Documents in 3 ways:

  1. Save the file with .jspx extension
  2. Enclose the entire JSP in a <jsp:root> tag
  3. Use <jsp-config> in web.xml
Example09
Loading editor…

Rules

  • From Servlet 2.4 onwards, the container can identify JSP Documents automatically (no special arrangement needed).
  • Do not mix standard and XML based syntax in the same file.
  • In XML syntax, all tags and attributes are case-sensitive.
  • Attribute values must be enclosed in single or double quotes.
📝 Key Takeaways
  • JSP Documents use XML tags like <jsp:scriptlet>, <jsp:expression>, etc.
  • Standard actions (jsp:useBean, etc.) are identical in both syntaxes
  • Use .jspx extension or <jsp-config> in web.xml to identify JSP Documents

🧠 Test Your Knowledge

3 Questions
Progress: 0 / 3

Finished this lesson?

Mark it as complete to track your progress and see technology completion stats.