Article

The JSP Files - Parts 1 to 8: Tagged and Bagged

Page: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 Next

Taking Some Medication

Just as you can access data from text fields, you can also use the
getParameter() method to evaluate the state of radio buttons and list boxes. Suppose you modify the form above to something a little more complex.

<html>                  
                 
<head>                  
<basefont face="Arial">                  
</head>                  
                 
<body>                  
                 
<center>                  
<form method="GET" action="pills.jsp">                  
                 
<font size="-1" face="Arial">                  
Gimme                  
<select name="quantity">                  
<option value="10">10</option>                  
<option value="25">25</option>                  
<option value="50">50</option>                  
<option value="100">100</option>                  
</select>                  
of those little                  
<input type="Radio" name="colour" value="red" checked>red                  
<input type="Radio" name="colour" value="blue">blue                    
pills, willya? </font>                  
                 
<p>                  
                 
<input type="Submit">                  
</form>                  
                 
</center>                  
</body>                  
                 
</html>

And here's "pills.jsp".

<html>                  
<head>                  
<basefont face="Arial">                  
</head>                  
                 
<body>                  
<center>                  
<%                  
// pills.jsp                  
                 
// define the variables and assign values                  
String colour = request.getParameter("colour");                  
String quantity_string = request.getParameter("quantity");                  
                 
// convert string to number                  
int quantity = Integer.parseInt(quantity_string);                  
                 
                 
// process and display                  
                 
// who needs green pills?                  
if (colour.equals("blue"))                  
{                  
out.println("Sorry, we don't carry blue pills here.");                  
}                  
else                  
{                  
 if (quantity >= 50)                  
 {                  
 out.println("Stocking up, are we?");                  
 }                  
 else                  
 {                  
 out.println("Here you go. And would you like fries with that?");                  
 }                  
}                  
%>                  
</center>                  
</body>                  
</html>

Depending on the combination of pill type and quantity, an appropriate message will be displayed.

As you can see, evaluating radio buttons and list boxes is almost exactly the same as evaluating regular text fields.

Before moving on to the other form constructs - namely, checkboxes and multiple-select list boxes - you need to understand a new type of variable: the JSP array.

Copyright Melonfire, 2000. All rights reserved.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links