0

I'm trying to create an arraylist of object with Java servlet. Then i need to send the arraylist to the html page. I have the class with all the methods:

public class Internship {
    private String city;
    private int hours;
    private String time;

    public Internship(String city, int hours, String time) {
        this.city = city;
        this.hours = hours;
        this.time = time;
    }

    public String getcity() {
        return city;
    }

    public void setcity(String city) {
        this.city = city;
    }

    public int gethours() {
        return hours;
    }

    public void sethours(int hours) {
        this.hours = hours;
    }

    public String gettime() {
        return time;
    }

    public void settime(String time) {
        this.time = time;
    }
}

i have the arraylist and the map

Map<String, Object> root = new HashMap();

ps = db.prepareStatement("select * from internship where id=?");
ps.setInt(1, Integer.parseInt(userId));
rs = ps.executeQuery();

String city = "";
int hours;
String time = "";
ArrayList<Internship> list = new ArrayList();

while (rs.next()) {
    list.add(new Internship(rs.getString("city"), rs.getInt("hours"), rs.getString("time ")));
}

root.put("list", list);

and in the html side i have

<select id="list1" name="list1">
   <c:forEach items="${list}" var="list1">
    <option value="${list1.key}">
        ${list1.value}
    </option>
   </c:forEach>
</select>

The problem is that in the html part i can see the list just selecting each element, for example:

<h3>Where</h3> <p>${list[1]}</p>

but i would like to iterate the list and show all the elements in the html page.

deHaar
  • 17,687
  • 10
  • 38
  • 51
Rudy
  • 33
  • 4
  • 1
    Possible duplicate of [How to loop through a HashMap in JSP?](https://stackoverflow.com/questions/1835683/how-to-loop-through-a-hashmap-in-jsp) – Jozef Chocholacek Feb 04 '19 at 15:59
  • Can you show us how is `root` passed from controller to JSP and the full JSP content? If `root` is the only object passed to JSP through `request.SetAttribute("root", root)`, you should us `` in order to iterate on the list... – Pietro Martinelli Feb 04 '19 at 16:05
  • @PietroMartinelli yes, root is the only object passed. I'm can see it in html only selecting the index (list[1]) but i can't see it with the iteration, even using – Rudy Feb 05 '19 at 10:19
  • Ok, so there is something wrong with JSTL... do you include something like `<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>` in your JSP? Can you show here how the returned HTML looks like? – Pietro Martinelli Feb 05 '19 at 10:48

0 Answers0