How to use request.getParameterValues?
Solution 1
name attribute is not specified to input tag, if name attribute not specified then no value will be sent.
In your case
request.getParameter
andrequest.getParameterValues
return same value becauseplayers
element is specified only one. When you userequest.getParameter
it'll return directstring
,request.getParameterValues
will returnstring[]
with length1
.
If you want to send multiple players and you don't want to repeat element in your jsp, then concat the players
with some special character like the following:
document.getElementById("players").value=playernames.join("::");
You can get is as string in lineup.jsp
and you can split it with the same special characters like the following:
<%
String players = request.getParameter("players");
String[] s = players.split("::");
%>
Solution 2
String[] players = request.getParametervalues("nameOfTheHiddenField");
Please try specifying a name for the hidden field and it will work.
Dan S.
Updated on July 07, 2022Comments
-
Dan S. almost 2 years
I'm trying to pass an array from one jsp page to another using a hidden form.
Here is the relevant code of my jsp files.
<td style="vertical-align: top;"><button onclick="getPlayerNames()"id="generate">Generate</button><br></td> <form id="playerNames" method="post" action="lineups.jsp"> <input type="hidden" id="players" /> </form> <script> function getPlayerNames(){ var selected = document.querySelectorAll("#selected-players > tr > td"); var playernames = []; for(var i=0; i<selected.length; ++i){ //alert(selected[i].textContent); var num = (i-1)%6; if(num==0){ playernames.push(selected[i].textContent); } } document.getElementById("players").values=playernames; alert(document.getElementById("players").values); document.getElementById("playerNames").submit(); }</script>
The alert message does show that the correct values are being placed in "players"
Then on my lineup.jsp I have:
<%String[] s = request.getParameterValues("players"); System.out.println(s[0]);%>
and I get a null pointer exception on the line with 'System.out.println(s[0]);'
-
Rahul Yadav over 8 yearsCan you just try with
request.getParameter("players")
? -
Dan S. over 8 yearsWhen I do that it says "can not convert from string to String[]" so I switch s to a string and 'System.out.println(s)' prints "null"
-
Rahul Yadav over 8 yearsYou need to assing it to
String s = request.getParameter("players")
-
Dan S. over 8 yearsYes I did that and the outcome was null.
-
Rahul Yadav over 8 yearsYou will need to add name attribute to hidden field and use that in request.getParameter();
-
-
Dan S. over 8 yearsThank you I this helped a lot.