Form Servlet

Discussion in 'Java' started by roohbir, Mar 9, 2007.

  1. roohbir

    roohbir Guest

    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
     
    roohbir, Mar 9, 2007
    #1
    1. Advertising

  2. roohbir

    Lew Guest

    roohbir wrote:
    > <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
     
    Lew, Mar 10, 2007
    #2
    1. Advertising

  3. roohbir

    mart Guest

    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...uest.html#getParameterValues(java.lang.String)

    On Mar 9, 11:57 pm, "roohbir" <> wrote:
    > 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
     
    mart, Mar 12, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andy Fish
    Replies:
    4
    Views:
    2,423
    Andy Fish
    Dec 17, 2003
  2. Sean Clarke
    Replies:
    1
    Views:
    1,900
    Sudsy
    Jan 7, 2004
  3. circuit_breaker
    Replies:
    2
    Views:
    2,018
    Jack Jia
    Apr 4, 2004
  4. chamikara
    Replies:
    1
    Views:
    49,875
  5. DiscoStu
    Replies:
    0
    Views:
    506
    DiscoStu
    Jul 8, 2004
Loading...

Share This Page