Forums Register Login

response.sendRedirect() behaviour

+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
Try printing the output of response.isCommitted()
just before calling sendRedirect()
+Pie Number of slices to send: Send
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() !
+Pie Number of slices to send: Send
Try
<% out.flush();
if(res.isCommitted()){
System.out.println("Yes response is committed!!!");
}
res.sendRedirect("------------");
%>
[ June 25, 2003: Message edited by: Amer Khan ]
+Pie Number of slices to send: Send
To begin with: Which application server do you use and what version of the JSP spec does it implement?
+Pie Number of slices to send: Send
I am using Tomcat4.0.1 which uses Servlet2.3 and JSP1.2 specs.
+Pie Number of slices to send: Send
hi nikita sri
what was the result of ur question...?
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
hi nikita,
I think with Tomcat it doesn't work to throw the IllegalStateException.
You must try it with another servlet container.
+Pie Number of slices to send: Send
Try this

<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).
+Pie Number of slices to send: Send

[ July 31, 2003: Message edited by: Amer Khan ]
+Pie Number of slices to send: Send
I think Tomcat just ignore the exception.
No holds barred. And no bars holed. Except this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1322 times.
Similar Threads
about sendRedirect
sendRedirect()
sendRedirect method
Thrown error but still redirected
IllegalStateException in JSP
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 11:43:52.