Servlet中转发地址出错,是怎么回事?
maven的webapp项目,运行之后发现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());
}
}
项目目录:
debug后发现在request.getRequestDispatcher("/WEB-INF/pages/books.jsp").forward(request, response);
前符合预期结果,进行转发后出现404页面:
求解答,谢谢!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论