报错:Cannot call sendRedirect() after the response has been committed

Error message : Can not call sendRedirect () after the response has been committed
Problem Description : In the case where the response has been redirected, behind which can have other respons redirection.
Code Display

  if (adduser.getName() == "" || adduser.getName() == null) {

                response.sendRedirect(request.getContextPath() + "/add.jsp");
         
            }
            int num = user.addUser(adduser);
            if (num > 0) {
                response.sendRedirect(request.getContextPath() + "/userListServlet");
         
            }

Solution : plus return later in response to redirect the statement, after performing the first redirect response, the latter statement can not be executed, the problem is solved!
Demo code

  if (adduser.getName() == "" || adduser.getName() == null) {

                response.sendRedirect(request.getContextPath() + "/add.jsp");
              return;
            }
            int num = user.addUser(adduser);
            if (num > 0) {
                response.sendRedirect(request.getContextPath() + "/userListServlet");
               return;
            }
Published 33 original articles · won praise 1 · views 1445

Guess you like

Origin blog.csdn.net/smileKutper/article/details/95754218