1

I am developing a web application. I have a java class which has array of type String. In the same package, I have .jsp page which handles UI. I want to show contents of java String array into jsp's select tag.

<select>
   <option>_____</option>
</select>

How do I populate this select box with Java String array?

2
  • I tried importing that java class into my jsp so that I will be able to access array element. but it is not working Commented Sep 5, 2013 at 4:55
  • @Saurabh can you please give some code what you have tried so that we can understand your problem Commented Sep 5, 2013 at 4:58

5 Answers 5

2

Iterate over the loop with for-each loop.

With Expression Language,It looks like,

   <select name="item">   
       <c:forEach items="${itemsArray}" var="eachItem">   
            <option value="${eachItem}">${eachItem}></option>   
       </c:forEach>   
   </select>  
Sign up to request clarification or add additional context in comments.

Comments

0

you can use <c:foREach> tag .

<select>
   <c:forEach var="option" items="${requestScope.array}">
       <option>${option}</option>
   </c:forEach>
</select>

Here is sample code

3 Comments

Ok.. I am seeing an empty select box. In requestScope.array do I need to make any changes? Or in ${option} should I change anything?
you need to keep the values as request attribute named array from your servlet.
I don't think servlet is interfering here. I have a simple core java class which has String array, the content of which I want to display in jsp select tag.
0

Use JSTL in JSP and the code will be much cleaner:

<html:select property="yourPropName">
<html:options name="yourDataList" />
</html:select>

2 Comments

this DataList is nothing but string array elements which I want to show in select
@Saurabh Deshpande yourDataList is your array string or List<String> from the request or session. It's name of request or session attribute where you put your array.
0

Have you tried with POJO class. and access it from jsp like below.

<select>
<% String[] pojoObj= (String)request.getAttribute("data");
for (String str: pojoObj ){
%>
<option><%=str%></option>
<%}%>
</select>

2 Comments

Sorry, a big NO to scriplets, Recommending scriplets is highly discouraged.
This link may help you to understand why to avoid scriptlets in jsp . thewebplant.com/why-no-scripts-should-be-written-in-jsps-java
-1

read the elements of the array in a loop and then print it inside the loop

<select> 
<%
for(int i=0; i<arr.size(); i++){
<option value="<%= arr[i]%>"><%= arr[i]%></option>
} 
%>
</select>

3 Comments

it says arr cannot be resolve. I already imported class in jsp.
in place of arr u have to write the name of your array object
@anonymous - I did that.. then itself it is saying cannot resolved

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.