简体   繁体   中英

getting a check box value

I am sending a request to the servlet it returning some data from db am constructing table and check box with the resulted data in, servlet itself using out.println and now i need to do select the data for further manipulation using check box and now i dono how to get a value of selected text boxes.

here is my servlet code,

ps=connection.prepareStatement("select t.tc_name,s.scenario_name,t.scenario_id from testcase t, scenario s where t.scenario_id=s.scenario_id;");
        ResultSet rs=ps.executeQuery();
        out.println("<table>");
            /*out.println(executionValues.append("<tr><td>").append("Test Case Name :").append("</td><td>").append("Scenario Name :").append("</td></tr>"));*/    
            while(rs.next()){

            out.println("<li class='panel' value='"+rs.getInt("scenario_id")+"'><b>Scenario Name:</b>"+rs.getString("scenario_name")+"</li><b>Test Case Name:</b>"+rs.getString("tc_name")+"<input type=\"checkbox\" name=\"checkbox\"></li>");

        }

you should remove the ;

your sql query:

("select t.tc_name,s.scenario_name,t.scenario_id from testcase t, scenario s where t.scenario_id=s.scenario_id;");

you should change like:

("select t.tc_name,s.scenario_name,t.scenario_id from testcase t, scenario s where t.scenario_id=s.scenario_id");

You're printing a whole new <html> and <form> around every single checkbox . Your HTML ends up in browser like as:

<html>
    <head></head>
    <body>
        <html><body><form><input type="checkbox"></form></body></html>
        <form><input type="submit"></form>
    </body>
</html>

This is syntactically invalid HTML. You need to rewrite your code so that all checkboxes and the submit button ends up in the same form:

<html>
    <head></head>
    <body>
        <form>
            <input type="checkbox">
            <input type="submit">
        </form>
    </body>
</html>

Then you also don't need those ugly JavaScript workarounds. You just give the checkboxes the same name, but a different value. This way you can just grab the checked values by HttpServletRequest#getParameterValues() .

String[] users = request.getParameterValues("user");

For example:

<form name="input" action="html_form_action" method="get">
<input type="checkbox" name="vehicle" value="Bike">I have a bike<br>
<input type="checkbox" name="vehicle" value="Car">I have a car 
<br><br>
<input type="submit" value="Submit">
</form>

If you check both of checkboxes your server will receive this parameters like so:

http://sitename.com/your_page.jsp?vehicle=Bike&vehicle=Car

After that you can get values like this:

String checkboxValues = request.getParameter("vehicle");

checkboxValues gets all values separated by comma.

Refer this link:

http://theopentutorials.com/examples/java-ee/servlet/getting-checkbox-values-from-html-form-in-servlet/

Dear suganth It is not appropriate now to put html code in servlet. Instead you use jsp pages. but if you have to do in this way then You may provide

  if(conditionMatch){ 
     // code for checked box
     }
     else{ 
          // code for unchecked box
         }
         Hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM