Tell me, please, what is wrong here. Only a row with column names is displayed, but no data.

class 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> 
  • @GuestNew, If you are given a comprehensive answer, mark it as correct (click on the check mark next to the selected answer). - Vitalina

2 answers 2

Try something like this:

 List<Item> itemList = new ArrayList<Item>(); 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")); itemList.add(item); } request.setAttribute("itemList", itemList); request.getRequestDispatcher("item-list.jsp").forward(request, response); 
      while(rs.next()) { Item item = new Item(); String name=rs.getString("name")); req.setAttribute("name",name); String description=rs.getString("description")); req.setAttribute("description",description); String category=rs.getString("category")); req.setAttribute("category",category); long price=rs.getLong("price")); req.setAttribute("price",price); request.setAttribute("itemList", item); request.getRequestDispatcher("item-list.jsp").forward(request, response); } <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>