Writing Scriptlet Element in XML Format

This section describes the syntax of writing Scriptlet Elements in XML format for a JSP document. The Java statement block should be enclosed in a CDATA sequence to protect special characters.

Writing Scripting Elements in XML format for a JSP Document looks easy. See syntax below:

<jsp:declaration> Java declaration statements </jsp:declaration%>

<jsp:scriptlet> Java statements </jsp:scriptlet>

<jsp:expression> Java expression </jsp:expression>

But don't forget that the entire JSP Document, including Java statements, must a well formatted XML document.

This is actually a tough requirement, because of the '<' operator and other special operators are commonly used in many Java statements. One solution is to use the <![CDATA[...]]> sequence to protect the Java statement block or expression.

Here is an example of how use CDATA to protect Java statements, LoopTest.jspx:

<?xml version="1.0"?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.3">
<!-- LoopTest.jspx
 - Copyright (c) 2006 HerongYang.com. All Rights Reserved.
-->
<jsp:directive.page contentType="text/html"/>
<html><body>
<p>
<jsp:scriptlet><![CDATA[
   out.println("<b>Sum of 1, 2, ... 10:</b><br/>");
   int sum = 0;
   for (int i=0; i<10; i++) {
      sum += i+1;
   }
   out.println(sum);
]]></jsp:scriptlet>
</p>
</body></html>
</jsp:root>

If you open this page with a Web browser, you should have no problem to see the result:

Sum of 1, 2, ... 10:
55

Table of Contents

 About This Book

 JSP (JavaServer Pages) Overview

 Tomcat 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

 JSP Document - JSP Version Error

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

 Using Tomcat on CentOS Systems

 Using Tomcat on macOS Systems

 Connecting to SQL Server from Servlet

 Developing Web Applications with Servlet

 Archived Tutorials

 References

 Full Version in PDF/EPUB