How to use request.getParameterValues?

26,639

Solution 1

  1. name attribute is not specified to input tag, if name attribute not specified then no value will be sent.

  2. In your case request.getParameter and request.getParameterValues return same value because players element is specified only one. When you use request.getParameter it'll return direct string, request.getParameterValues will return string[] with length 1.

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.

Share:
26,639
Dan S.
Author by

Dan S.

Updated on July 07, 2022

Comments

  • Dan S.
    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
      Rahul Yadav over 8 years
      Can you just try with request.getParameter("players")?
    • Dan S.
      Dan S. over 8 years
      When 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
      Rahul Yadav over 8 years
      You need to assing it to String s = request.getParameter("players")
    • Dan S.
      Dan S. over 8 years
      Yes I did that and the outcome was null.
    • Rahul Yadav
      Rahul Yadav over 8 years
      You will need to add name attribute to hidden field and use that in request.getParameter();
  • Dan S.
    Dan S. over 8 years
    Thank you I this helped a lot.