How to display a list from database in a JSP page

62 Views Asked by At

Cannot display data from database:

Page Image:

ProductListServlet.java

@WebServlet("/ProductServlet")
public class ProductListServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Good> list = (new DAOGood()).getAllList();
        request.setAttribute("products", list);
        request.getRequestDispatcher("/ProductList.jsp").forward(request, response);
    }

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

DAODood.java

public class DAOGood implements Idao<Good>{
    @Override
    public List<Good> getAllList() {
        List<Good> list = new ArrayList<>();;
        try (Session session = DBConnector.getSessionFactory().openSession()) {
            Query<Good> query = session.createQuery("FROM Good", Good.class);
            list = query.getResultList();
        }
        return list;
    }

ProductList.jsp

<body>
<h1>Product List</h1>
<table style="margin-top: 16px">
    <tr style="color: darkred">
        <th>Nazva</th>
        <th>Articel</th>
        <th>Price</th>
    </tr>
    <c:forEach var="e" items="${products}">
        <tr>
            <td><c:out value="${e.nazva}"/></td>
            <td><c:out value="${e.article()}"/></td>
            <td><c:out value="${e.price()}"/></td>>
        </tr>
    </c:forEach>
</table>
</body>
</html>

I made a test of method getAllList() and everything works.

I tried to make some changes with jstl but...

Database works correctly.

Log

03-Oct-2023 22:49:59.017 INFO [http-nio-8080-exec-5] org.hibernate.engine.transaction.jta.platform.internal.JtaPlatformInitiator.initiateService HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
Hibernate: select u1_0.id,u1_0.login,u1_0.password,u1_0.user_type from user u1_0
03-Oct-2023 22:50:02.492 INFO [Catalina-utility-2] org.apache.catalina.startup.HostConfig.deployDirectory У�тановка веб приложени� в папку [C:\Program Files\Apache Software Foundation\Tomcat10.1\webapps\manager]
03-Oct-2023 22:50:02.538 INFO [Catalina-utility-2] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
03-Oct-2023 22:50:02.548 INFO [Catalina-utility-2] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [C:\Program Files\Apache Software Foundation\Tomcat10.1\webapps\manager] has finished in [55] ms
0

There are 0 best solutions below