0

Good evening, I am trying to display the output of a HashTable inside a HTML <table> instead of using System.out.print. The problem is, I do not know the exact way to do it. I tried several methods but I still do not understand the logic on how to do it. I tried something like this:

Sample Code

public void printHtmlData() {
Hashtable htmlData = new Hashtable();

.....
.....

Enumeration enumeration = htmlData.elements();
    while (enumeration.hasMoreElements()) {
        System.out.println(enumeration.nextElement());
    }

}

The output with System.out.print

[I|desperately, need, a, girl, haru, haru, big, bang, the, best]
[I|123, 456, a, girl, haru, haru, big, bang, the, best]
[I|desperately, need, 789, 000, haru, haru, big, bang, the, best]
[I|desperately, need, a, girl, just, a, sample, output, for, testing]

I tried to do this for the html output in another jsp file

<jsp:useBean ....."/>
Hashtable printHtml = new Hashtable();
          <TABLE width="100%" style="border-width : 2px 0px 0px 0px;border-style : solid solid solid solid;border-color : #C0C0C0 #C0C0C0 #C0C0C0 #C0C0C0;">
            <TR>
              <TD><%
                    printHtml.printHtmlData();
              %></TD>
            </TR>
          </TABLE>

I need some hints, thanks...

Andy
  • 5,900
  • 2
  • 20
  • 29
薛源少
  • 306
  • 1
  • 6
  • 18
  • Sorry I'm a little rusty on scriptlets, but have you tried simply moving the printing code inside the html ? Since you are using scriplets, you can use the `out` object to render some html markup as you are printing the map values you are interested in. – Andy Jul 12 '13 at 04:19
  • 1
    I should say though, putting logic inside the view is not a good thing. You really want to separate your concerns. Consider using full blown JSP and refer to this for a better answer http://stackoverflow.com/questions/1835683/how-to-loop-through-a-hashmap-in-jsp – Andy Jul 12 '13 at 04:20
  • ya, I will look through it... thanks... – 薛源少 Jul 12 '13 at 06:08
  • Stay put. I'll tell you what to do. You look lost but go practice after. – Andy Jul 12 '13 at 06:24

2 Answers2

1

The correct approach would be to have a servlet populate the HttpServletRequest with your model, the HashTable, and forwarding it to your JSP. (Also, consider using a HashMap instead.)

Within a Servlet:

Hashtable nonHtmlData = new Hashtable();

// populate the map; set as request attr
request.setAttribute("model", nonHtmlData);

// forward to JSP
RequestDispatcher view = request.getRequestDispatcher("display.jsp");
view.forward(request, response);

Then within your JSP using EL and JSTL tags:

<table>
    <c:forEach var="list" items="${model}">
        <tr>
            <th>${list.key}</th>
        <c:forEach var="listItem" items="${list.value}">
            <td>${listItem}</td>
        </c:forEach>
        </tr>
    </c:forEach>
</table>
Ravi K Thapliyal
  • 51,095
  • 9
  • 76
  • 89
0

From your comments, I'm thinking that you do not know JSTL yet. Ravi Thapliyal answer is the right way to go about this by separating the business logic and the presentation. Check out Model View Controller for more info. Anyway since you are using scriplets, you could simply move the logic inside the HTML then use the implicit out object to display your content (again, keep in mind that this is not a good approach if you try to mix business logic with presentation). You can use the pseudocode below as a guide. Fix the HTML as needed if you want to format a certain way. And yes, do consider using a HashMap instead if synchronization is not an issue for you. You can start here for starters.

Differences between HashMap and Hashtable?

Pseudocode

<table>
    <tr>
        <% while (enumeration.hasMoreElements()) { %>
           <td><%= enumeration.nextElement() %></td>  
         <%}%>
    </tr>
</table>
Community
  • 1
  • 1
Andy
  • 5,900
  • 2
  • 20
  • 29