How to create dynamic rows in jsp according to the number of data read from servlet?
Hi all experts...!!
I have devoloped a servlet which reads data from DB & the resuslt is set to a session & send it to the jsp. In jsp the result is put to a table(data grid).
<td width="100" ><%= (ArrayList)session.getAttribute("name") %> </td>
<td width="100" cellpadding="2" cellspacing="5" border="2"><%= (ArrayList)session.getAttribute("age") %></td>
My problem is all the data is included in to one row. I need to get all the data row by row. (if 5 names are in the DB , 05 rows should be created.)
Tell me where I have done the mistake...
plz help me on this.....
Re: dynamic table creation
Hello jam123 and welcome to the Java Programming Forums.
I am unfamiliar with JSP but would it be possible to put this code in some kind of 'for' loop?
If you make it loop as many times as there are entries then this would solve your problem.
Re: dynamic table creation
Yes just like JavaPF says you should loop through the information in your arraylist and print it line by line.
Code :
<%
final List<String> names = (ArrayList<String>) session.getAttribute("names");
%>
<table>
<%
for(final String name : names)
{
%>
<tr>
<td><%=name %></td>
</tr>
<%
}
%>
</table>
Something like that or you can use JSTL to do this with tags which might be preferable. I only print out a list of strings in that example but you get the point, you could have a list of objects and print out the selected data from the object using that loop.
// Json