Deepesh Shetty Deepesh Shetty - 6 months ago 6
HTML Question

Find whether check boxes are checked inside a servlet

I have several check box in a form I just wanna way to check whether they are checked or not .

If checked i need to store their id in the database(that i can do it ) . But my question is how to determine whether are checked or not instead of checking for each check box on at a time . I need to check whether its checked or not inside a servlet.

This is my code

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
Role Id<input type="text" name="roll_id"/><br>
Role Name<input type="text" name="roll_name"/><br>
Role Description<textarea name="roll_desc"></textarea><br>



<input type="checkbox" name="s1_t1_view" value="s1_t1_view" >view<br>
<input type="checkbox" name="s1_t1_add" value="s1_t1_add" >add<br>
<input type="checkbox" name="s1_t1_edit" value="s1_t1_edit" >edit<br>
<input type="checkbox" name="s1_t1_delete" value="s1_t1_delete" >delete<br>


<input type="checkbox" name="s1_t2_view" value="s1_t2_view" >view<br>
<input type="checkbox" name="s1_t2_add" value="s1_t2_add" >add<br>
<input type="checkbox" name="s1_t2_edit" value="s1_t2_edit" >edit<br>
<input type="checkbox" name="s1_t2_delete" value="s1_t2_delete" >delete<br>



<input type="checkbox" name="s2_t1_view" value="s2_t1_view" >view<br>
<input type="checkbox" name="s2_t1_add" value="s2_t1_add" >add<br>
<input type="checkbox" name="s2_t1_edit" value="s2_t1_edit" >edit<br>
<input type="checkbox" name="s2_t1_delete" value="s2_t1_delete" >delete<br>


<input type="checkbox" name="s2_t2_view" value="s2_t2_view" >view<br>
<input type="checkbox" name="s2_t2_add" value="s2_t2_add" >add<br>
<input type="checkbox" name="s2_t2_edit" value="s2_t2_edit" >edit<br>
<input type="checkbox" name="s2_t2_delete" value="s2_t2_delete" >delete<br>
<input type="submit" name="sumbit" text="submit">

But in my code I have several check boxes . I need to hardcode that for every check box . Is there way so that i put it in a loop and check for all check boxes ?


To be simple, you can use the name attribute to get the data since you are using different name for each checkbox.

In Servlet :

String s1_t1_view = request.getParameterValues("s1_t1_view" );
String s1_t1_add = request.getParameterValues("s1_t1_add" );

If you want to use group of checkbox to give the user a choice between multiple values, you will need to iterate over the group in the servlet. You can use this :

In HTML : (same name = same group)

<input type = "checkbox" name = "s1_t1" value = "s1_t2_view"   >View  <br>
<input type = "checkbox" name = "s1_t1" value = "s1_t2_add"    >Add   <br>
<input type = "checkbox" name = "s1_t1" value = "s1_t2_edit"   >Edit  <br>
<input type = "checkbox" name = "s1_t1" value = "s1_t2_delete" >Delete<br>

In Servlet :

String[] results = request.getParameterValues("s1_t1");
for (int i = 0; i < results.length; i++) {