• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

servlet: No return method after rd.forward?

 
manoj rajkumar
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I was pretty puzzled when a newbie asked me this question:
When we forward the request from one servlet to another servlet,
.......
i=0;
rd.forward(req,res);
i=1; //will this statement be executed ???
......
1> will the 3rd line get executed?
2> What exactly happens when we do rd.forward().
(specifically, does the service method exit?)
Please do give your valuable comments on this situation.
[ September 11, 2006: Message edited by: Bear Bibeault ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, servlet methods are like any other Java method.
If you do not explicitly returning and there is no exception thrown than the method will continue.
 
manoj rajkumar
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ben,
Just to conclude, if we do have any other statements after this, we will get an IllegalStateException. Right?
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you try to forward something after you have once already forwarded then you will get illegalstatexception.Make sure that if your first forward gets executed then the control should also leave the method.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by manoj rajkumar:
Thanks Ben,
Just to conclude, if we do have any other statements after this, we will get an IllegalStateException. Right?


If you have any statements that try to write to the output stream it will cause an IllegalStateException.

Otherwise, no, you can put all the code you like after a redirect or forward.
I personally never do. It leads to too much confusion in my opinion.
 
manoj rajkumar
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, I think that pretty much answered my question.
Actually, I was under the impression that forward does something that will return the control back from the method. (a.k.a. return statement.)
and I was wondering how a method called forward could make a return from the parent method ( doget/dopost or any method that called the rd.forward(); ).

But as per our discussion ( and my google search on the topic ), the method statements after it( forward() ) do get executed, but only that it may throw an IllegalStateException, if we try to write to the response.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by manoj rajkumar:
Yeah, I think that pretty much answered my question.
Actually, I was under the impression that forward does something that will return the control back from the method. (a.k.a. return statement.)
and I was wondering how a method called forward could make a return from the parent method ( doget/dopost or any method that called the rd.forward(); ).

But as per our discussion ( and my google search on the topic ), the method statements after it( forward() ) do get executed, but only that it may throw an IllegalStateException, if we try to write to the response.


Sounds like you've got it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic