aspose file tools*
The moose likes Servlets and the fly likes servlet: No return method after rd.forward? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "servlet: No return method after rd.forward?" Watch "servlet: No return method after rd.forward?" New topic
Author

servlet: No return method after rd.forward?

manoj rajkumar
Greenhorn

Joined: Sep 11, 2006
Posts: 22
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

Joined: Dec 11, 2004
Posts: 13410

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.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

http://faq.javaranch.com/search?IllegalStateException
goes over this
manoj rajkumar
Greenhorn

Joined: Sep 11, 2006
Posts: 22
Thanks Ben,
Just to conclude, if we do have any other statements after this, we will get an IllegalStateException. Right?
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
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.


Rahul Bhattacharjee
LinkedIn - Blog
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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

Joined: Sep 11, 2006
Posts: 22
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

Joined: Dec 11, 2004
Posts: 13410

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: servlet: No return method after rd.forward?