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.

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
stylistica
  • 43
  • 5

1 Answers1

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.

ovunccetin
  • 8,443
  • 5
  • 42
  • 53