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

I'm new at this. I want to pass an array of flag values ​​from one JSP page to another. Data Acquisition Page

<% 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 get checkboxes in JSP on another page. I tried the following code but it does not work properly

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

But its transfer of meaning

 [Ljava.lang.String;@a0a595 

Please help me solve this problem.

+4
source share
4 answers

You can use it as follows. in the shape of

 <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 } 
+6
source
 for(int count=0; count<h.length; count++){ // DO SOME OPERATION on h[count]; } 

Also, just a recommendation, please don’t qns[] variables like qns[] , you can always save it simply by saying selectedItems

+1
source

You get an array, so you need to get the elements using an index, for example:

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

or use a loop to iterate the entire array.

0
source

You can use stringbuilder (), I hope it 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>') } 
0
source

Source: https://habr.com/ru/post/1499430/


All Articles