consider the following jsp code <html> <body> This is the test page <% out.flush();%> testing sendRedirect() method for th response object <% response.sendRedirect("http://localhost:8080/test.htm"); %> </body> </html> I am trying to commit the response before sendRedirect() method so that I will get IllegalStateException. But I am getting the output "This is the test page" on the screen instead of the exception! But the book says "If the response is already been commited, this method throws an IllegalStateException". Can anybody explain this behaviour please?
I inserted the following line before response.sendRedirect() <%= "is committed = "+response.isCommitted() %> Still I get the same output as before. Everything is ignored after out.flush() !
Let me post the code that I tried to execute and the result that I got. <html> <body> This is the test page to test response.sendRedirect <% out.flush();%> <% if(response.isCommitted()){ System.out.println("yes response is committed. now trying to redirect so that it will throw IllegalStateException"); response.sendRedirect("http://localhost:8080/test.htm"); } else { response.sendRedirect("http://localhost:8080/test.htm"); } %> </body> </html> The output I got was: This is the test page to test response.sendRedirect and in the std output(System.out.println) I got "yes response is committed. now trying to redirect so that it will throw IllegalStateException " So that means, response is commited and then while executing the redirect() method, it should throw IllegalStateException right? But I am not gettiing any such exception. Can anybody explain this behaviour?
<html> <body> This is the test page to test response.sendRedirect <% out.flush();%> <% try{ if(response.isCommitted()){ System.out.println("yes response is committed. now trying to redirect so that it will throw IllegalStateException"); }
response.sendRedirect("http://localhost:8080/test.html"); }catch(Exception e){ System.out.println("exception-----> occured"); } %> </body> </html> If u don't catch the exception (check the log folder to see the exception) u can also register an errorpage in web.xml(error-type).