2

I have the following code:

<div>
<%
    TaxonomicTypeFeed ttf = new TaxonomicTypeFeed();
    ArrayList<String> tmp = ttf.getTypes();
    System.out.println("Going to print");
        for (int i=0; i < tmp.size(); i++)
    {
        System.out.println(tmp.get(i));
    }
%>
            
    <form>
        <select>
        <%
            Iterator<String> i = tmp.iterator();
            while (i.hasNext())
            {
            String str = i.next(); %>
            <option value="<%str.toString();%>"><%str.toString();%>
            </option>
        <%}%>
    </select>
    </form>
</div>

It creates a dropdown list fine, however there is no text. This is the first time I have ever used any of these options before, so I have no idea if I am even going about it in the right manner.

1 Answer 1

3

You need to print the values by <%= %>. The <% %> won't print anything.

<option value="<%=str%>"><%=str%></option>

Unrelated to the problem: this is not the best practice. Consider using taglibs/EL. You end up with better readable/maintainable code.

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<jsp:useBean id="taxonomicTypeFeed" class="com.example.TaxonomicTypeFeed" />
...
<select>
    <c:forEach items="${taxonomicTypeFeed.types}" var="type">
        <option value="${type}">${type}</option>
    </c:forEach>
</select>

Instead of <jsp:useBean> you can also use a preprocessing servlet.

Sign up to request clarification or add additional context in comments.

1 Comment

I will look into the preprocessing servlet as well, cheers again.

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.