Home arrow Java & J2EE arrow Page 6 - The JSP Files (part 4): The Red Pill

What's For Dessert? - Java

Now that the basics are done with, it's time to begin applyingthat theory to Web development. This week, The JSP Files investigates theJSP Request object, and demonstrates how it can be used to process formdata. Learn about JSP's array variables, find out what chocolate chipcookies and couch potatoes have in common, and explore a very unusualpharmacy.

  1. The JSP Files (part 4): The Red Pill
  2. The Last Action Hero
  3. Entering The Matrix
  4. Requesting More
  5. Taking Some Medication
  6. What's For Dessert?
  7. A Chocolate Addiction
  8. Couch Potato
  9. Beating It Into Submission
By: Vikram Vaswani, (c) Melonfire
Rating: starstarstarstarstar / 6
March 07, 2001

print this article


Thus far, the variables you've used contain only a single value - for example,

int i = 0
However, array variables are a different kettle of fish altogether. An array variable can best be thought of as a "container" variable, which can contain one or more values. For example,

String[] desserts = {"chocolate mousse", "tiramisu", "apple pie", "chocolate fudge cake"};
Here, "desserts" is an array variable, which contains the values "chocolate mousse", "tiramisu", "apple pie", and "chocolate fudge cake".

Array variables are particularly useful for grouping related values together - names, dates, phone numbers of ex-girlfriends et al.

The various elements of the array are accessed via an index number, with the first element starting at zero. So, to access the element

"chocolate mousse"
you would use the notation


"chocolate fudge cake"
would be

- essentially, the array variable name followed by the index number enclosed within square braces. Geeks refer to this as "zero-based indexing".

Defining an array variable in JSP is somewhat convoluted, as compared to languages like PHP and Perl; you need to first declare the variable and its type, and then actually create the array.

<% // declare type of arrayString[] desserts;// initialize array// the number indicates the number of elements the array will holddesserts = new String[4];// assign values to array elementsdesserts[0] = "chocolate mousse";desserts[1] = "tiramisu";desserts[2] = "apple pie";desserts[3] = "chocolate fudge cake";%>
Or you can use the simpler version:

<% String[] desserts = {"chocolate mousse", "tiramisu", "apple pie","chocolate fudge cake"};%>
Note that if you try to add more elements than the number specified when creating the array, JSP will barf with a series of strange error message.

>>> More Java & J2EE Articles          >>> More By Vikram Vaswani, (c) Melonfire

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- More Java Bugs Lead to More Attacks
- Oracle's Java One Brings News, Surprises
- Oracle Patches Java Runtime Environment
- Apple Syncs Java Update with Oracle
- Spring 3.1 Java Development Framework Compat...
- Jelastic Java PaaS Availability and Pricing ...
- NetBeans 7.1 Released, Supports JavaFX 2
- SolarWinds Releases Newest Version of Java M...
- Free Monitoring Tool for Java Apps on Heroku
- Heroku Adds JCloud Platform Support, Java 7 ...
- Java SE 8 Speculation in Full Swing
- Java SE 7 Now Available
- New JVM Language and Java Reporting Tool
- Java 7 Release Update and New Eclipse Toolkit
- The Best Java Netbeans IDE Plugins

Developer Shed Affiliates


Dev Shed Tutorial Topics: