- 1). Open a text editor like "WordPad." This text file will be where you place all of your source code. You will need to create the skeleton of a web page first. Write the following text inside the source code file:
<HTML>
<HEAD>
<TITLE>Accessing an Array</TITLE>
</HEAD>
<BODY>
<%
%>
</BODY>
</HTML> - 2). Place all of the JSP code found in the following steps between the <% and %> symbols.
- 3). Create an array of string named "cars." Then fill this string with a few different types of cares. Write the following at the top of the source code file:
String[] cars = {"Lamborghini", "Porsche", "BMW"}; - 4). Loop through the array of cars using a "for" loop which visits each member of an array once, so you can do something special like print out each car name. This requires accessing the array. Accessing an array is done by using the array name followed by the symbols []. The element of the array goes between these symbols. For example, to print out the fifth element of an array, you would write "arrayName[5]." To print out all the elements in the array, write the following statements:
for (int i = 0; i < cars.length; i++) {
out.print("<p>" + cars[i] + "</p>");
} - 5). Save the file as "Array.html." View the file in a web browser.
next post