-1

Hello i am learning java servlet and i am coding a web ecommerce and i want to load data product form databse to jsp file i got some issues.My list product can't show on the jsp file.Pls help me

this is my class HomeController

package com.WebBanHang.controller;

import java.io.IOException;
import java.util.ArrayList;
import com.WebBanHang.model.Product;
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 com.WebBanHang.dao.Dao;

@WebServlet("/home")
public class HomeController extends HttpServlet {
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
         //b1: get data from dao
        Dao dao = new Dao();
        ArrayList<Product> list = dao.getAllProduct();
      
        //b2: set data to jsp
        request.setAttribute("listP", list);
        request.getRequestDispatcher("/view/Home.jsp").forward(request, response);
        
    }
    
}

this is my class Dao to get product from database

   package com.WebBanHang.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import com.WebBanHang.model.Product;

public class Dao {
    public Dao() {
        super();
        // TODO Auto-generated constructor stub
    }

    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    
    public ArrayList<Product> getAllProduct(){
        ArrayList<Product> list = new ArrayList<>();
        String query = "select * from tblProduct";
        try {
            conn = new DbConnection().getConnection();
            ps = conn.prepareStatement(query);
            rs = ps.executeQuery();
            while(rs.next()) {
                list.add(new Product(rs.getString(1), 
                        rs.getString(2),
                        rs.getDouble(3), 
                        rs.getString(4), 
                        rs.getString(5)));
            }
        } catch (Exception e) {
            // TODO: handle exception
        }
        return list;
    }
    
}

and this is my jsp file

<c:forEach items="${listP}" var="o">
        <div class="row wow fadeIn">
          <!--Fourth column-->
          <div class="col-lg-3 col-md-6 mb-4">

            <!--Card-->
            <div class="card">

              <!--Card image-->
              <div class="view overlay">
                <img src="${o.image}" class="card-img-top" alt="">
                <a>
                  <div class="mask rgba-white-slight"></div>
                </a>
              </div>
              <!--Card image-->

              <!--Card content-->
              <div class="card-body text-center">
                <!--Category & Title-->
                <a href="" class="grey-text">
                  <h5>${o.name}</h5>
                </a>
                <h5>
                  <strong>
                    <a href="" class="dark-grey-text">${o.description}</a>
                  </strong>
                </h5>

                <h4 class="font-weight-bold blue-text">
                  <strong>${o.price}</strong>
                </h4>

              </div>
              <!--Card content-->

            </div>
            <!--Card-->

          </div>
          <!--Fourth column-->

        </div>
        <!--Grid row-->
    </c:forEach>

When i run class HomeController my jsp file not show my list product

Help me pls.Thank you

CuDer
  • 1
  • 1

1 Answers1

-1

when reading requests attributes you need to use the object requestScope.

Example:

<c:forEach items="${requestScope.listP}" var="o">
Dharman
  • 30,962
  • 25
  • 85
  • 135