Form Servlet

R

roohbir

Hello,

I have an HTML form which has an drop down box. I want to display the
selected option from this from a Servlet. I am having problems with
this and have a feeling I am not extracting data from the drop-down
box properly. I would be really thankful if somebody can point out
what the error is. The Servlet sends the response but only 0 is
displayed while the user's choice might have been 1 or 2 or 3 or 4.

The drop down box code in html is as follows:
<td>Socks</td>
<td>4</td>
<td><select name="socks" size="1">
<option selected="selected" value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select></td>
</tr>
<tr>
<td>Shirt</td>
<td>16</td>
<td><select name="shirt" size="1">
<option selected="selected" value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select></td>
</tr>

The Servlet code for extraction is here:
// extract data from the drop-down buttons here
int sockQuantity =
Integer.parseInt(request.getParameter("socks"));
int shirtQuantity =
Integer.parseInt(request.getParameter("shirt"));
int trouserQuantity =
Integer.parseInt(request.getParameter("trouser"));
int jumperQuantity =
Integer.parseInt(request.getParameter("jumper"));
int shoeQuantity =
Integer.parseInt(request.getParameter("shoes"));

Thanks in advance.
ros060
 
L

Lew

roohbir said:
<td>Socks</td>
<td>4</td>
<td><select name="socks" size="1">
<option selected="selected" value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select></td>
</tr>
<tr>
<td>Shirt</td>
<td>16</td>
<td><select name="shirt" size="1">
<option selected="selected" value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select></td>
</tr>

Dude, lose the TAB characters. Use spaces for Usenet posts.

-- Lew
 
M

mart

Hello do you not need to use request.getParameterValues() which would
return an array of values, strings.

Something like this, where getParameterValues() returns an array of
Strings.

String[] myShirts = request.getParameterValues("shirts");

then you could loop over the array and parseInt off everything.
for(int i=0;i<myShirts.length;i++){
int shirtQuantity = Integer.parseIn(myShirts;
}

I am still a bit of a newbie at this stuff myself and I did not test
this but something along these
lines should do it.

hope it helps
cheers
Martin

http://java.sun.com/j2ee/sdk_1.3/te...est.html#getParameterValues(java.lang.String)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top