8.3.2.3 JavaServer Pages API Technology
JavaServer Pages is Java technology that helps software developers serve dynamically generated web pages based on HTML, XML or other document types. Released in 1999 as Sun’s answer to ASP and PHP, JSP was designed to address the perception that the Java programming environment didn’t provide developers with enough support for the Web.
Architecturally, JSP may be considered a high-level abstraction of Java Servlets. JSP pages are loaded in the server and operated from a structured special installed Java server packet called a Java EE Web Application, often packaged as a .war or .ear file archive.
JSP allows Java code and certain pre-defined actions to be interleaved with static Web markup content, with the resulting page being compiled and executed on the server to deliver an HTML or XML document. The compiled pages and any dependent Java libraries use Java byte-code rather than a native software format and must therefore be executed within a Java Virtual Machine (JVM) that integrates with the host operating system to provide an abstract platform-neutral environment.
JSP syntax is a fluid mix of two basic content forms: scriptlet elements and markup. Markup is typically standard HTML or XML, while scriptlet elements are delimited blocks of Java code which may be intermixed with the markup. When the page is requested, the Java code is executed and its output is added, in situ, with the surrounding markup to create the final page. Because Java is a compiled language, not a scripting language, JSP pages must be compiled to Java byte-code classes before they can be executed, but such compilation is needed only when a change to the source JSP file has occurred.
Java code is not required to be complete (self contained) within its scriptlet element block but
can straddle markup content provided the page as a whole is syntactically correct (for example, any Java if/for/while blocks opened in one scriptlet element must be correctly closed in a later ele-ment for the page to successfully compile). This system of split inline code sections is called step over scripting because it can wrap around the static markup by stepping over it. Markup which falls inside a split block of code is subject to that code, so markup inside an if block will only appear in the output when the if condition evaluates to true; likewise, markup inside a loop construct may appear multiple times in the output depending upon how many times the loop body runs.
The JSP syntax adds additional XML-like tags, called JSP actions, to invoke built-in functional-ity. Additionally, the technology allows for the creation of JSP tag libraries that act as extensions to the standard HTML or XML tags. JVM-operated tag libraries provide a platform-independent way of extending the capabilities of a Web server. Note that not all commercial Java servers are Java EE specification compliant.
JavaServer Pages (JSP) technology lets you put snippets of Servlet code directly into a text-based document. A JSP page is a text-based document that contains two types of text: static data (which can be expressed in any text-based format such as HTML, WML and XML) and JSP elements, which determine how the page constructs dynamic content.
The JavaServer Pages Standard Tag Library (JSTL) encapsulates core functionality common to many JSP applications. Instead of mixing tags from numerous vendors in your JSP applications, you employ a single, standard set of tags. This standardization allows you to deploy your applications on any JSP container that supports JSTL and makes it more likely that the implementation of the tags is optimized.
JSTL has an iterator and conditional tags for handling flow control, tags for manipulating XML documents, internationalization tags, tags for accessing databases using SQL and commonly used functions.
JSP pages are compiled into Servlets by a JSP compiler. The compiler either generates a Servlet in Java code that is then compiled by the Java compiler, or it may compile the Servlet to byte code which is directly execuTable. JSPs can also be interpreted on the fly, reducing the time taken to reload changes.
JSP simply puts Java inside HTML pages using JSP tags. You can take any existing HTML page and change its extension to .jsp instead of .html.
Regardless of whether the JSP compiler generates Java source code for a Servlet or emits the byte code directly, it is helpful to understand how the JSP compiler transforms the page into a Java Servlet. For example, consider the input JSP page shown in Figure 8.38. This JSP page can be com-piled to create its resulting generated Java Servlet. The JSP tags <% . . . %> or <jsp . . . /> enclose Java expressions, which are evaluated at runtime by JVM.

FIGURE 8.38 An example of JSP pages.
Refer to Figure 8.38. In step A, two JSP code lines declare a JSP page and an import component. Then, in step B, two Java integer variables are created; one is an instance variable, and the other is a Stack-based variable.