Java Reference
In-Depth Information
Creating a JavaServer Page
A JavaServer Page is quite similar to an HTML page because it consists largely
of HTML code. JSPs are Web pages with one or more sections of scriptlets
surrounded by JSP tags and embedded within the HTML. Just as certain
characters called tags indicate HTML elements, JSPs also have special tags to
separate JSP scriptlets from HTML. Table 12-6 lists JSP tags. Tags typically are
not case-sensitive.
Table 12-6
JSP Tags
TAG
PURPOSE
<%
Begins a scriptlet that contains one or more Java statements
%>
Ends all JSP sections, except JSP comments
<%=
Begins an expression that can be converted to a string
<%@
Begins a JSP directive for conditions that apply to the entire JSP, such as a
page directive for an import attribute
<%—
Begins a JSP comment; JSP comments, just as Java comments within a
scriptlet, are not compiled or executed; however, HTML comments are
compiled and executed, but are not displayed by the browser
—%>
Ends a JSP comment
<%!
Begins a JSP declaration; declares instance variables or methods
Because most of a JavaServer Page is HTML code, it is easier for a Web
designer to write the portion of the JSP that displays the view to the user. A JSP
also is similar to a servlet, in that it contains sections of Java code as you might
expect to find in a Java program. As shown in Table 12-6, tags provide the capa-
bility to insert scriptlets, string values, comments, JSP directives, methods, and
Java declarations of instance variables and methods within an HTML document.
Because a JSP is a hybrid of HTML and Java code, often a Web designer will cre-
ate the HTML portion and then a Java programmer will write the Java portion
within the HTML.
With a JSP, one or more sections of Java code, surrounded by JSP tags, are
embedded within the HTML; the JSP is translated into a servlet that is compiled
when it is first called to execute. The resulting servlet contains statements that
output the HTML code, as would be done with any servlet that creates a Web
page. It is important to note that the resulting product of a compiled JSP is a
servlet that executes on the server. Although JSP is embedded in HTML source
code and may appear to be somewhat like JavaScript, it is a server-side resource
and does not execute on the client-side.
You may notice that a new or changed JSP initially displays more slowly than
a similar Web page and more quickly thereafter. When the JSP first is requested
from the Web server, the JSP container translates the JSP into a servlet, replacing
the .jsp extension with _jsp.java, and then compiles it, causing the JSP to take
extra time to display. Subsequent calls use the compiled code and display a result
more quickly. If a compiler error is generated, it displays as a Web page and
 
Search WWH ::




Custom Search