• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

forward happens before service() finishes

 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
vidya sagar
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the case when sendRedirect is used instead of forward, for same code given by rathi.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 1669
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic