Appendix D Snippets

p. 748

Bottom of page:

<%
  int salAmount = 3000;
  if (salAmount > 0) {
%>
<h2>The salary is positive.</h2>
<%
  }
  else {
%>
<table border="1">
  <tr align="center">
    <td><b>Warning!</b></td>
  </tr>
  <tr align="center">
    <td>Salary is negative or zero.<br />
      Contact your Financial Advisor.
    </td>
  </tr>
</table>
<%
  }
%>

p. 751

Scripting Elements in the servlet:

<!-- Salary display -->
<%-- scriptlets --%>
<%
  int salAmount = 3000;
  if (salAmount > 0) {
%>
<h2>The salary is positive.</h2>
<br>
<%
  out.write("The new salary is " + calcRaise(salAmount));
%>
<%
}
 else {
 %>
<h1>The salary is
<%-- expression --%>
<%= salAmount %>
</h1>
<%
 }
%>

<br>
<%// expression %>
<%= "Salary is " + salAmount %>

%-- declaration --%>
<%!
  public static double calcRaise(int salary) {
    return(salary * 1.3);
  }
%>

p. 759

JSP Standard Tag Library:

<table border="1">
<% for (int i = 1; i <= 3; i++) { %>
  <tr>
    <% for (int j = 1; j <= 4; j++) { %>
      <td>
      <%= i %>:<%= j %>
      </td>
    <%} %>
  </tr>
<% } %>
</table>

p. 760

JSTL Example:

<table border="1">
  <c:forEach begin="1" end="3" var="i" >
    <tr>
    <c:forEach begin="1" end="4" var="j">
      <td>
        <c:out value="${i}" />:<c:out value="${j}" />
      </td>
    </c:forEach>
    </tr>
  </c:forEach>
</table>

p. 766

The Sample Code:

<%@ page contentType="text/html;charset=windows-1252"%>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<html>
  <head>
    <title>Build a Table</title>
  </head>
  <body>
    <c:if test="${pageContext.request.method=='POST'}" >
      <c:set var="varName" value="rows" />
      <c:choose>
        <c:when test="${param[varName]=='0' or param.columns=='0'}" >
          Both Rows and Columns must be greater than 0.<br />
        </c:when>
        <c:otherwise>
          <table border="1">
            <c:forEach begin="1" end="${param.rows}" var="i" >
              <tr>
              <c:forEach begin="1" end="${param.columns}" var="j">
                <td>
                  <c:out value="${i}:${j}" />
                </td>
              </c:forEach>
              </tr>
            </c:forEach>
          </table>
        </c:otherwise>
      </c:choose>
    </c:if>
    
    <h2>Table Dimensions</h2>
    <form method="POST">
      Rows: <input type="text" name="rows" size="3" />
      Columns: <input type="text" name="columns" size="3" />
      <input type="submit" value="Build" /><br />
    </form>
    
  </body>
</html>