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

Commit

Permalink
Merge pull request #178 from hardingadonis/nghia-update-paging-for-or…
Browse files Browse the repository at this point in the history
…der-page

Update paging order
  • Loading branch information
hardingadonis authored Mar 10, 2024
2 parents 1807ecf + a6691bc commit b98eb2d
Showing 1 changed file with 27 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -22,20 +21,37 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response)

List<Order> 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<Order> orderPaging = Singleton.orderDAO.pagination(offset, LIMIT);

List<Order> orderPaging = Singleton.orderDAO.pagination(offset, LIMIT);
int total = Singleton.orderDAO.totalPages(LIMIT);

request.setAttribute("numOfOrder", count);
request.setAttribute("currentPage", pageNum);
request.setAttribute("totalPage", total);
Expand All @@ -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;
}
}
}

0 comments on commit b98eb2d

Please sign in to comment.