@Boris_ANTIHYPE

Как вывести информацию из БД в JSP?

Необходимо передать список на jsp страницу в таблицу! В консоль всё выводит, а jsp страница пуста
сервлет
public class CatController extends HttpServlet {

    private Connection connection = null;
    private Util util = new Util();

    private static final String SQL_GET_ALL_CAT = "SELECT * FROM cat";

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        Statement statement = null;
        ResultSet resultSet = null;

        List<Cat> cats = new ArrayList<Cat>();

        try {
            connection = util.getConnection();
            connection.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
            connection.setAutoCommit(false);
            statement = connection.createStatement();

            ResultSet result = statement.executeQuery(SQL_GET_ALL_CAT);

            while (result.next()){
                Cat cat = new Cat();
                cat.setId(result.getInt("id"));
                cat.setName(result.getString("name"));
                cat.setAge(result.getInt("age"));
                cat.setColor(result.getString("color"));
                cat.setGender(result.getString("gender"));
                cat.setBreed(result.getString("breed"));
                cat.setDad_id(result.getInt("dad_id"));
                cat.setMam_id(result.getInt("mam_id"));

                cats.add(cat);
            }

            req.setAttribute("cat", cats);
            req.getRequestDispatcher("index.jsp").forward(req, resp);

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

    }
}

jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

<table>
    <tr>
        <c:forEach var="cats" items="${cat}">
            <td>${cats.id}</td>
            <td>${cats.name}</td>
            <td>${cats.age}</td>
            <td>${cats.color}</td>
            <td>${cats.gender}</td>
            <td>${cats.breed}</td>
            <td>${cats.dad_id}</td>
            <td>${cats.mam_id}</td>
        </c:forEach>
    </tr>
</table>

</body>
</html>
  • Вопрос задан
  • 1962 просмотра
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы