Java
  Home arrow Java arrow Page 6 - 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 
Sun Developer Network 
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 - How It Works


    (Page 6 of 11 )

    You’ve already examined the HTML code quite closely, so you’ll now concentrate on the second file,whenexamplehandler.jsp. This file checks whether quantity is greater than zero using the EL inside a JSTL <when> tag:

    <c:when test="${param.quantity > 0}">

    If this is true, what appears before the closing <when> tag will be used for the final page, which here is simply the message: Thank you for your order!! As you see, the <c:when ></c:when> tags act like the if { } block in normal Java code.

    If, however, the EL condition in the <when> tag isn’t true, then you show the message Sorry, please enter a positive quantity:

    <c:otherwise>
      Sorry, please enter a positive quantity
    </c:otherwise>

    In this case, the <c:otherwise></c:otherwise> tags behave like a Java else block. Be warned that the relationship between if...else and <c:if>...<c:otherwise> isn’t perfect. Some have likened the JSTL tags to the switch statement, which is covered in the “Using the switch Statement” section.

    Notice that you could display the quantity of items ordered on this page quite easily by inserting the following EL in whenexamplehandler.jsp:

    <c:when test="${param.quantity > 0}" >
      Thank you for your order of ${param.quantity} items!! </c:when>

    The final question in the “Trying It Out: Using the JSTL with Your Pizza Form” section at the end of Chapter 3 touched on the problem of ensuring your users submit meaningful data. In this example, for instance, there’s nothing to stop a user from entering letters or other non-numeric characters in the Quantity box. The JSTL will attempt to convert the parameter value to an appropriate type (an integer in this case), but often conversion simply isn’t possible, resulting in a screen of user-scaring error messages, as you can see in Figure 5-5.


    Figure 5-5.  A string has been entered in the form

    It’s important to prevent such unprofessional outcomes in your programs, and you do it by the process of input validation. Validation involves anticipating places where inappropriate input is possible, handling such eventualities by checking the submitted input, and displaying a warning of your own if the input isn’t of the correct type. This could involve adding code to the page to perform this task, or you could pass the responsibility off to another component. Chapter 10 introduces Struts, a framework that, among many other things, makes the validation of user input fairly simple.

    You can use an if statement to gain some simple protection. The final example in Chapter 3 checked that the title wasn’t null but not the price. And it didn’t check to see that the price wasn’t a negative number. As a partial solution, you could change the conditional logic to the following:

    <c:if test="${(param.title != null) && (param.price > 0)}">

    Unfortunately, it’s still possible that an error could occur should price not be a number. The handling of errors (known as exceptions in Java) that may occur is an important consideration for any programmer.

    Nesting if Statements

    You can model this “choice within a choice” in Java by embedding an if statement or an if...else statement inside another if or if...else statement. This is known as nesting if statements and is generally as follows (the nested statements are in bold):

    if (expression1)
    {
      if (expression2)
      {
        Statement Block 1
      }
    }
    else
    {
      if (expression3)
      {
        Statement Block 2
      }
      else
      {
        Statement Block 3
      }
    }

    This ability to nest statements shouldn’t be entirely unexpected. Because a statement block is simply one or more lines of Java code, there’s no reason why an if statement block shouldn’t contain further if and if...else statements.

    You’ll now see a code snippet to illustrate this point. You’ll return to the earlier shipping cost scenario. Here, your customer is allowed free shipping only if more than four items are purchased. If he spends more than $100 on his five or more items, then the customer gets a 5 percent discount as well. However, if he spends more than $2,000 on the five or more items, the customer gets a free television:

    if ( itemQuantity > 4 )
    {
      System.out.println("Free Shipping for You!!");
      if (itemCost > 100)
      {
        System.out.println("5% Discount for You!!");
        if (itemCost > 2000)
        {
        System.out.println("Free Television for You!!");
       }
      }
    }

    Notice that you have more than one level of nesting here: You can have as many levels as you need, and you can create the same construct with JSTL <choose> tags:

    <c:choose>
      <c:when test="${itemQuantity > 4}" >
        <c:choose>
          <c:when test="${itemCost > 2000}">
            You've earned a free television, plus a 5% discount and free shipping.
          
    </c:when>
          
    <c:when test="${itemCost > 100}">
          5% discount on your purchase, in addition to no shipping charge.
          </c:when>
          <c:otherwise>
          We will ship your order free of charge.
          </c:otherwise>
        </c:choose>
      </c:when>
    </c:choose>

    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-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT