Как правильно вывести данные с помощью сервлета на jsp?

Рейтинг: 1Ответов: 2Опубликовано: 08.03.2015

Подскажите, пожалуйста, что здесь неправильно. Выводится только строка с названиями столбцов, а данные нет.

класс ItemServlet:

package com.store.servlet;

import com.store.util.Item;
import com.store.util.User;
import org.apache.log4j.Logger;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@WebServlet(name = "Item", urlPatterns = { "/Item" })

public class ItemServlet extends HttpServlet {
    static Logger logger = Logger.getLogger(RegisterServlet.class);

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
        protected void doGet(HttpServletRequest request,
                         HttpServletResponse response) throws ServletException, IOException {

            Connection con = (Connection) getServletContext().getAttribute("DBConnection");
            PreparedStatement ps = null;
            ResultSet rs = null;

            try {
                ps = con.prepareStatement("select name, description, category, price from item");

                rs = ps.executeQuery();
                while(rs.next())
                {
                Item item = new Item();
                    item.setName(rs.getString("name"));
                    item.setDescription(rs.getString("description"));
                    item.setCategory(rs.getString("category"));
                    item.setPrice(rs.getLong("price"));

                    request.setAttribute("itemList", item);
                    request.getRequestDispatcher("item-list.jsp").forward(request, response);
                        }

            } catch (SQLException e) {
                e.printStackTrace();
                logger.error("Database connection problem");
                try {
                    throw new ServletException("DB Connection problem.");
                } catch (ServletException e1) {
                    e1.printStackTrace();
                }
            }finally{
                try {
                    rs.close();
                    ps.close();
                } catch (SQLException e) {
                    logger.error("SQLException in closing PreparedStatement or ResultSet");
                }

            }     
    }
}

item-list.jsp:

<%@ page session="false" pageEncoding="UTF-8" %>

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<html>

    <body>

    <%--<c:if test="${!empty itemList}">--%>
        <table class="item-table">
       <tr>
              <th>Name</th>
              <th>Desscription</th>
              <th>Category</th>
              <th>Price</th>
       </tr>

            <c:forEach items="${itemList}" var="item">
              <tr>
                       <td>${item.name}</td>
                       <td>${item.description}</td>
                       <td>${item.category}</td>
                       <td>${item.prce}</td>
                </tr>
            </c:forEach>

        </table>
    <%--</c:if>--%>

    </body>
</html>

Ответы

Ответов пока нет.