Muhammad
Muhammad

Reputation: 631

jstl - display resultset in jsp

I have followed reply from BalusC to resolve my problem

How do I make a Java ResultSet available in my jsp?

Now, when I run page.jsp, i am getting this error

org.apache.jasper.JasperException: javax.el.PropertyNotFoundException: Property 'name' not readable on type java.lang.String

Please can you advise a solution.

i have followed below as well with no luck.

javax.el.PropertyNotFoundException: using JSTL in JSP

page.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>  
<%@page contentType="text/html" pageEncoding="UTF-8" import="java.util.List"%>  
<!DOCTYPE html>  
<html>  
    <head>  
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
        <title>JSP Page</title>  
    </head>  
    <body>  
    <c:forEach items="${rows}" var="row">
 <c:out value="${row.name}" />
 </c:forEach>
<c:if test="${not empty error}">Error: ${error}</c:if>

    </body>  
</html>  

Controller.java

public class Controller extends HttpServlet {


protected void doGet(HttpServletRequest request, HttpServletResponse response) 

throws ServletException, IOException {

try {
    List<Row> rows = Row.list();
    //request.getSession().setAttribute("rows", rows);
    request.setAttribute("rows", rows);
 } catch (SQLException e) {
    request.setAttribute("error", "Retrieving rows failed.");
    e.printStackTrace();
}

 catch (Exception e) {
    e.printStackTrace();
}
finally {System.out.print("servlet");}
request.getRequestDispatcher("page.jsp").forward(request, response);
}
}

Row.java

import javax.naming.*;
import javax.sql.*;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class Row { 
    private String name;

private String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public static List<Row> list() throws SQLException {
    Connection connection = null;
    Statement statement = null;
    ResultSet resultSet = null;
    List<Row> rows = new ArrayList<Row>();

    try {
        Context initCtx = new InitialContext();
        Context envCtx = (Context) initCtx.lookup("java:comp/env");
        DataSource ds = (DataSource)
          envCtx.lookup("jdbc/TestDB");
         connection = ds.getConnection();
        statement = connection.createStatement();
        resultSet = statement.executeQuery("select id, name from account");
        while (resultSet.next()) {
            Row row = new Row();
            row.setName(resultSet.getString("name"));
            rows.add(row);
        }
    } catch(Exception e) {
        e.printStackTrace();
    }

    finally {
        if (resultSet != null) try { resultSet.close(); } 
 catch (SQLException logOrIgnore) {}
        if (statement != null) try { statement.close(); } 
 catch (SQLException logOrIgnore) {}
        if (connection != null) try { connection.close(); } 
 catch (SQLException logOrIgnore) {}
    }

    return rows;
}
}

Many Thanks Muhammad

Upvotes: 1

Views: 4642

Answers (1)

reevesy
reevesy

Reputation: 3472

Your getter is private

private String getName() {
    return name;
}

It is not visible

Change it to public

public String getName() {
    return name;
}

Upvotes: 2

Related Questions