-1

I am new to Servlets/JSP. I have built a Web application where I query db based on user input and store the resultset as session attribute. Now when I try to access the result set using JSTL tag for each its giving error. I have used Derby as my db. following is the code snippet.

<c:forEach var="row" items="${sessionScope.borrow_list.rows}">
        <li>Name : <c:out value="${row.book}" /></li>
        <li>Author : <c:out value="${row.author}" /></li>
</c:forEach>

and this is the exception.

JspPropertyNotFoundException: /member_home.jsp(20,2) '${sessionScope.borrow_list.rows}' Property 'rows' not found on type org.apache.derby.client.net.NetResultSet42

However I did see an example where the above code was used. Am I doing something wrong or my db(ResultSet) doesn't support this functionality.

Razib
  • 10,965
  • 11
  • 53
  • 80
  • Don't send result set to you jsp. Create a bean class, i.e. book, loop through the resultset and assign value to the bean class and return the bean class to JSP instead. – Sas Mar 03 '15 at 19:49

2 Answers2

0

By the way from your code and error message you have given it seems - you have put borrow_list.rows in session from some where of your code. But the borrow_list doesn't contain any property called rows.

Hope it will help.
Thanks a lot.

Razib
  • 10,965
  • 11
  • 53
  • 80
0

After the JDBC statement is executed, one has a ResultSet. That result set should be processed and then both ResultSet and Statement be closed.

Something like this:

public static class Tb {
    String a;
    int b;
    BigDecimal c;
}

List<Tb> borrowList = new ArrayList<>();

String sql = "SELECT a, b, c FROM tb WHERE a LIKE ? ORDER BY a"
try (PreparedStatement stmt = connection.prepareStatement(sql)) {
    stmt.setString(1, searchKey + "%"); // First ?.
    try (ResultSet rs = stmt.executeQuery()) {
         while (rs.next()) {
             Tb record = new Tb();
             record.a = rs.getString("a");
             record.b = rs.getInt("b");
             record.c = rs.getBigDecimal("c");
             borrowList.add(record);
         }
    } // Closes rs.
} // Closes stmt.

request.setAttribute("borrow_list", borrowList);
getServletContext().getRequestDispatcher("/view.jsp").forward();

In the JSP you can then work normally.

It might be that in your found example a RowSet was used. Whereas ResultSet is something like an iterator, JdbcRowSet is like a list, heavier on the data.

Attributes can be helt in miscellaneous scopes: a request scope is just for this single request.

Make the class Tb static when defined as inner class of say the servlet class. This prevents there being a ServletClass.this being in Tb.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138