Servlet question - how to collect checked items of a checkbox list ?

Discussion in 'Java' started by Daku, Feb 25, 2010.

  1. Daku

    Daku Guest

    Could some Java guru please help ? I have a standard Servlet class, in
    the 'doPost' method of
    which I generate a Web page, which dynamically
    generates a list of checkboxes while displaying the results of a
    database query. The form in the dynamically generated Web page simply
    does a HTTP 'POST' to the same servlet. So, if I select a set of items
    in the check box list, and then do a POST, how do I recover the
    checked items ?
    Any hints, suggestions would be of immense help. Thanks in advance.
     
    Daku, Feb 25, 2010
    #1
    1. Advertising

  2. Daku

    markspace Guest

    Re: Servlet question - how to collect checked items of a checkboxlist ?

    Daku wrote:
    > Could some Java guru please help ? I have a standard Servlet class, in
    > the 'doPost' method of
    > which I generate a Web page, which dynamically
    > generates a list of checkboxes while displaying the results of a
    > database query. The form in the dynamically generated Web page simply
    > does a HTTP 'POST' to the same servlet. So, if I select a set of items
    > in the check box list, and then do a POST, how do I recover the
    > checked items ?
    > Any hints, suggestions would be of immense help. Thanks in advance.



    String[] checkBoxes = request.getParamterValues("name");
    for( String s : checkBoxes ) {
    out.println( "<br />checkBox: "+s );
    }


    You might want to look into EL and JSP too. Here's an online free class
    for JEE:

    <http://www.javapassion.com/j2ee/>
     
    markspace, Feb 25, 2010
    #2
    1. Advertising

  3. Daku

    Wojtek Guest

    markspace wrote :
    > Daku wrote:
    >> Could some Java guru please help ? I have a standard Servlet class, in
    >> the 'doPost' method of
    >> which I generate a Web page, which dynamically
    >> generates a list of checkboxes while displaying the results of a
    >> database query. The form in the dynamically generated Web page simply
    >> does a HTTP 'POST' to the same servlet. So, if I select a set of items
    >> in the check box list, and then do a POST, how do I recover the
    >> checked items ?
    >> Any hints, suggestions would be of immense help. Thanks in advance.

    >
    >
    > String[] checkBoxes = request.getParamterValues("name");
    > for( String s : checkBoxes ) {
    > out.println( "<br />checkBox: "+s );
    > }


    The problem with this is the checkboxes which are not checked, as the
    specification allows for a non-checkbox to never be returned.

    [*] One
    [ ] Two
    [*] Three

    If you use the loop above you will see that 2 checkboxes have been
    checked, since the browser returns 1 and 3, but you will not know WHICH
    two.

    Rather than using <.. name="name">, dynamically generate the name:

    for ( int index = 0; index < maxIndex; index++)
    buffer.append("<... name=\"name" + index + "\">);

    and then store the index count in a hidden field:

    buffer.append("<input type=\"hidden\" name=\"max\" value=\"" + maxIndex
    + "\">");

    In the response first read the "max" parameter, then loop that many
    times looking for "name" + index. Any retrieval which is null means a
    checkbox which was not checked.

    Um, this is rough code, probably will cause syntax errors and is merely
    meant as a guide.

    --
    Wojtek :)
     
    Wojtek, Feb 25, 2010
    #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. yurps
    Replies:
    1
    Views:
    6,995
    rajeshthangarasu1983
    Feb 29, 2008
  2. circuit_breaker
    Replies:
    2
    Views:
    2,094
    Jack Jia
    Apr 4, 2004
  3. Replies:
    1
    Views:
    2,357
  4. randy
    Replies:
    13
    Views:
    484
    Tad McClellan
    Nov 1, 2003
  5. Replies:
    0
    Views:
    168
Loading...

Share This Page