What Are Scripting Elements

This section describes what are scripting elements - code fragments in JSP pages written the default Java language. 3 types of scripting elements are supported in JSP 2.1: scriptlets, scripting expressions, and scripting declarations.

With Apache Tomcat installed, we are ready to learn and write JSP pages. The easiest way to start writing JSP page is probably to use scripting elements, if you know the Java language.

What Are Scripting Elements? Scripting elements are code fragments in JSP pages written the default Java language.

There are 3 types of scripting elements supported in JSP: scriptlets, scripting expressions, and scripting declarations. They will be converted into the Java Servlet class source code during the JSP compilation step differently as described below:

1. Scriptlets - Java code fragments that will be converted into the Servlet source code as is. Scriptlets must be enclosed in these special tags: "<%" and "%>" in a JSP page. For example, the following JSP page contains one scriptlet, which is a single Java statement invoking the out.println() method. This Java statement will be converted into the Servlet source code directly.

In JSP page:
...
<% out.println("Hello world!"); %>
...

In Servlet class:
...
out.println("Hello world!");
...

2. Scripting Expressions - Java expressions that will be converted into the Servlet source code as out.print(expression) statements. Scripting expressions must be enclosed in these special tags: "<%=" and "%>" in a JSP page. For example, the following JSP page contains one scripting expression, which is a Java String literal. This expression will be converted into the Servlet source code as an out.print(expression) statement.

In JSP page:
...
<%= "-- From JSP" %>
...

In Servlet class:
...
out.print("-- From JSP");
...

3. Scripting Declarations - Java class level declaration blocks that will be converted into the Servlet source code as is and placed them at the class header level. Scripting declarations must be enclosed in these special tags: "<%!" and "%>" in a JSP page. For example, the following JSP page contains one scripting declaration, which is a method declaration. This declaration will be converted into the Servlet source code directly at the class header level.

In JSP page:
...
<%!
   private void greeting() {
      out.println("To Everyone:");
   }
%>
...

In Servlet class:
...
   private void greeting() {
      out.println("To Everyone:");
   }
...

Table of Contents

 About This Book

 JSP (JavaServer Pages) Overview

 Tomcat Installation on Windows Systems

JSP Scripting Elements

What Are Scripting Elements

 Converting JSP Pages with Scripting Elements

 Example JSP Page with Scripting Elements

 Example Java Class Converted from a JSP Page

 Mixing Static Data with Scriptlets for Compound Statements

 Java Servlet Introduction

 JSP Implicit Objects

 Syntax of JSP Pages and JSP Documents

 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