JSP.2.10 Scripting Elements


The JSP 1.1 specification describes three scripting language elements ”declarations, scriptlets, and expressions. A scripting language precisely defines the semantics for these elements but, informally, a declaration declares elements, a scriptlet is a statement fragment, and an expression is a complete language expression. The scripting language used in the current page is given by the value of the language directive (see Section JSP.2.7.1, "The page Directive"). In JSP 1.1, the only value defined is "java . "

Each scripting element has a "<%"-based syntax as follows :

 <%! this is a declaration %>  <% this is a scriptlet %>  <%= this is an expression %> 

White space is optional after "<%!", "<%", and "<%=" and before "%>."

The equivalent XML elements for these scripting elements are described in Section JSP.7.4.

JSP.2.10.1 Declarations

Declarations are used to declare variables and methods in the scripting language used in a JSP page. A declaration should be a complete declarative statement, or sequence thereof, according to the syntax of the scripting language specified.

Declarations do not produce any output into the current out stream.

Declarations are initialized when the JSP page is initialized and are made available to other declarations, scriptlets, and expressions.

Examples

For example, the first declaration below declares an integer and initializes it to zero; the second declaration declares a method.

 <%! int i = 0; %>  <%! public String f(int i) { if (i<3) return("..."); ... } %> 
Syntax
 <%! declaration(s) %> 

JSP.2.10.2 Scriptlets

Scriptlets can contain any code fragments that are valid for the scripting language specified in the language directive. Whether the code fragment is legal depends on the details of the scripting language; see Chapter JSP.4.

Scriptlets are executed at request-processing time. Whether or not they produce any output into the out stream depends on the actual code in the scriptlet. Scriptlets can have side effects, modifying the objects visible in them.

When all scriptlet fragments in a given translation unit are combined in the order in which they appear in the JSP page, they shall yield a valid statement or sequence thereof, in the specified scripting language.

If you want to use the %> character sequence as literal characters in a scriptlet, rather than to end the scriptlet, you can escape them by typing %\> .

Examples

Here is a simple example where the page changed dynamically depending on the time of day.

 <% if (Calendar.getInstance().get(Calendar.AM_PM) == Calendar.AM) {%>  Good Morning  <% } else { %>  Good Afternoon  <% } %> 
Syntax
 <% scriptlet %> 

JSP.2.10.3 Expressions

An expression element in a JSP page is a scripting language expression that is evaluated and the result is coerced to a String which is subsequently emitted into the current out JspWriter object.

If the result of the expression cannot be coerced to a String , then either a translation time error shall occur or, if the coercion cannot be detected during translation, a ClassCastException shall be raised at request time.

A scripting language may support side effects in expressions. If so, they take effect when the expression is evaluated. Expressions are evaluated left to right in the JSP page. If the expressions appear in more than one runtime attribute, they are evaluated left to right in the tag. An expression might change the value of the out object, although this is not something to be done lightly.

The contents of an expression must be a complete expression in the scripting language in which they are written.

Expressions are evaluated at HTTP processing time. The value of an expression is converted to a String and inserted at the proper position in the . jsp file.

Examples

In the next example, the current date is inserted.

 <%= (new java.util.Date()).toLocaleString() %> 
Syntax
 <%= expression %> 


Java 2 Platform, Enterprise Edition. Platform and Component Specifications
Java 2 Platform, Enterprise Edition: Platform and Component Specifications
ISBN: 0201704560
EAN: 2147483647
Year: 2000
Pages: 399

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net