Java
  Home arrow Java arrow Page 2 - The JSP Files (part 2): Attack Of The ...
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 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
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 - Flavour Of The Month


    (Page 2 of 6 )

    And just as you can compare numbers, JSP also allows you to compare strings, with a couple of very useful String object methods.

    First, the equals() method allows you to check whether the value of a particular string variable matches another. The following example should demonstrate this.


    <% // define variables String myFavourite = "chocolate"; String yourFavourite = "strawberry"; // compare strings if (myFavourite.equals(yourFavourite)) { out.println("A match made in heaven!"); } else { out.println("Naw - try again!"); } %>
    Try changing the values of the variables to match each other, and gasp in awe as the output changes.

    In case the equals() method doesn't appeal to you, JSP offers you a choice in the form of the compareTo() method, which returns a value indicating which of the two strings is greater. Take a look:

    <% // define variables String alpha = "abcdef"; String beta = "zyxwvu"; // compare strings out.println(alpha.compareTo(beta)); %>
    In this case, if the value of the variable "beta" is greater than that of the variable "alpha", the compareTo() method will return a negative integer; if it's the other way around, the comparison will return a positive integer. And if the two strings are identical, the comparison will return 0.

    Incidentally, the comparison is based on both the first character of the string, and the number of characters in the string. One string is considered "greater" than another if the numeric value of its first character is greater, or if its length is greater. In the example above, "z" has a greater numeric code than "a", and so the comparison will return a negative integer. But don't take our word for it - try it yourself and see!{mospagebreak title=Turning Up The Heat} Why do you need to know all this? Well, comparison operators come in very useful when building conditional expressions - and conditional expressions come in very useful when adding control routines to your code. Control routines check for the existence of certain conditions, and execute appropriate program code depending on what they find.

    The first - and simplest - decision-making routine is the "if" statement, which looks like this:


    if (condition) { do this! }
    The "condition" here refers to a conditional expression, which evaluates to either true or false. For example,

    if (hard drive crashes) { get down on knees and pray for redemption }
    or, in JSP-lingo.

    <% if (hdd == 0) { pray(); } %>
    If the conditional expression evaluates as true, all statements within the curly braces are executed. If the conditional expression evaluates as false, all statements within the curly braces will be ignored, and the lines of code following the "if" block will be executed.

    Here's a simple program that illustrates the basics of the "if" statement.

    <%! // declare temperature variable int temp = 50; %> <% // check temperature and display output if (temp > 30) { out.println("Man, it's hot out there!"); } %>
    In this case, a variable named "temp" has been defined, and initialized to the value 50. Next, an "if" statement has been used to check the value of the "temp" variable and display a message if it's over 30. Note our usage of the greater-than (>) conditional operator in the conditional expression.

    An important point to note - and one which many novice programmers fall foul of - is the difference between the assignment operator [=] and the equality operator [==]. The former is used to assign a value to a variable, while the latter is used to test for equality in a conditional expression.

    So

    a = 47;
    assigns the value 47 to the variable a, while

    a == 47
    tests whether the value of a is equal to 47.

    More Java Articles
    More By Vikram Vaswani and Harish Kamath, (c) Melonfire


     

       

    JAVA ARTICLES

    - New Object Initialization in Java
    - 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





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT