0

I've following code in my servlet

  public class RedirectingExceptionServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        out.println("Hello, response is being committed");
        out.flush();
        response.sendRedirect("redirectedExceptionServlet");
        System.out.println("Tried redirecting after committing response");
    }

}

Here, I'm trying to redirect after the response has been committed.

As per the specification Send Redirect java doc sendRedirect(String) should throw IllegalStateException if you try to call sendRedirect(path) after response is committed

Rather i see a page with message : Hello, response is being committed

Another interesting thing is, i'm unable to see statement "Tried redirecting after committing response" in server console

Can someone pls explain me this behavior?

Env details : Windows 7, Tomcat 7.0.47, J2EE-6

Satyam Koyani
  • 4,236
  • 2
  • 22
  • 48
JavaHopper
  • 5,567
  • 1
  • 19
  • 27
  • what I learnt by experimenting few things is, once the container encounters out.flush(); it returns from there and doesn't execute remaining lines of code or simply ignores them. Please correct if my understanding is wrong. – JavaHopper Dec 19 '13 at 03:43

1 Answers1

0

Once the container encounters out.flush(); it returns from there and doesn't execute remaining lines of code or simply ignores them and this is true only in case there is no redirection before out.flush();

I have tried a lot of combinations. If someone is interested to have a look at these combinations, I can share them

JavaHopper
  • 5,567
  • 1
  • 19
  • 27