Reputation: 1721
I have an ArrayList
in my Servlet code. Now, I want to show that ArrayList
in a tabular format. How can I achive this?
E.g.
ArrayList dataList = new ArrayList();
// dataList Add Item
out.println("<h1>" + dataList +"</h1>"); // I want to view it in tabular format.
Upvotes: 0
Views: 8956
Reputation: 1108702
Tables are in HTML to be represented by <table>
element. You should be using JSP for HTML code to separate view from the controller (which will greatly increase maintainability). You could use JSTL to control the flow in JSP. You can use JSTL <c:forEach>
tag to iterate over a collection.
In the servlet, put the list in the request scope and forward to a JSP:
request.setAttribute("dataList", dataList);
request.getRequestDispatcher("/WEB-INF/dataList.jsp").forward(request, response);
In the /WEB-INF/dataList.jsp
, you can present it in a HTML <table>
as follows:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
...
<table>
<c:forEach items="${dataList}" var="dataItem">
<tr>
<td>${dataItem.someProperty}</td>
<td>${dataItem.otherProperty}</td>
</tr>
</c:forEach>
</table>
Upvotes: 3
Reputation: 5792
You would have to go with HTML Tables instead of H1 elements, iterate through the list of objects and print their properties
Upvotes: 0
Reputation: 31637
See below how ArrayList
works
out.println("<table>");
ArrayList dataList = new ArrayList();
// add some data in it....
for (Iterator iter = dataList.iterator(); iter.hasNext();) {
out.println("<tr><td>" + (String)(iter.next()) + "</td></tr>");
}
out.println("</table>");
Good Luck!!!
Upvotes: 1