More About the Request Class

 

Introduction.  In the last webpage we made use of the getParameter( ) method in the request class to retrieve information from a form with basically no discussion.  Now we want give a bit more background on the Request class in JSPs.

The following table surveys three major methods.

 

Method Description
getParameter(String param) Returns the value of the specified param as a string if it exists.  It returns null if it doesn't exist.  This is often the value specified in the value attribute of an HTML form control.
getParameterValues(String param) This returns an array of String objects containing all of the values that the given request parameter has.  It returns null for those that don't have values.

These are particularly useful when working with controls that can have multiple selections and you want to get all the selections the user made in one array.

getParameterNames( ) This returns an Enumeration object that contains the names of all the parameters accessible by the request.  If no such objects exist it returns null.

 

Now we will illustrate these further on a form that has only radio buttons and a select list box that allows for multiple selections.

First you need to develop the HTML form page get_parameter_form.html.  This page has an example of at least one of each of the controls listed above.

 

<html>
<head>
<title>Form to Demonstrate getParameter</title>
</head>

<body bgcolor = "003344" text="cccccc">
<form action="handle_get_parameter_form.jsp" method=post>
<table>
<tr>
<td><font size = 4 color=cccccc>Credit Cards You Use:</font>
<br><font size = 3 color=aaaaaa><b>Select all that apply</b></font>
</td>
<td><select name="sel_credit_card" size=4 multiple>
<option value="Amex">American Express
<option value="DinersClub">Diner's Club
<option value="Discover">Discover
<option value="MasterCard">Mastercard
<option value="Visa">Visa
</select>
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td></td>
</tr>
<tr>
<td><font size = 4 color=cccccc>Education:</font>
</td>
<td><input type=radio name="rb_education" value="NoCollege">
<font size = 4 color=cccccc>No College</font><br>
<input type=radio name="rb_education" value="College">
<font size = 4 color=cccccc>College</font><br>
<input type=radio name="rb_education" value="Masters">
<font size = 4 color=cccccc>Masters</font><br>
<input type=radio name="rb_education" value="PhD">
<font size = 4 color=cccccc>Ph.D.</font>
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td></td>
</tr>
<tr>
<td colspan = 2 align = center><input type = submit name="submit" value="submit">
</td>
</tr>
</table>
</form>
</body>
</html>

 

Notice how we have put in more options for the credit cards and allowed for multiple selections.  Remember, multiple selections are invoked by holding down the Ctrl key as one selects other items.

After uploading and running this in your account on the server you should see something like the following when you go to the page.

 

 

Notice how I have selected more than one credit card.

After entering information you want to process it.  This will be done with the following script which you should call handle_get_parameter_form.jsp.  Notice how this is the name of the action in the <form> tag of the preceding page.

 

<html>
<head>
<title>Displaying the Inputted Information</title>
</head>

<body bgcolor = "003344" text="cccccc">
<%
// obtaining the information from the form and displaying it
// defining local variables
String credit_cards[] = request.getParameterValues("sel_credit_card");
String education = request.getParameter("rb_education");
String education_array[] = request.getParameterValues("rb_education");
%>
<!-- Some HTML with embedded JSP to display the results -->
You claim you have <%= credit_cards.length %> credit cards.<br>
The credit cards you claim you have are <br>
<% for(int i=0; i < credit_cards.length; i++)
{
%>

You have <b><%= credit_cards[i] %></b> <br>

<%
}
%>
<P>
The number of elements in the education array is <%= education_array.length %> <br>
Your education level is <b><%= education %> </b><BR>
<P>Displaying the array from getParameterValues( ) gives <br>
<% for(int j=0; j < education_array.length; j++)
{
%>

Your education level is <b><%= education_array[j] %></b> <br>

<%
}
%>
</P>
</body>
</html>

 

This uses Java for loops and some of the built in features of arrays to display all the entries.

 

 

Notice how the null values do not cause any sort of display nor are they entered in the arrays.