Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

response.sendRedirect() behaviour

 
nikita sri
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Varun Khanna
Ranch Hand
Posts: 1400
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try printing the output of response.isCommitted()
just before calling sendRedirect()
 
nikita sri
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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() !
 
Amer Khan
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try
<% out.flush();
if(res.isCommitted()){
System.out.println("Yes response is committed!!!");
}
res.sendRedirect("------------");
%>
[ June 25, 2003: Message edited by: Amer Khan ]
 
Panagiotis Varlagas
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To begin with: Which application server do you use and what version of the JSP spec does it implement?
 
nikita sri
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using Tomcat4.0.1 which uses Servlet2.3 and JSP1.2 specs.
 
deepak kukreja
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi nikita sri
what was the result of ur question...?
 
nikita sri
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Engin Okucu
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi nikita,
I think with Tomcat it doesn't work to throw the IllegalStateException.
You must try it with another servlet container.
 
Amer Khan
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Amer Khan
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

[ July 31, 2003: Message edited by: Amer Khan ]
 
Yuan Ye
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think Tomcat just ignore the exception.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic