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.

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

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

Hope it helps..

Leave a Comment

Comments

No comments yet. Why don’t you start the discussion?

    Leave a Reply

    Your email address will not be published. Required fields are marked *