java - Iterate ArrayList in JSP -


i have 2 arraylists in class , want send jsp , iterate elements in arraylist in select tag.

here class:

package accessdata;  import java.util.arraylist;  public class connectingdatabase  {    arraylist<string> food=new arraylist<string>();    food.add("mango");    food.add("apple");    food.add("grapes");     arraylist<string> food_code=new arraylist<string>();    food.add("man");    food.add("app");    food.add("gra"); } 

i want iterate food_code options in select tag , food values in select tag in jsp; jsp is:

<select id="food" name="fooditems">  // don't know how iterate  </select> 

any piece of code highly appreciated. in advance :)

it better use java.util.map store key , values instead of 2 arraylist, like:

map<string, string> foods = new hashmap<string, string>();  // here key stores food codes // , values visible user in drop-down foods.put("man", "mango"); foods.put("app", "apple"); foods.put("gra", "grapes");  // if servlet or action class having access httprequest object httprequest.setattribute("foods", foods); // can retrieve in jsp 

now iterate map in jsp use:

<select id="food" name="fooditems">     <c:foreach items="${foods}" var="food">         <option value="${food.key}">             ${food.value}         </option>     </c:foreach> </select> 

or without jstl:

<select id="food" name="fooditems">  <% map<string, string> foods = (map<string, string>) request.getattribute("foods");  for(entry<string, string> food : foods.entryset()) { %>      <option value="<%=food.getkey()%>">         <%=food.getvalue() %>     </option>  <% } %>  </select> 

to know more iterating jstl here so answer , here tutorial how use jstl in general.


Comments