7
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
  </head>
  <body>
<%=new Date() %>

<%
ArrayList al = new ArrayList();
al.add("C");
al.add("A");
al.add("E");
al.add("B");
al.add("D");
al.add("F");
%>

    <select>
      <option value="<%=al%>"></option>
    </select>
  </body>
</html>

This is my code i want to add Arraylist in drop down in Jsp I dont know how to Bind arraylist in html obtion or drop down please help me i have tried Much but unable to do this .

MaVRoSCy
  • 17,747
  • 15
  • 82
  • 125
Aman Kumar
  • 323
  • 4
  • 5
  • 11

4 Answers4

12

You have to use JSTL <forEach> to iterate through the elements and add it to the select-option . Probably make the List a scoped attribute . Populate the List object in the servlet, set it in request/session scope and forward the request to this JSP. Remember you can populate the List in the JSP itself and use pageScope to refer it , but that will be bad design in my opinion.

<select>
 <c:forEach var="element" items="${al}">
  <option value="${element}">${element}</option>
 </c:forEach>
</select> 

Here , al is the name of the attribute which stores the List in probably request or session scope.

Use JSTL in project :

  1. Download the JSTL 1.2 jar .

  2. Declare the taglib in JSP file for the JSTL core taglib.

    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    

If you want to use just the scriptlets(which is bad off course) :

<%

List<String> al = new ArrayList<String>();
al.add("C");
..........
...........

%>
<select>
  <%  for(String element: al) { %>
   <option value="<%=element%>"><%=element%></option>
  <% } %>
</select>

The above code will work if you have defined the List as List<String> , or else you need to cast the element to String.

Read How to avoid Java Code in JSP-Files?.

Community
  • 1
  • 1
AllTooSir
  • 48,828
  • 16
  • 130
  • 164
  • please update my code so that i can Put it i have tried am doing first time want to learn – Aman Kumar Jul 26 '13 at 11:02
  • @TheNewIdiot: I wonder if the above `for` statement is better than the `Iterator`. – me_digvijay Jul 26 '13 at 11:12
  • @DigvijayYadav `Iterator` means more code to write in this case :P – AllTooSir Jul 26 '13 at 11:14
  • In ur case same value is not displaying – Aman Kumar Jul 26 '13 at 11:16
  • 1
    @AmanKumar (1)If you want to learn then follow good practices and try to investigate , getting readymade copy-paste code won't help you. (2) If you want the `option` to display some text then keep the text inside `` ! – AllTooSir Jul 26 '13 at 11:18
  • @TheNewIdiot Good answer (although OP just wanted a quick solution, that's why she/he chose my answer), there is something important about using the JSTL and the standard tags that you are missing to mention: you need to include a taglib directive at the beginning of the JSP. If you improve your answer with that directive, you'll have my upvote ;-) – morgano Jul 26 '13 at 11:20
5

EDITED

Try this:

<%

ArrayList al = new ArrayList();
al.add("C");
al.add("A");
al.add("E");
al.add("B");
al.add("D");
al.add("F");

%>

<select>
   <%  for(int i = 0; i < al.size(); i++) {
           String option = (String)al.get(i);
   %>
   <option value="<%= option %>"><%= option %></option>
   <% } %>
</select>
</body>
</html>
morgano
  • 17,210
  • 10
  • 45
  • 56
1

Take a look at the tag in the core JSTL library.

Store the arraylist in pageScope.myList and loop as follows:

<select>
<c:forEach items="${pageScope.myList}" var="item" varStatus="status">
<option value='${item}'></option>
</c:forEach >
</select>

This is preferable than using scriptlets which are not considered good practice

Alex Theedom
  • 1,604
  • 15
  • 16
1

Try this: declare your arraylist in between <%! … %>

<%! ArrayList al = new ArrayList(); %>
Sonam Vyas
  • 11
  • 2