Không đổ được dữ liệu sản phẩm vào trang jsp java web?

CuDer

Member
23/4/20
31
0
6
Viet Nam
Chào mọi người hiện em đang học jsp java web và em có làm theo một bài trên mạng về làm cái web bán hàng.Em đã lấy được danh sách sản phẩm trong database rồi nhưng mà đổ lên jsp thì nó lại không hiện danh sách sản phẩm trong database lên ạ.Có thể em code sai ở phần controller nên mong mọi người xem giúp ạ.

Code phần homecontroller

Java:
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(name = "HomeController", urlPatterns = {"/home"})
public class HomeController extends HttpServlet {
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        //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);
        
    }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
    
}
Code phần home.jsp
HTML:
<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>
 

Joe

Thành viên VIP
21/1/13
2,997
1,320
113
Pls. minimum codes so that the readers could reproduce the problems. For example, where is the Dao and what it does ? If you always post such incomplete codes your thread would be put in the folder Bài viết vi phạm
 

Joe

Thành viên VIP
21/1/13
2,997
1,320
113
The method setAttribute(String name, Object o) of ServletRequest API says:
It is warned that when the request is dispatched from the servlet resides in a different web application by RequestDispatcher, the object set by this method may not be correctly retrieved in the caller servlet.
Further the line request.getRequestDispatcher("view/Home.jsp") refers to the relative path that starts with /view/Home.jsp. In your codes the / before view is missing. The Doc. says
The getRequestDispatcher method takes a String argument describing a path within the scope of the ServletContext. This path
must be relative to the root of the ServletContext and begin with a ‘/’, or be empty.
More: read the Servlet Specification (download from HERE)
 
  • Like
Reactions: CuDer

CuDer

Member
23/4/20
31
0
6
Viet Nam
The method setAttribute(String name, Object o) of ServletRequest API says:

Further the line request.getRequestDispatcher("view/Home.jsp") refers to the relative path that starts with /view/Home.jsp. In your codes the / before view is missing. The Doc. says

More: read the Servlet Specification (download from
HERE)
Em có thêm '/' như anh bảo nhưng mà kết quả vẫn không được ạ.web nó vẫn hiển thị lên được nhưng mà phần body ở front end thì nó không đổ dữ liệu lên được ạ.
 

Joe

Thành viên VIP
21/1/13
2,997
1,320
113
Have you made sure that the relative path /view/home.jsp in the accessible PATH? Try to give the absolute path for home.jsp and see what would happen?
 

Joe

Thành viên VIP
21/1/13
2,997
1,320
113
No. It is the entry in the Computer environmental path. For example:
WINDOWS:
Code:
set PATH = C:\WebBanHang\WebContent\View\Home.jsp;%PATH%
UBUNTU
Code:
export PATH= "/WebBanHang...../Home.jsp:$PATH"
 

CuDer

Member
23/4/20
31
0
6
Viet Nam
No. It is the entry in the Computer environmental path. For example:
WINDOWS:
Code:
set PATH = C:\WebBanHang\WebContent\View\Home.jsp;%PATH%
UBUNTU
Code:
export PATH= "/WebBanHang...../Home.jsp:$PATH"
vậy làm như nào để lấy được đường dẫn tuyệt đối của cái file jsp kia ạ?
 

Joe

Thành viên VIP
21/1/13
2,997
1,320
113
The common rule is: where the servlets are started the directory of the jsp is usually subdirectory of the servlets directory. And the Servlet container (e.g. WebLogic or WebSphere, etc.) needs only to look for the relative path and it is in your case /view/home.jsp.

An excerpt from Oracle's JavaServer Pages Reference
To continue this example for an HTML page in this application, the following URL points to the file /home/dir/mybankappdir/dir1/abc.html:

http://host[:port]/mybank/dir1/abc.html

For each servlet environment there is also a default servlet context. For this context, the context path is simply "/", which is mapped to the default servlet context application root.
@CuDer:
I strongly recommend you to start studying the Servlet-JavaServerPage (jsp) before you begin to develop complicated webapp. You could start HERE and especially this CHAPTER then about the conventions and rules of the Servlet container you currently use (WebLogic, WebSphere, Tomcat or whatever).
 
Sửa lần cuối: