Results 1 to 4 of 4
  1. #1
    saqib15 is offline Member
    Join Date
    Feb 2010
    Posts
    11
    Rep Power
    0

    Default Checkbox value passing to servlet

    Anyone knows how to pass checkbox values to servlets if they are checked?
    If anyone can give me some idea:
    for refrence i am attaching my code:

    WSRole[] role = getUserRoles(users);
    WSTask[] task = proj.getTasks();
    WSHumanTaskStep[] human = task[0].getHumanSteps();
    WSUser[][] user = getUsersInRole(role, users);
    for(int h =0; h<human.length; h++){
    out.write("<td></td><td bgcolor =#669933>"+human[h].getDisplayString()+"</td>");
    }
    for (int i = 0; i < user.length; i++) {
    out.write("<tr>");
    out.write("<td bgcolor=#cecef6>"+role[i].getName()+"</td><td>");
    for (int j = 0; j < user[i].length; j++) {
    out.write("<FORM method=\"post\" "+
    "action=\"/ws/ws_ext?servlet=GetUsersList&token="+token+"&users= "+users+"\"");
    out.write("<input type=checkbox name=users value="+user[i][j].getName()+"'<checked>");
    out.write(user[i][j].getName());//this data to be with all tabs
    }
    out.write("</td></tr>");
    }
    out.println("<input type=\"submit\" value=\"Next &gt;&gt;\">");
    out.write("</FORM>");

    out.write("</table>");
    } catch (java.io.IOException e) {
    log.error("WRITING SOMETHING");
    }
    return true;
    }

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    HTML check boxes, I think, only should have the checked attribute if they are to be rendered as checked in the page when the page is loading.

    so your logic here should probably be
    Java Code:
    out.write("<input type=\"checkbox\" name=\"users\" value=\""+user[i][j].getName()+"\" />");
    to have the checkbox rendered as not checked
    (by the way, I like to make the HTML correct XHTML, using quoted values and closing tags, which as you can see quickly becomes a mess to do this in a servlet with all the "\"" business . so JSP is really a better medium to do the HTML templating, IMHO

    So to render a checkbox as checked when rendering the HTML from [the servlet], we would need to test if the checkbox should be checked, and render something like "checked" attribute, and for proper XHTML, we should also supply a value for this, e.g.

    checked="checked"

    I believe is the good way to do this.

    so,

    Java Code:
    out.write("<input type=\"checkbox\" name=\"users\" value=\""+user[i][j].getName()+"\"  checked=\"checked\" />");
    When a checkbox is selected it will have each checked value show up on the request parameters, for example name=value1&name=value2 if there are two check boxes of value1 and value2 selected.

    But, annoying enough,when a check box is not selected there is no value on the request parameter when the form is submitted.

  3. #3
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Are you also not allowed to use JSPs? Mixing Java code with HTML was discovered to be a bad idea back in 1999.

  4. #4
    saqib15 is offline Member
    Join Date
    Feb 2010
    Posts
    11
    Rep Power
    0

    Default

    No i am not allow to use JSP pages for that i think i need to use JS when page render, also will need to work out a useful checkbox naming system so that tell which box is which.

Similar Threads

  1. CheckBox
    By boss in forum NetBeans
    Replies: 3
    Last Post: 01-17-2010, 04:51 AM
  2. Jtree with checkbox
    By msankar.ravi in forum AWT / Swing
    Replies: 1
    Last Post: 07-09-2009, 11:25 PM
  3. Checkbox to list
    By msixty in forum New To Java
    Replies: 5
    Last Post: 05-07-2009, 08:10 PM
  4. passing object as value for checkbox values??
    By Pooja Deshpande in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 03-17-2009, 11:55 AM
  5. checkbox
    By Alan in forum AWT / Swing
    Replies: 3
    Last Post: 05-18-2007, 10:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •