JSP Tag Libraries

Скачать в pdf «JSP Tag Libraries»

■    Executing logic on the server side; for example, accessing a database.

■    Implementing conditional HTML by posing a condition on the execution of portions of the page.

■    Looping over JSP fragments, enabling operations such as populating a table with dynamic content.

The bits of code we saw in listing 2.2 were scriptlets that performed some conditional logic. To see another scriptlet in action, take a look at listing 2.3.

Listing 2.3 Sample JSP file that uses scriptlets




double num = Math.random(); boolean bigger = num > 0.5; int cap = (int)(num * 10);



Is <% out.print(num); %> bigger then 0.5? <br> О <% if(bigger) { %>    C


<% } else { %>


<% } %>



Now, let’s loop randomly … <br>

<% for(int i = 0 ; i < cap ; i++)    { %>

This is iteration number <% out.print(i); %>. <br> <% } %>




О Our very simple business rules and output scriptlets The first scriptlet is embedding java code that represents the business logic, in this case just some simple math. Once our math logic is complete, we print the response to the user with the out.print.() statement.

e Some conditional control based on the value of bigger The second set of scriptlets is performing conditional HTML; the condition is posed over the results of the “business logic,” namely, the variable bigger.

Скачать в pdf «JSP Tag Libraries»