Skip to content Skip to sidebar Skip to footer

How To Display The Html Table With All The Rows Returned From The Database In Jsp?

I have below html and java code to display one row in html table.

Solution 1:

Instead of sending one object from your server code send list of object which you want to display as list of rows.

I have not tested, you please take care of handling null check.

<table id="myTable" border="0" cellspacing="0" style="border-spacing:0; width:100%;border-collapse: collapse;">
            <%
                List<Object> object = (List<Object>)request.getAttribute("myContact");
        for(int i=0;i<object.size();i++){
                MyModel myModel = (MyModel)object.get(i);
                String mail = myModel.getmail()!=null ? myModel.getmail().toString().trim() : "";
                String title = myModel.gettitle()!=null ? myModel.gettitle().toString().trim() : "";
                String name = myModel.getname()!=null ? myModel.getname().toString().trim() : "";               
            %>


            <tr>
            <td class="table-border-bottom"><label for="name">Name:</label></td>
            <td class="table-border-bottom"><input id="name" type="text" value='<%=name%>' name="name" class="required" style="height: 17px;"/>
            </td>
            <td class="table-border-bottom"><label for="contactTitle">Title:</label></td>
            <td class="table-border-bottom"> <input id="title" type="text" value='<%=title%>' name="title" class="required" style="height: 17px;"/>

            </td>
            <td class="table-border-bottom"><label for="mail">Email:</label></td>
            <td class="table-border-bottom"><input id="mail" type="text" value='<%=mail%>' name="mail" class="required email" style="height: 17px; "/>

            </td>
            </tr>

    <% } %>

            <tr align="center">
            <td valign="bottom" colspan="6" style="height: 45px; ">
            <input type="button" id="submit" name="submit" value="Save" style="width: 80px ; height:24px; text-align: center;border-radius: 10px 10px 10px 10px;"/> 
            <input type="button" id="revert" name="revert" value="Revert" style="width: 80px ; height:24px;text-align: center;border-radius: 10px 10px 10px 10px;"/></td>
            </tr>

      </table>   

Also, it would be helpful to you by going with JSTL instead of doing with scriptlet.

For styling of rows, apply class like

CSS

.rowClass{
  /* APPLY STYLE TO ROWS */
}

Solution 2:

If your MyModel class has bean style getters like :

public String getMail() {
   return this.mail;
}

You should use EL like ${myContact.mail} to retrieve the value of mail attribute.

It is even better to use JSTL's <c:out value="${myContact.mail}"> tag to avoid cross-site scripting.


In case you want to display a List<MyModel> , set it in the request scope in the Servlet which forwards the request to your JSP .

request.setAttribute("myModelsList",myModelsListObject);

Then use , JSTL's <forEach> loop to iterate over each element of the List and display it .

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
 ...
<table>
   <c:forEach items="${myModelsList}" var="myModel" varStatus="count">
    <tr id="${count.index}">
        <td>${myModel.mail}</td>
        <td>${myModel.title}</td>
        <td>${myModel.name}</td>
    </tr>
  </c:forEach>
</table>

Read also :

  1. How to avoid Java Code in JSP-Files?.
  2. Use JSTL forEach loop's varStatus as an ID
  3. How to alternate HTML table row colors using JSP?

Solution 3:

Instead, of one single Contact object you would now set a List<Contact> instead as a request attribute.

List<Contact> myContacts = (List<Contact>) request.getAttribute("myContacts");

Then just use an Iterator with a while loop.

<table id="myTable" border="0" cellspacing="0" style="border-spacing:0; width:100%;border-collapse: collapse;">
<%
    List<Contact> myContacts = (List<Contact>) request.getAttribute("myContacts");

    Iterator<Contact> contacts = myContacts.iterator();
    while (contacts.hasNext()) {
      Contact myModel = contacts.next();

      String mail = myModel.getmail()!=null ? myModel.getmail().toString().trim() : "";
      String title = myModel.gettitle()!=null ? myModel.gettitle().toString().trim() : "";
      String name = myModel.getname()!=null ? myModel.getname().toString().trim() : "";               
%>

<tr>
<td class="table-border-bottom"><label for="name">Name:</label></td>
<td class="table-border-bottom"><input id="name" type="text" value='<%=name%>' name="name" class="required" style="height: 17px;"/>
</td>
<td class="table-border-bottom"><label for="contactTitle">Title:</label></td>
<td class="table-border-bottom"> <input id="title" type="text" value='<%=title%>' name="title" class="required" style="height: 17px;"/>

</td>
<td class="table-border-bottom"><label for="mail">Email:</label></td>
<td class="table-border-bottom"><input id="mail" type="text" value='<%=mail%>' name="mail" class="required email" style="height: 17px; "/>

</td>
</tr>

<% } // close loop
 %>

<tr align="center">
<td valign="bottom" colspan="6" style="height: 45px; ">
<input type="button" id="submit" name="submit" value="Save" style="width: 80px ; height:24px; text-align: center;border-radius: 10px 10px 10px 10px;"/> 
<input type="button" id="revert" name="revert" value="Revert" style="width: 80px ; height:24px;text-align: center;border-radius: 10px 10px 10px 10px;"/></td>
</tr>

</table>

Post a Comment for "How To Display The Html Table With All The Rows Returned From The Database In Jsp?"