Coding and Invoking a Method

<%-- Here's a variable and method declaration --%> <%! int toInteger = 100 ; int sumOfFirstIntegers = addIntegers( 100 ) ; public int addIntegers( int to ) { return to * (to + 1) / 2 ; }%>

The sum of integers from 1 to <%= toInteger %> is: <%= sumOfFirstIntegers %> Listing 4-3 also includes a few JSP expressions. As you may have guessed by now, you can code JSP declarations in XML style syntax as follows: Java Code JSPs also permit you to code pieces of Java in your page by coding scriptlets, as explained in the next section. JSP Scriptlets A scriptlet is an arbitrary piece of Java code. The general format is as follows: <% aPieceOfJavaCode %> Understand that a piece of code can be entire statements or groups of statements. Scriptlets are executed at request time. Hence, code contained in scriptlets may modify objects by invoking methods. Listing 4-4 shows a page similar to Listing 4-3 but using a scriptlet. Listing 4-4: JSP page with a scriptlet <%@ page contentType="text/html" %> <%-- Here's a JSP scriptlet --%> <% int toInteger = 100 ;
Please take a look on frontpage web hosting, and see why we provide the most affordable frontpage web hosting on the best equipment.

Leave a Reply