Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

sendRedirect problem  RSS feed

 
Gurpreet Sachdeva
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

I am working with following code....
In this I am getting a problem.According to me when it execute the sendRedirect() and goes on other page it should not print the "After catch" statement but it is printing it. Why is it so?

<%
try
{
int i=1;
int j=0;
int x=i/j;
}
catch(Exception e)
{
Sytem.out.println("Inside catch");
response.sendRedirect("abc.html");
}
System.out.println("After catch");
%>

Thanks
Gurpreet Sachdeva
[This message has been edited by Gurpreet Sachdeva (edited October 24, 2001).]
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sendRedirect() (and dispatcher.forward() and .include() for that matter) do not stop the flow of control in the application, the code still has to finish.
Therefore you need soething like

This is just a sample, I am NOT telling you to have a return inside a catch block, this is a really bad idea!

Dave
 
Kyle Brown
author
Ranch Hand
Posts: 3892
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
He's right -- sendRedirect() doesn't stop program flow -- it simply says that control of the output is no longer the job of this servlet or JSP. You have to write your code so that you are sure you do not do anything after the sendRedirect().
Kyle
------------------
Kyle Brown,
Author of Enterprise Java (tm) Programming with IBM Websphere
See my homepage at http://members.aol.com/kgb1001001 for other WebSphere information.
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There might be a few situations where code after a sendRedirect() is ok. I use that in an application so that the client receives a response in a timely manner. The only reason this is completely fine, is that the result of the code after the sendRedirect() in no way makes a difference to the application. ie: if it throws an error, it is buried and ignored anyways. Also, as a business rule of the app, if the operation does not complete successfully in any other way, the client is not notified. Pretty crazy, but there it is.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mike Curwen:
The only reason this is completely fine, is that the result of the code after the sendRedirect() in no way makes a difference to the application. ie: if it throws an error, it is buried and ignored anyways. Also, as a business rule of the app, if the operation does not complete successfully in any other way, the client is not notified. Pretty crazy, but there it is.

Not for the faint-of-heart though.
I'm reviewing an app at the moment that has bunches and bunches of stuff like this (presentation sent then business logic continues) and it is very unstable in their case.
If the presentation starts (but does not complete) and some more business logic gets going, then the client recieves the partial page then clicks another link, when the original code tries to write more presentation, an Exception is thrown.
This is also complicated in their case since they are using a MVC architecture with a global controller and it maintains client state (and what step they are at) and gets confused very easy.
Just saying: it's possible, but I personally wouldn't do it.
Dave.
($0.02)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!