aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes response.sendRedirect() behaviour Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "response.sendRedirect() behaviour" Watch "response.sendRedirect() behaviour" New topic
Author

response.sendRedirect() behaviour

nikita sri
Greenhorn

Joined: Dec 29, 2000
Posts: 11
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

Joined: May 30, 2002
Posts: 1400
Try printing the output of response.isCommitted()
just before calling sendRedirect()


- Varun
nikita sri
Greenhorn

Joined: Dec 29, 2000
Posts: 11
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

Joined: Apr 05, 2003
Posts: 163
Try
<% out.flush();
if(res.isCommitted()){
System.out.println("Yes response is committed!!!");
}
res.sendRedirect("------------");
%>
[ June 25, 2003: Message edited by: Amer Khan ]

<i>Dare to dream - everything that exists today,was once a figment of someone's imagination, nobody says tomorrow can't be a figment of your today.</i>
Panagiotis Varlagas
Ranch Hand

Joined: Nov 27, 2000
Posts: 233
To begin with: Which application server do you use and what version of the JSP spec does it implement?
nikita sri
Greenhorn

Joined: Dec 29, 2000
Posts: 11
I am using Tomcat4.0.1 which uses Servlet2.3 and JSP1.2 specs.
deepak kukreja
Ranch Hand

Joined: Aug 13, 2001
Posts: 54
hi nikita sri
what was the result of ur question...?


Deepak<br />SCJP
nikita sri
Greenhorn

Joined: Dec 29, 2000
Posts: 11
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

Joined: Feb 09, 2002
Posts: 174
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

Joined: Apr 05, 2003
Posts: 163
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

Joined: Apr 05, 2003
Posts: 163

[ July 31, 2003: Message edited by: Amer Khan ]
Yuan Ye
Ranch Hand

Joined: Mar 05, 2003
Posts: 172
I think Tomcat just ignore the exception.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: response.sendRedirect() behaviour