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
JCode Cell
1 
2Standard: <% out.println("Hello"); %>
3XML: <jsp:scriptlet>out.println("Hello");</jsp:scriptlet>
4

Expression

Standard SyntaxXML Based Syntax
<%= expression %><jsp:expression> expression </jsp:expression>
Example03
JCode Cell
1 
2Standard: <%= 2+3 %>
3XML: <jsp:expression>2+3</jsp:expression>
4

Declaration

Standard SyntaxXML Based Syntax
<%! Java Declarations %><jsp:declaration> Java Declarations </jsp:declaration>
Example04
JCode Cell
1 
2Standard: <%! int count = 0; %>
3XML: <jsp:declaration>int count = 0;</jsp:declaration>
4

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
JCode Cell
1 
2<%-- Both syntaxes use the same standard actions --%>
3<jsp:useBean id="c" class="pack1.CustomerBean" />
4<jsp:forward page="second.jsp" />
5

Comments

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

Example07
JCode Cell
1 
2<!-- this is an XML comment -->
3

Template Text

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

Example08
JCode Cell
1 
2<%-- XML template text --%>
3<jsp:text>Any template text here</jsp:text>
4

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
JCode Cell
1 
2<web-app>
3 <jsp-config>
4 <jsp-property-group>
5 <url-pattern>*.jspx</url-pattern>
6 <jsp-xml>true</jsp-xml>
7 </jsp-property-group>
8 </jsp-config>
9</web-app>
10

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