Java
  Home arrow Java arrow Page 8 - Making Decisions, Decisions
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Making Decisions, Decisions
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 3
    2005-05-05

    Table of Contents:
  • Making Decisions, Decisions
  • Creating Multidimensional Arrays
  • Comparing Data Values
  • Making Decisions
  • Understanding Variable Scope in Statement Blocks
  • How It Works
  • Understanding the Conditional Operator
  • Trying It Out: Working with the choose...when...when Construct
  • Understanding Loops and Iteration
  • Introducing Branching Statements
  • Trying It Out: Using Arrays

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

    More Java Articles
    More By Apress Publishing


     

    Buy this book now. This article was excerpted from Beginning JSP 2: From Novice to Professional by Peter den Haan et. al. (Apress, 2004; ISBN: 1590593391). Check it out at your favorite bookstore. Buy this book now.

    JAVA ARTICLES

    - 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 ...
    - Generics and Limitations in Java
    - Getting Started with Java Web Development in...







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek