The JSP Files (part 4): The Red Pill - Taking Some Medication
(Page 5 of 9 )
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 valuesString colour = request.getParameter("colour");String quantity_string = request.getParameter("quantity");// convert string to numberint 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.
Next: What's For Dessert? >>
More Java Articles
More By Vikram Vaswani, (c) Melonfire