JSP Tutorials - Herong's Tutorial Examples - v5.11, by Herong Yang
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
JSP (JavaServer Pages) Overview
Tomcat Installation on Windows Systems
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
Syntax of JSP Pages and JSP Documents
JavaBean Objects and "useBean" Action Elements
Managing HTTP Response Header Lines
Non-ASCII Characters Support in JSP Pages
Overview of JSTL (JSP Standard Tag Libraries)
Multiple Tags Working Together
Using Tomcat on CentOS Systems
Connecting to SQL Server from Servlet