Servlet: Cannot forward after response has been committed

前端 未结 4 1757
难免孤独
难免孤独 2020-12-17 06:22

I\'m working on servlet page that renders content based on geo-location, and I want to use both sendRedirect and forward together; e.g

4条回答
  •  南笙
    南笙 (楼主)
    2020-12-17 07:06

    Once you redirect, the servlet you're working on is no longer in control. You need to get the servlet that is the target of the redirect to recognize the correct condition to forward and then call forward there, with similar code:

    RequestDispatcher view = request.getRequestDispatcher(RESOURCES_PAGE);
    view.forward(request, response);
    

    Even if it's the same servlet, it's a new invocation of the servlet's doGet( or other similar method.

提交回复
热议问题