Question: I have a simple statement in a servlet file that will go through the database and get the result set

```
ResultSet AssignmentList = stmt.executeQuery("SELECT * FROM Assignments WHERE " + projectID + "= Pid;");
request.setAttribute("assignmentList", AssignmentList);

```

As you can see I'm sending the `ResultSet` to a JSP file. And in that JSP file I tried to iterate through it like this.

```
<table class="CSSTableGenerator">
    <thead>
    <tr>
        <th>Aid</th>
        <th>Title</th>
        <th>Task</th>
        <th>ID of User Responsible</th>
    </tr>
    </thead>
    <tbody>

    <c:forEach items="${assignmentList}" var="item">
        <tr>
            <td>${item.Aid}</td>
            <td>${item.Title}</td>
            <td>${item.Task}</td>
            <td> ${item.UserID}</td>            
        </tr>
    </c:forEach>

    </tbody>
</table>

```

but when I do that I get an error. 
Am I suppose to iterating in wrong way, or is it not possible to iterate through a `ResultSet`.

Answer: 
After we read the ResultSet, we need to close the database connection. Because you read the ResultSet in JSP, you can only close the connection in JSP. This, in my opinion, is not a good practice.

After you query the database in the servlet, read the ResultSet, store the result in a List, and then close the database connection. Then, pass this List to JSP in a request attribute. Your JSP retrieve the List from request attribute using EL. You can use JSTL to iterate over this List.
You can try with [SQL Tag Library](http://docs.oracle.com/javaee/5/tutorial/doc/bnald.html) that is designed for accessing databases in a JSP page.

I have already shared an approach for [Getting values from mysql database to show in JSP (JAVA)](https://stackoverflow.com/questions/24120224/getting-values-from-mysql-database-to-show-in-jsp-java/24121273#24121273)) along with sample code that might help you.

**Logic:** Simply fetch the data form the database in Servlet and populate the data in a POJO class and set the final list of all the records as request attribute and finally forward the request to the JSP page.