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.
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
you would use the notation
"chocolate fudge cake"
- 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;// assign values to array elementsdesserts = "chocolate mousse";desserts = "tiramisu";desserts = "apple pie";desserts = "chocolate fudge cake";%>