aspose file tools*
The moose likes JSP and the fly likes forward happens before service() finishes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "forward happens before service() finishes" Watch "forward happens before service() finishes" New topic
Author

forward happens before service() finishes

ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
When we forward request from one Servlet/JSP to other, what actually happens???

First forward happens then service() method finishes or the other way???

I guess, service() finishes after forward.

See the below code:



[ May 26, 2006: Message edited by: rathi ji ]
[ May 26, 2006: Message edited by: Bear Bibeault ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

The forward itself will not halt the execution of the service method.
For this reason, you should either branch your code so that the forward happens last or immediately follow your forward with a return statement.

The same is true with redirects.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Originally posted by Ben Souther:
The forward itself will not halt the execution of the service method.
For this reason, you should either branch your code so that the forward happens last or immediately follow your forward with a return statement.

The same is true with redirects.


Sorry Ben. I didn't get.


[ May 28, 2006: Message edited by: rathi ji ]
Manesh Kumar
Ranch Hand

Joined: Mar 21, 2006
Posts: 94
forward() is just a method call. It will not stop execution of service method. If you dont want to do anything after farward you need to use return statement.
[ May 26, 2006: Message edited by: Dilip Kumar Jain ]

Manesh
vidya sagar
Ranch Hand

Joined: Mar 02, 2005
Posts: 580
What is the case when sendRedirect is used instead of forward, for same code given by rathi.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by vidya sagar:
What is the case when sendRedirect is used instead of forward, for same code given by rathi.


Read my earlier post.
Better yet, try it.
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1016
The flow of control in java methods does not suddenly change because you are writing servlets.
So if you call a method, it will do that method, finish it, and then continue on down through the code.

Now dealing with JSP/servlet coding, if the method call sends/commits the response (like a forward/redirect does) then you should make sure your code does not continue writing to the response, as it will raise an IllegalStateException.
As such, it is normally a good policy, after a forward/redirect to return immediately, and thus prevent problems.
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Originally posted by Stefan Evans:
The flow of control in java methods does not suddenly change because you are writing servlets.
So if you call a method, it will do that method, finish it, and then continue on down through the code.

Now dealing with JSP/servlet coding, if the method call sends/commits the response (like a forward/redirect does) then you should make sure your code does not continue writing to the response, as it will raise an IllegalStateException.
As such, it is normally a good policy, after a forward/redirect to return immediately, and thus prevent problems.


Thanks Stefan and All.

So means my first post was right.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: forward happens before service() finishes
 
Similar Threads
expired problem in jsp (back button/link)
Want to find out request is sent from which page
RequestDispatcher include..
Include Statements
when exactly response is commited?