Java Beginner
Java Beginner

Reputation: 1655

How to display data from collections in JSP

I have a Java Bean as Below

  class Account 
  {
     private long id;
     private String userName;
     private String userId;

     //Getters, Setters for Above 
  }

   public List<Account> getAccountList()
   {    
      List<Account> accountList = new ArrayList();

      Account account;

      .
      .
      .
      //db code for fetching data's from database

      while(rs.next()) 
      {       
         account = new Account();

         account.setId(rs.getLong("Id"));
         account.setUserName(rs.getString("UserName"));
         account.setUserId(rs.getString("UserId"));

         accountList.add(account);
      }

      return accountList ;
  }

I am Assigning the List which I got in the function in a servlet and I am forwarding it to JSP page where I will display List of Users in Account.

List<Account> accountList = new ArrayList<Account>();
accountList = objdbUtil.getAccountList();
request.setAttribute("arrUsersList", accountList);          
RequestDispatcher rdst =  request.getRequestDispatcher("UserList.jsp");
rdst.forward(request, response);

Now how to display the Data's in JSP as the List contains collections in it.

Can I directly use below code

<c:forEach var="arrUsersList" items="${requestScope.arrUsersList}">
</c:forEach>

Upvotes: 0

Views: 7198

Answers (1)

NickJ
NickJ

Reputation: 9559

You are on the right lines. The forEach tag will iterate through your list of Account objects.

<c:forEach var="account" items="${requestScope.arrUsersList}">
    <c:out value="${account.userName}" />
    ...
</c:forEach>

Upvotes: 1

Related Questions