diff --git a/src/main/java/io/hardingadonis/saledock/controller/management/order/OrderServlet.java b/src/main/java/io/hardingadonis/saledock/controller/management/order/OrderServlet.java index 30d45de..c5440ba 100644 --- a/src/main/java/io/hardingadonis/saledock/controller/management/order/OrderServlet.java +++ b/src/main/java/io/hardingadonis/saledock/controller/management/order/OrderServlet.java @@ -7,11 +7,10 @@ import jakarta.servlet.http.*; import java.io.*; import java.util.*; -import java.util.stream.*; @WebServlet(name = "OrderServlet", urlPatterns = {"/order"}) public class OrderServlet extends HttpServlet { - + final static int LIMIT = 10; @Override @@ -22,20 +21,37 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) List orders = Singleton.orderDAO.getAll(); int count = Singleton.orderDAO.count(); - - int pageNum = getInteger(request.getParameter("page")); + + String pageNumParam = request.getParameter("page"); + int pageNum = 0; + + if (pageNumParam == null || pageNumParam.isEmpty()) { + pageNum = 1; + } else { + try { + pageNum = Integer.parseInt(pageNumParam); + } catch (NumberFormatException e) { + response.sendRedirect("./error-404"); + return; + } + } + + if (pageNum < 0) { + response.sendRedirect("./error-404"); + return; + } + int offset = pageNum; - - if (offset == -1){ - offset = 0; + + if (offset == 0) { pageNum = 1; } else { - offset = (offset - 1)*LIMIT; + offset = (offset - 1) * LIMIT; } - - List orderPaging = Singleton.orderDAO.pagination(offset, LIMIT); + + List orderPaging = Singleton.orderDAO.pagination(offset, LIMIT); int total = Singleton.orderDAO.totalPages(LIMIT); - + request.setAttribute("numOfOrder", count); request.setAttribute("currentPage", pageNum); request.setAttribute("totalPage", total); @@ -50,15 +66,4 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } - - private static int getInteger(String parameter){ - if (parameter == null) { - return -1; - } - try { - return Integer.parseInt(parameter); - } catch (NumberFormatException e) { - return -1; - } - } }