Chapter 21 - Code Snippets
==========================
p.716:
<%
int salAmount = 3000;
if (salAmount > 0) {
%>
The salary is positive.
<%
}
else {
%>
| Warning! |
Salary is negative or zero.
Contact your Financial Advisor.
|
<%
}
%>
---------------------------------------------------------------------------
p.718:
<%-- scriptlets --%>
<%
int salAmount = 3000;
if (salAmount > 0) {
%>
The salary is positive.
<%
out.write("The new salary is " + calcRaise(salAmount));
%>
<%
}
else {
%>
The salary is
<%-- expression --%>
<%= salAmount %>
<%
}
%>
<%// expression %>
<%= "Salary is " + salAmount %>
<%-- declaration --%>
<%!
public static double calcRaise(int salary) {
return(salary * 1.3);
}
%>
---------------------------------------------------------------------------
p.742, Modify the JSP Page, step 5:
<%
for (int i = 1; i < 10; i++) {
%><%
for (int j = 1; j < 10; j++) {
%>| <%= i * j %> | <%
}
%>
<%
}
%>
---------------------------------------------------------------------------
p.748, step 1:
<%
String[] params = request.getParameterValues("inputNum");
int lastNum;
if (params != null) {
lastNum = Integer.parseInt(params[0]);
}
else {
lastNum = 9;
}
%>
---------------------------------------------------------------------------
<%
for (int i = 1; i <= lastNum; i++) {
%><%
for (int j = 1; j <= lastNum; j++) {
%>| <%= i * j %> | <%
}
%>
<%
}
%>
---------------------------------------------------------------------------
p.750:
<%
// Header loop
for (int i = 1; i < 10; i++) {
%>|
<%= i %>
| <%
}
%><%
// Body nested loops from row 2 to 9
for (int i = 2; i < 10; i++) {
// first column
%>| <%= i %> | <%
for (int j = 2; j < 10; j++) {
// columns 2 to 9
%>
<%= i * j %>
| <%
}
%>
<%
}
%>
---------------------------------------------------------------------------