简体   繁体   中英

ServletRequest.getParameterValues returns only first value

I am assigning an integer converted into string value to a hidden input through a loop where s1 is the loop iterator.

<% int k=0; 
String s1 = null;
while(mar.next()){ 
k++;
s1 = Integer.toString(k);%>
<table>
<tr><td><b>User Name </b> </td> <td><%= mar.getString("UserName")  %></td></tr>
<tr><td><b>Type </b></td><td> <%= mar.getString("Type")  %></td></tr>
<tr><td><b>Complain </b></td> <td><%= mar.getString("Complaint")  %></td></tr>
<tr><td><b>Status </b> </td> <td><%= mar.getString("Status")  %></td></tr>
<tr><td><b>Date </b> </td> <td><%= mar.getString("Date") %></td></tr>
</table> 
<form action = "demo1.jsp" method = "post">
<% out.println(mar.getString("Id")); %>
<input type="radio"  name = "<%= mar.getString("Id") %>"value="pending"checked>Pending
<input type="radio"  name = "<%= mar.getString("Id") %>" value="done">Done
<input type="submit" name = "B" id = "submit" >
<input type = "hidden" name = "try" value = <%= s1 %> >
<% out.println(s1); %>
</form>

<br>
<br>
<%
 }

Below is the code which should return a string of values from 1 to 3 (values of s1)

String[] vals;
vals = new String[3];
out.println(vals.length);
vals = request.getParameterValues("try");

out.println(vals.length);

for(int i=0; i<vals.length; i++){
    out.println("Hy");
    out.println(vals[i]);
} 

It is only returning one value which is the first one and printing vals.length prints 1.

You are creating three forms. For each form there will be separate request made. Since each form contains only one hidden input you will always get only one value for each request for below code

 request.getParameterValues("try");

Now if you have to submit all the forms with one click. here or here are the answers

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