10

I have two arraylists in my class and I want to send it to my JSP and then iterate the elements in arraylist in a select tag.

Here is my 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 to iterate food_Code as options in select tag and food as values in Select tag in JSP; my JSP is:

<select id="food" name="fooditems">

// Don't know how to iterate

</select>

Any piece of code is highly appreciated. Thanks in Advance :)

Elber CM
  • 310
  • 5
  • 21
Anish Sharma
  • 149
  • 1
  • 2
  • 10

5 Answers5

18

It would be better to use a java.util.Map to store the key and values instead of two ArrayList, like:

Map<String, String> foods = new HashMap<String, String>();

// here key stores the food codes
// and values are that which will be visible to the user in the drop-down
foods.put("man", "mango");
foods.put("app", "apple");
foods.put("gra", "grapes");

// if this is your servlet or action class having access to HttpRequest object then
httpRequest.setAttribute("foods", foods); // so that you can retrieve in JSP

Now to iterate the map in the 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 about iterating with JSTL here is a good SO answer and here is a good tutorial about how to use JSTL in general.

Community
  • 1
  • 1
Prakash K
  • 11,669
  • 6
  • 51
  • 109
  • can u please give me the link for JSTL Core jar cuz i have included like 10 jars and its still not recognizing it It will be really nice of u Thanks – Anish Sharma May 06 '13 at 11:39
  • Atleast the scriptlet code should work if not `JSTL`? Also did you check if your `Map` contains atleast one element? How are you setting the `foods` map to be accessed in JSP? And here is the link to the zip file which contains the `jstl.jar` & `standard.jar`: http://archive.apache.org/dist/jakarta/taglibs/standard/binaries/jakarta-taglibs-standard-1.0.1.zip – Prakash K May 06 '13 at 13:27
  • yes i have printed my map on console and it contains elements and also how can jsp know about the foods map in the class?? And when i use it without jstl that it cannot recognise the Entry and the foods in the code in my jsp.. – Anish Sharma May 07 '13 at 05:16
  • JSP would know when you make it know, you have to set the map in either the request scope (`request.setAttribute("foods", foods)`) or the session scope (`session.setAttribute("foods", foods)`) in the servlet class or action class whichever has access to `HttpRequest` object. And in JSP if you are using ELs as I have suggested then its fine but if you are using scriptlets then you need to first fetch the object as I have updated my answer. I would suggest please go through a good tutorial or a good book on JSP/Servlets whenever you get time. It is really essential to understand the workings. – Prakash K May 07 '13 at 06:02
  • @Praksh K i am very new to this server side work.still learning its been only a couple of months. do share a link for a good tutorial on jsp/Servlets – Anish Sharma May 07 '13 at 06:08
  • @AnishSharma I already have shared it in the answer :-) and the Stackoverflow [tag:jsp] tag wiki is also a very good place to start and grow. Also if you are interested, the book `Head First JSP & Servlets` is a very good book to gain beginner to intermediate level expertise – Prakash K May 07 '13 at 06:21
  • that book is amazing Thanks a lot.. but i am still facing problem with my question i have sent my map to jsp using requestdispatcher but still the select tag is empty.. PLease a little help will solve it – Anish Sharma May 07 '13 at 06:54
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/29522/discussion-between-prakash-k-and-anish-sharma) – Prakash K May 07 '13 at 06:55
4

You can use JSTL's foreach.

<c:forEach items="${foodItems}" var="item">
   ${item}
</c:forEach>

You need also to import JSTL core:

<%@ taglib prefix="c" 
       uri="http://java.sun.com/jsp/jstl/core" %>
Michal Borek
  • 4,584
  • 2
  • 30
  • 40
4
<c:forEach items="${list}" var="foodItem">
 ${foodItem.propertyOfBean}
</c:forEach>

This will solve your problem.

Madhusudan Joshi
  • 4,438
  • 3
  • 26
  • 42
1

You can retrieve the list in JSP as

<select id="food" name="fooditems">

  <c:forEach items="${listname}" var="food" >

     <option value="${food}"> ${food} </option>

  </c:forEach>

</select>
jwl
  • 10,268
  • 14
  • 53
  • 91
venky
  • 400
  • 1
  • 6
  • 19
0

There are multiple ways this can be done (with some changes in your scheme)

Using JSTL:

  1. Create a bean with two fields as food and food_code

    public class Food{ private String food; private String food_code; // Setter/getters follows }

Now the arraylist available on the page will be list Food objects. In the JSP code, you can use following:

<select name="fooditems">
    <c:forEach items="${list}" var="item">
        <option value="${item.food_code}">${item.food}</option>
    </c:forEach>
</select>

If you are using struts:

<html:select property="fooditems" >
<html:optionsCollection property="list" label="food" value="food_code" />
</html:select>

Here list is object key which will be used to retrieve the list from context (page/session)

Santosh
  • 17,667
  • 4
  • 54
  • 79
  • i have included struts tag library but its not recognizing the tag html:selecct.. – Anish Sharma May 06 '13 at 11:24
  • Have you included the directive at the top (`<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>`) ? – Santosh May 06 '13 at 11:56
  • I assume you included the required jars in the application. Do you see any errors in the log ? How does the generated html source of the jsp look like ? – Santosh May 06 '13 at 13:21