3

I need to show data from a PL SQL table for an assignment. I got the result as a HashMap and passed it to a JSP page. How can I show these data in a table ? Can I use HTML tags inside JSTL tags like this ?

<c:forEach items="${employees}" var="employee">
  <td>${employee.name}</td>
  <td>${employee.city}</td>
  <td>${employee.salary}</td>
<c:forEach>

Or are thre any other technique to print a table in a JSP page using PL SQL ???

EDIT: This is my code:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
  </head>
  <body>
    <h1>Employee Details</h1>
    <form action="handler" method="post">
        <table>
            <tr>
                <td>Employee ID</td>
                <td>First Name</td>
                <td>Last Name</td>
                <td>Start Date</td>
                <td>End Date</td>
                <td>Salary</td>
                <td>City</td>
                <td>Description</td>
            </tr>
            <c:forEach items="${employees}" var="employee">
            <tr>
                <td>${employee.value.id}</td>
                <td>${employee.value.fName}</td>
                <td>${employee.value.lName}</td>
                <td>${employee.value.startD}</td>
                <td>${employee.value.endD}</td>
                <td>${employee.value.salary}</td>
                <td>${employee.value.city}</td>
                <td>${employee.value.desc}</td>
            </tr>
            </c:forEach>                    
        </table>
    </form>
  </body>
</html>

EDIT 2:

This is how I created my HashMap:

while (rs.next()) {
  employee = new Employee(
    rs.getString("EMPLOYEEID"),
    rs.getString("FIRST_NAME"),
    rs.getString("LAST_NAME"),
    rs.getString("START_DATE"),
    rs.getString("END_DATE"),
    rs.getString("SALARY"),
    rs.getString("CITY"),
    rs.getString("DESCRIPTION")
  );

  System.out.println(employee.toString());
  employees.put(rs.getString("EMPLOYEEID"), employee);
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
ThisaruG
  • 3,222
  • 7
  • 38
  • 60

2 Answers2

1

This is the HaspMap creation in the java method:

Map<String, String> countryList = new HashMap<String, String>();
countryList.put("United States", "Washington DC");
countryList.put("India", "Delhi");
countryList.put("Germany", "Berlin");
countryList.put("France", "Paris");
countryList.put("Italy", "Rome");

The first argument in the HaspMap is the Key and second argument is the value. Now you need to access that in the jsp just with the key and the value as:

<c:forEach var="country" items="${capitalList}">
    Country: ${country.key}  - Capital: ${country.value}
</c:forEach>

Thats it. And in your case too, you can loop in jsp with key and value. Hope this helps.

anishroniyar
  • 96
  • 12
  • I've added the header. But it doesn't show any data. – ThisaruG Nov 03 '15 at 05:26
  • Have you checked the HashMap in the java method from where you are sending it to the jsp? Whether it contain the value or not? – anishroniyar Nov 03 '15 at 05:33
  • I has. I printed it out. It shows the data. Another thing. We can print an object like this right ??? – ThisaruG Nov 03 '15 at 05:36
  • No need to use .value in the HTML tag if you have created the HashMap properly. Can you show how you have created the HashMap in Java? – anishroniyar Nov 03 '15 at 05:41
  • I added code in the edit2 – ThisaruG Nov 03 '15 at 05:54
  • HaspMap is accessed with key and value. I don't think the object you are trying to access in the loop will work. I will show you the example of hash map and jsp in this edited answer. – anishroniyar Nov 03 '15 at 05:59
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/94057/discussion-between-thisaru-guruge-and-anishroniyar). – ThisaruG Nov 03 '15 at 06:03
  • The loop you are using in the jsp best fits if you are using List, where you can create the model class for the object access. – anishroniyar Nov 03 '15 at 06:04
  • We can't talk now. I am in middle of something. But if you want i can provide you with the List example in using it. And dont forget to check the answer if it helps you :) – anishroniyar Nov 03 '15 at 06:11
  • Thank you for the great help. I solved it now. Just overridden the toString Method in Employee class with html tags inside. Then used HashMap – ThisaruG Nov 03 '15 at 06:17
  • Glad you solved it :) – anishroniyar Nov 03 '15 at 07:19
1

Yes you can use HTML tags inside JSTL tags. That is how you can print a table in JSP/HTML.

You probably want each of the employee to be a row. To do that don't forget to wrap your tds inside a tr in c:forEach

Ashraf Purno
  • 1,065
  • 6
  • 11
  • What if I put `` inside a `` tag ??? I am asking this for know more. And thanks for the answer. I'll try that. – ThisaruG Nov 03 '15 at 05:16
  • 1
    Yes you can put `` inside a `` also. You can intermingle them in various ways. You can even nest them (like `c:forEach` then `tr` then again `c:forEach` then `td` etc.) – Ashraf Purno Nov 03 '15 at 05:19
  • But Still I don't get any results in my JSP page. I'm posting the code as an edit. Can you help me here ??? – ThisaruG Nov 03 '15 at 05:24