Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
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>
<br>
<br>

Screen1<br>

tab1<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>

tab2<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>



Screen2<br>

tab1<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>

tab2<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">
</body>
</html>

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 ?

like image 645
Deepesh Shetty Avatar asked May 15 '13 09:05

Deepesh Shetty


1 Answers

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++) {
    System.out.println(results[i]); 
}
like image 69
Xaltar Avatar answered Sep 28 '22 23:09

Xaltar