Database Reference
In-Depth Information
<option value= "WI" > Wisconsin </option>
<option value= "WY" > Wyoming </option>
</select>
Radio button sets, pop-up menus, and scrolling lists have several things in common:
A name for the element
When the user submits the form, the browser associates this name with the value
the user selected.
A set of values, one for each item in the list
The internal values available to be selected.
A set of labels, one for each item
The values that the user sees in the displayed form.
An optional default value
Which item in the list is selected initially when the browser displays the list.
To produce a list element for a form using database content, issue a statement that selects
the appropriate values and labels, encode any special characters they contain, and add
the HTML tags that are appropriate for the kind of list you want to display. Should you
desire to indicate a default selection, add a checked or selected attribute to the proper
item in the list.
Let's consider how to produce form elements for the color and state lists first. Both are
produced by fetching a set of column values from a table. Later we'll construct the
figurine size list, which takes its values from a column's definition (that is, its metadata)
rather than its contents.
In JSP, display a set of radio buttons for the colors using JSTL tags as follows. The color
names are used as the values as well as the labels, so they're printed twice:
<sql:query dataSource= "${conn}" var= "rs" >
SELECT color FROM cow_color ORDER BY color
</sql:query>
<c:forEach items= "${rs.rows}" var= "row" >
<input type= "radio" name= "color"
value= "<c:out value=" ${row.color}" /> "
/> <c:out value= "${row.color}" /><br />
</c:forEach>
<c:out> performs HTML entity encoding, so the & character that is present in some of
the color values is converted to &amp; automatically and causes no display problems in
the resulting web page. (For JSTL background, read “JSP, JSTL, and Tomcat Primer” on
the companion website; see the Preface ).
To display a pop-up menu instead, the retrieval statement is the same, but the row-
fetching loop differs:
Search WWH ::




Custom Search