Before writing your first JSP, you need to gain an understanding of the syntax and the structure of a JSP.
As you have seen, JSP elements are embedded in static HTML. Like HTML, all JSP elements are enclosed in open and close angle brackets (< >). Unlike HTML, but like XML, all JSP elements are case sensitive.
JSP elements are distinguished from HTML tags by beginning with either <% or <jsp:. JSPs follow XML syntax, they all have a start tag (which includes the element name) and a matching end tag. Like XML tags, a JSP tag with an empty body can combine the start and end tags into a single tag. The following is an empty body tag:
<jsp:useBean id="agency" class="web.AgencyBean"> </jsp:useBean>
The following tag is equivalent to the previous example:
<jsp:useBean id="agency" class="web.AgencyBean"/>
Optionally, a JSP element may have attributes and a body. You will see examples of all these types during today's lesson. See Appendix C, “An Overview of XML,” for more information on XML and the syntax of XML elements.
The basic JSP elements are summarised in Table 13.1.
You will learn about scripting elements first and then cover directives and actions later in today's lesson.
Scripting elements contain the code logic. It is these elements that get translated into a Java class and compiled. There are three types of scripting elements—declarations, scriptlets, and expressions. They all start with <% and end with %>.
Declarations are used to introduce one or more variable or method declarations, each one separated by semicolons. A variable must be declared before it is used on a JSP page. Declarations are differentiated from other scripting elements with a <%! start tag. An example declaration that defines two variables is as follows:
<%! String color = "blue"; int i = 42; %>
You can have as many declarations as you need. Variables and methods defined in declarations are declared as instance variables outside of any methods in the class.
JSP expressions are single statements that are evaluated, and the result is cast into a string and placed on the HTML page. An expression is introduced with <%= and must not be terminated with a semi-colon. The following is an expression that will put the contents of the i element in the items array on the output page.
<%= items[i] %>
JSP expressions can be used as values for attributes in JSP tags. The following example shows how the i element in the items array can be used as the value for a submit button on a form:
<INPUT type=submit value="<%= items[i] %>">
Note
There cannot be a space between the <% and = signs, because this would cause a syntax error in the JSP (see the later section on the “JSP Lifecycle” for how JSP errors are detected and reported).
Scriptlets contain code fragments that are processed when a request is received by the JSP. Scriptlets are processed in the order they appear in the JSP. They need not produce output.
Scriptlets can be used to create local variables, for example
<% int i = 42;%> <BIG>The answer is <%= i %></BIG>
The difference between scriptlet variables and declarations is that scriptlet variables are scoped for each request. Variables created in declarations can retain their values between requests (they are instance variables).
There are three types of comments in a JSP page. The first type is called a JSP comment. JSP comments are used to document the JSP page. A JSP comment is completely ignored; it is not included in the generated code. A JSP comment looks like the following:
<%-- this is a JSP comment --%>
An alternative way to comment a JSP is to use the comment mechanism of the scripting language, as in the following:
<% /* this is a java comment */ %>
This comment will be placed in the generated Java code.
The third mechanism for adding comments to a JSP is to use HTML comments.
<!-- this is an HTML comment -->
HTML comments are passed through to the client as part of the response. As a result, this form can be used to document the generated HTML document. Dynamic information can be included in HTML comments as shown in the following:
<!-- comment <%= expression %> comment -->
3.144.123.155