2

I am writing a stock application. Related to this question, I want the user to be able to sell the stocks they have previously bought.

I'm getting the previously bought stocks from the db at the servelet in the form of an ArrayList:

ArrayList a= db.getUserStocks(userid);
request.setAttribute("userstocks", a);
System.out.println(a);

This prints to the console:

[{Stock=Asianpaint}, {Stock=Infy}, {Stock=Tatasteel}]

I want the user to be able to select one of the above stocks from, say, a drop down list or an autocomplete search box and get the current value/price for it at the click of a button. How do I accomplish this in a JSP file?

TL;DR: Just being able to print the ArrayList values in a JSP file should be a good enough headstart.

3
  • 4
    Here's the information you want: How to iterate an Arraylist inside a HashMap using jstl? Commented Apr 29, 2013 at 19:06
  • @SotiriosDelimanolis I was about to write the answer and post a link to that question, but you were faster =\ Commented Apr 29, 2013 at 19:07
  • 1
    @LuiggiMendoza I've had a lot of coffee :) Post the answer anyway. Commented Apr 29, 2013 at 19:07

1 Answer 1

1

You can iterate a List using a for loop and create options of a dropdown list. A sample code is as follows:

<select id="stockListDropdown">
<% 
    ArrayList stockList = db.getUserStocks(userid);
    for (Stock s : stockList) {
%>
        <option value="<%=s.getValue()%>"><%=s.getName()%></option>
<%  }  %>
</select>

On click event of a button, you can read the selected value of "stockListDropdown" and process it in your own way.

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

Comments

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.