1

I am new to JSP and don't have much idea. So just let me clarify my requirement.

Say I have a program as below, where there's a First.jsp with an array Match_List[5].

String[] Match_List; 
Match_List[] = {a, b, c, d, e};

<form name="Team_Playerdetails" method="post" action="db_Match_Edit.jsp"> 
  <TABLE> 
  <% for (int j = 0; j < 10; j++) { %>  
    <TR>    
      <TD> 
        <SELECT name="Stat_Match_name"> 
        <% for (int i = 0; i < 5; i++) { %> 
          <option> 
            <% out.println(Match_List[i][1]); %> 
          </option> 
        <% } %> 
        </SELECT> 
      </TD> 
    </TR> 
  <% } %> 
  </TABLE> 
  <input type="submit" name="submit" value="Add All" tabindex="10" class="button" /> 
</form> 

Now this form above will display a same drop down lists 10 times. I want to select different options in these ten lists say like below:

List_1 Option: a 
List_2 Option: d 
List_3 Option: e 
List_4 Option: b 
List_5 Option: c 
..... 
List_10 Option: d 

Now I need that once user clicks the submit button then the variable should be moved to db_Match_Edit.jsp. Here I will have the code to catch the value as

U_Stat_Match_name = request.getParameter("Stat_Match_name");

But since all the 10 drop down options are getting caught by the same select variable name Stat_Match_name. So on db_Match_Edit.jsp I am only getting the last selected option in U_Stat_Match_name.

But I need all the 10 selected options in the db_Match_Edit.jsp jsp in an array.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
user1947768
  • 5
  • 2
  • 5

1 Answers1

0

Either just use HttpServletRequest#getParameterValues():

String[] statMatchNames = request.getParameterValues("Stat_Match_name");
// ...

Or just give them each an unique value based on the iteration index:

<select name="Stat_Match_name_<%=j%>"> 

and grab them individually as follows:

for (int j = 0; j < 10; j++) {
    String statMatchName = request.getParameter("Stat_Match_name_" + j);
    // ...
}

Unrelated to the concrete problem, this oldschool JSP code style suggests that you're learning JSP by a heavily outdated resource of possibly more than 10 years old. I recommend to concentrate on more recent and sane learning resources. Start at How to avoid Java code in JSP files? and then read our JSP wiki page.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • That may happen when the parameter name don't match, or when the desired inputs are not enclosed in the form being submitted, or when you're nesting forms in HTML. Hard to tell without seeing an SSCCE. – BalusC Jan 04 '13 at 14:44
  • test1.jsp is now having: Test2.jsp is having: CODE>>String[] statMatchName = new String[5]; for (int j = 0; j < 5; j++) { statMatchName[j]=request.getParameter("Stat_Match_name_"+j); } for(int j=0;j<5;j++) { out.println(statMatchName[j]); } – user1947768 Jan 04 '13 at 14:49
  • This code doesn't match my answer. You have for some reason placed quotes around the index and added another `=` behind. Why are you trying to do it differently without actually understanding what you're doing? Have you even looked if the generated HTML output is correct? (rightclick page in browser and do *View Source*) – BalusC Jan 04 '13 at 14:50
  • sorry Balus, i actually made the change as suggested by you "Stat_Match_name_<%=j%>"> but while posting it to you due to some issue it got added like that. So can you suggest on this – user1947768 Jan 04 '13 at 14:58