How to display a list of objects in JSP through Servlet

Hello friends, Today I am going to show you how to display a list of objects using JSP and servlet. In this example, we will display a list of object using JSP pages and Servlet Controller.

In the controller, you have to set the list of object first and redirect the JSP page.

request.setAttribute("posts", posts);

Two ways you can display the data using JSP tag and JSTL tag also.

<table>
  <c:forEach items="${posts}" var="post">
   <tr>
     <td>${post.id}</td>
     ....
   </tr>
  </c:forEach>
</table>

<%
  ArrayList<Post> posts=(ArrayList<Post>) request.getAttribute("posts"); 
  for (Post post: posts) {   
%>
  <tr>
    <td><%=post.id%></td>
    ....
   </tr>
<%}%>

Hope it helps..

Leave a Reply