Java
  Home arrow Java arrow Page 6 - The JSP Files (part 2): Attack Of The ...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
VPS Hosting 
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

The JSP Files (part 2): Attack Of The Killer Fortune Cookies
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 4
    2001-02-22

    Table of Contents:
  • The JSP Files (part 2): Attack Of The Killer Fortune Cookies
  • Flavour Of The Month
  • Do It Or Else...
  • Cookie-Cutter Code
  • Lunch In Milan
  • Switching Things Around

  • 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


    The JSP Files (part 2): Attack Of The Killer Fortune Cookies - Switching Things Around


    (Page 6 of 6 )

    Finally, JSP rounds out its conditional expressions with the "switch" statement, which offers an alternative method of transferring control from one program block to another. Here's what it looks like:

    switch (decision-variable) { case first_condition_is true: do this! case second_condition_is true: do this! case third_condition_is true: do this! ... and so on... default: do this by default! }
    The "switch" statement can best be demonstrated by rewriting the previous example using "switch" instead of "if-else if-else".

    <%! int dayOfWeek = 3; String fortune; %> <% // the decision variable here is the day chosen by the user switch (dayOfWeek) { // first case case 1: fortune = "Adam met Eve and turned over a new leaf."; break; // second case case 2: fortune = "Always go to other people's funerals, otherwise they won't come to yours."; break; case 3: fortune = "An unbreakable toy is useful for breaking other toys."; break; case 4: fortune = "Be alert - the world needs more lerts."; break; case 5: fortune = "Crime doesn't pay, but the hours are good."; break; // if none of them match... default: fortune = "Sorry, closed on the weekend"; break; } // print output out.println(fortune); %>
    The first thing you'll notice is that the "day" variable from the previous example has been converted to a numeric "dayOfWeek" variable - this is because the "switch" construct only works when the decision variable is an integer.

    There are also a couple of important keywords here: the "break" keyword is used to break out of the "switch" statement block and move immediately to the lines following it, while the "default" keyword is used to execute a default set of statements when the variable passed to "switch" does not satisfy any of the conditions listed within the block.

    And that's about it. You now know enough about JSP's conditional statements to begin writing simple programs - so go practice! And come back for the next issue, when we'll be talking about loops, demonstrating other String object methods, and even taking a quick look at the new Response object.
    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

     

       

    JAVA ARTICLES

    - Adding Images With iTextSharp
    - Adding Columns With iTextSharp
    - Creating Simple PDF Files With iTextSharp
    - The Spring Framework: Understanding IoC
    - Introducing the Spring Framework
    - Java Classes
    - Completing the Syntactic Comparison of Java ...
    - Syntactic Comparison of Java and C/C++
    - Java Statements
    - Conditionals, Expressions and Other Java Ope...
    - Java Operators
    - Primitive Data Types and Basic Language Rule...
    - Java and Object-Oriented Programming
    - Java Beginning Programming
    - Gaming Development Setup





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway