Home arrow Java arrow Page 8 - Making Decisions, Decisions
JAVA

Making Decisions, Decisions


Most JavaServer Pages will need to perform one of several actions according to some condition. This is managed in the code through control statements, which come in three flavors: conditional, iterative, and branching. This article will explain the syntax Java provides for these statements, how and when to use arrays, and more. It is taken from chapter five of Beginning JSP 2 From Novice to Professional, written by Peter den Haan et. al. (Apress, 2004; ISBN: 1590593391).

Author Info:
By: Apress Publishing
Rating: 4 stars4 stars4 stars4 stars4 stars / 11
May 05, 2005
TABLE OF CONTENTS:
  1. · Making Decisions, Decisions
  2. · Creating Multidimensional Arrays
  3. · Comparing Data Values
  4. · Making Decisions
  5. · Understanding Variable Scope in Statement Blocks
  6. · How It Works
  7. · Understanding the Conditional Operator
  8. · Trying It Out: Working with the choose...when...when Construct
  9. · Understanding Loops and Iteration
  10. · Introducing Branching Statements
  11. · Trying It Out: Using Arrays

print this article
SEARCH DEVARTICLES

Making Decisions, Decisions - Trying It Out: Working with the choose...when...when Construct
(Page 8 of 11 )

To use the choose...when...when construct, follow these steps:

  1. You’ll create an HTML file to build the initial form displayed to the user. Call this file whenexample2.html, and place it in your Decisions folder. It starts out identically to your previous HTML file: 

    <html>
      <head>
        
    <title>Conditional Statements</title>
    </head>
    <body>
  2. As before, you create an HTML form, containing a table, that will post its data to a JSP:

    <form method="POST" action="whenexamplehandler2jsp">
      <table border="0" cellpadding="0" cellspacing="0" width="100%">
        <tr>
          <td><b>View Fruit Details</b></td>
          <td></td>
          <td></td>
        </tr>
        <tr>
          <td colspan="3">
  3. This time, you create a drop-down list, with the following HTML:

    <select size="1" name="fruit">
      <option selected value="1">Orange</option>
      <option value="2">Apple</option>
      <option value="3">Pear</option>
    </select>
  4. You need a submit button again, this time labeled Get Details:

                <input type="submit" value="Get Details" name="GetDetailsBtn" />
              </td>
            </tr>
          </table>
        </form>
      </body>
    </html>
  5. When the user clicks the Get Details button, they’ll be passed to the whenexamplehandler2.jsp file. This is fairly short and appears as follows in its entirety:

    <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>
    <html>
      <head>
        <title>Decisions Examples</title>
      </head>
      <body>
        <b>Fruit Details Page</b><br /><br />
        <c:choose>
          <c:when test="${param.fruit eq 1}" >
            Orange<br />
            Price : $0.30, Color: Orange<br />
        </c:when>
         <c:when test="${param.fruit eq 2}" >
            Apple<br />
            Price : $0.35, Color: Red<br />
          </c:when>
          <c:when test="${param.fruit eq 3}" >
            Pear<br />
            Price : $0.50, Color: Brown<br />
          </c:when>
        </c:choose>
      </body>
    </html>
  6. Now browse to localhost:8080/Decisions/whenexample2.html in your browser. Select a fruit from the drop-down list, as shown in Figure 5-6.


    Figure 5-6.  Choose a fruit.

  7. If you choose Apple and click Get Details, you’ll see the information shown in Figure 5-7.


    Figure 5-7. The chosen fruit’s details are displayed.

How It Works

The fruit selected in the initial HTML page is stored in a form field called fruit, and you use it in the test attribute of JSTL <when> tags in your JSP page:

<c:when test="${param.fruit eq 2}" >
  Apple<br />
  Price : $0.35, Color: Red<br />
</c:when>

Notice the JSTL relational operator eq, which has the same function as the equals sign. Also note the similarity between the <c:when> tag and the case statement in Java’s switch construct.


blog comments powered by Disqus
JAVA ARTICLES

- Java Too Insecure, Says Microsoft Researcher
- Google Beats Oracle in Java Ruling
- Deploying Multiple Java Applets as One
- Deploying Java Applets
- Understanding Deployment Frameworks
- Database Programming in Java Using JDBC
- Extension Interfaces and SAX
- Entities, Handlers and SAX
- Advanced SAX
- Conversions and Java Print Streams
- Formatters and Java Print Streams
- Java Print Streams
- Wildcards, Arrays, and Generics in Java
- Wildcards and Generic Methods in Java
- Finishing the Project: Java Web Development ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials