Salini L
Salini L

Reputation: 879

How to pass an array of checkbox value From one JSP page to another

I am a beginner. I want to pass an array of check box values from one JSP page to another. The page getting data is

<%
     ResultSet rs=s.notapprovedqns();
 %>
 <%                
     while(rs.next())
     { %>
      <tr><td><input name="qns[]" type="checkbox" value="<% out.println(rs.getInt("question_id")); %>" /></td><td><center><%=rs.getString("question_id") %></center></td><td><%=rs.getString("question") %></td></td></tr>
     <% 
        }
      %>

How can i receive check box values in JSP another page. I have tried the following code but its not working properly

String[] h=null;
h=request.getParameterValues("qns[]");

But its passing the value

[Ljava.lang.String;@a0a595 

Please somebody help me to solve this problem.

Upvotes: 4

Views: 20235

Answers (4)

Mazhar
Mazhar

Reputation: 306

You Can use it as follows. in Form

<form method="post" action="process.jsp">
    <input type="checkbox" name="list" value="value1">
    <input type="checkbox" name="list" value="value2">
    <input type="checkbox" name="list" value="value3">
</form>

In process.jsp

    String[] ids=request.getParameterValues("list");
    // this will get array of values of all checked checkboxes
    for(String id:ids){
     // do something with id, this is checkbox value
    }

Upvotes: 6

AhmadDani
AhmadDani

Reputation: 251

You may use stringbuilder(), i hope it's works:

 ResultSet rs=s.notapprovedqns();
 StringBuilder lstquestion = new StringBuilder();
 while(rs.next()) {
    String question_id = rs.getString("question_id");
    String question = rs.getString("question");
    lstquestion.append('<tr><td><input name="qns[]" type="checkbox" value='+question_id+' /></td><td><center>'+question_id+'</center></td><td>'+question+'</td></td></tr>')

 }

Upvotes: 0

JNL
JNL

Reputation: 4703

for(int count=0; count<h.length; count++){
    // DO SOME OPERATION on h[count];
}

Also, just a recommendation, please do not name the variables as qns[], you can always keep it simple by saying selectedItems

Upvotes: 1

Juned Ahsan
Juned Ahsan

Reputation: 68715

You are getting an array so you need to get the elements using index, such as:

h=request.getParameterValues("qns[]");
String item = h[0]

or use a loop to iterate entire array.

Upvotes: 0

Related Questions