Skip to content
This repository has been archived by the owner on May 31, 2024. It is now read-only.

Commit

Permalink
optimize code of paging servlet
Browse files Browse the repository at this point in the history
  • Loading branch information
bakaqc committed Mar 1, 2024
1 parent 241e943 commit 11fbe10
Showing 1 changed file with 4 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,33 @@

@WebServlet(name = "ProductServlet", urlPatterns = {"/product"})
public class ProductServlet extends HttpServlet {
final static int LIMIT = 10;

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");

List<Product> products = Singleton.productDAO.getAll();

int page = 1;
int limit = 10;

if (request.getParameter("page") != null) {
try {
page = Integer.parseInt(request.getParameter("page"));

} catch (Exception e) {
response.sendRedirect("./error-404");
return;
}
}

List<Product> list = Singleton.productDAO.pagination((page - 1) * limit, limit);
List<Product> list = Singleton.productDAO.pagination((page - 1) * LIMIT, LIMIT);
Integer count = Singleton.productDAO.count();
int totalPage = Singleton.productDAO.totalPages(limit);

list.isEmpty();
int totalPage = Singleton.productDAO.totalPages(LIMIT);

request.setAttribute("productList", list);
request.setAttribute("currentPage", page);
request.setAttribute("totalPage", totalPage);
request.setAttribute("numOfPro", count);
request.setAttribute("limit", limit);
request.setAttribute("limit", LIMIT);

request.setAttribute("page", "product");

Expand Down

0 comments on commit 11fbe10

Please sign in to comment.