Nearby lessons
10 of 30JSP - 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 Syntax | XML Based Syntax |
|---|---|
<% Java Code %> | <jsp:scriptlet> Java Code </jsp:scriptlet> |
Example02
Expression
| Standard Syntax | XML Based Syntax |
|---|---|
<%= expression %> | <jsp:expression> expression </jsp:expression> |
Example03
Declaration
| Standard Syntax | XML Based Syntax |
|---|---|
<%! Java Declarations %> | <jsp:declaration> Java Declarations </jsp:declaration> |
Example04
Directives
| Directive | Standard Syntax | XML 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
Comments
JSP does not provide a specific XML syntax for comments. Use XML comments:
Example07
Template Text
XML syntax provides a standard way to write template text using <jsp:text>:
Example08
How Containers Identify JSP Documents
Web containers identify JSP Documents in 3 ways:
- Save the file with
.jspxextension - Enclose the entire JSP in a
<jsp:root>tag - Use
<jsp-config>inweb.xml
Example09
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 QuestionsProgress: 0 / 3