webapp project of maven, after running, it was found that there was a problem with forwarding in Servlet.
index.jsp:
<%
response.sendRedirect(request.getContextPath() + "/bookServlet?method=getBooks");
%>
servlet:
public class BookServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
BookService bookService = new BookService();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String methodName = request.getParameter("method");
try {
Method method = getClass().getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
method.setAccessible(true);
method.invoke(this, request, response);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
protected void getBooks(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String pageNoStr = request.getParameter("pageNo");
String minPriceStr = request.getParameter("minPrice");
String maxPriceStr = request.getParameter("maxPrice");
int pageNo = 1;
int minPrice= 0;
int maxPrice = Integer.MAX_VALUE;
try {
pageNo = Integer.parseInt(pageNoStr);
} catch (NumberFormatException e) {}
try {
minPrice = Integer.parseInt(minPriceStr);
} catch (NumberFormatException e) {}
try {
maxPrice = Integer.parseInt(maxPriceStr);
} catch (NumberFormatException e) {}
CriteriaBook criteriaBook = new CriteriaBook(minPrice, maxPrice, pageNo);
Page<Book> page = bookService.getPage(criteriaBook);
request.setAttribute("bookpage", page);
request.getRequestDispatcher("/WEB-INF/pages/books.jsp").forward(request, response);
System.out.println(request.getContextPath());
}
}
Project directory:
debug request.getRequestDispatcher("/WEB-INF/pages/books.jsp").forward(request, response);
404:
for answers, thank you!