java.lang.IllegalStateException: Cannot (forward | sendRedirect | create session) after response has been committed

后端 未结 8 1062
南笙
南笙 2020-11-21 11:06

This method throws

java.lang.IllegalStateException: Cannot forward after response has been committed

and I am unable to spot th

相关标签:
8条回答
  • 2020-11-21 11:53

    Typically you see this error after you have already done a redirect and then try to output some more data to the output stream. In the cases where I have seen this in the past, it is often one of the filters that is trying to redirect the page, and then still forwards through to the servlet. I cannot see anything immediately wrong with the servlet, so you might want to try having a look at any filters that you have in place as well.

    Edit: Some more help in diagnosing the problem…

    The first step to diagnosing this problem is to ascertain exactly where the exception is being thrown. We are assuming that it is being thrown by the line

    getServletConfig().getServletContext()
                      .getRequestDispatcher("/GroupCopiedUpdt.jsp")
                      .forward(request, response);
    

    But you might find that it is being thrown later in the code, where you are trying to output to the output stream after you have tried to do the forward. If it is coming from the above line, then it means that somewhere before this line you have either:

    1. output data to the output stream, or
    2. done another redirect beforehand.

    Good luck!

    0 讨论(0)
  • 2020-11-21 11:56

    Bump...

    I just had the same error. I noticed that I was invoking super.doPost(request, response); when overriding the doPost() method as well as explicitly invoking the superclass constructor

        public ScheduleServlet() {
            super();
            // TODO Auto-generated constructor stub
        }
    

    As soon as I commented out the super.doPost(request, response); from within doPost() statement it worked perfectly...

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
            //super.doPost(request, response);
            // More code here...
    
    }
    

    Needless to say, I need to re-read on super() best practices :p

    0 讨论(0)
提交回复
热议问题