• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

include( ) v/s forward( ) - need help !

 
Niranjan Deshpande
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Have a query regarding the forward( ) and include( ) call.
Say i have the following two servlets -




as per hfsj, "forward( ) is a 'permanent handoff', so control wont return to servlet1 and the println( ) statement will
not be executed.and with inlcude( ), its a 'temporary handoff to the next resource'which means control will
return back to servlet2 after the next reaource finishes contributing to the response.

But as per enthuware mock engine,

The execution control returns to the resource that has called the forward() method after the callee
resource finishes processing.
RequestDispatcher.forward() is also a normal method call, so the control does return to the calling resource.


Who is correct ? if my hfsj interpretation is correct, enthuware is wrong ! please help me understand this,.
thanks in advance !
 
Dave Wingate
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd be interested to know the answer, too.

Were you able to try out the code? Since the issue is whether or not control returns to the caller of the forward() method, you could execute the forward()-calling servlet and then look to see if the forward()-caller's println() gets executed. If its println() method does get executed, then control must have returned to the forward()-caller.
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The forward() method is just another method. You CAN write code after its invocation and it can be executed. With include() you are including the response of another servlet as well. Try some examples to get a feel of it.
 
Paul Clapham
Sheriff
Posts: 21583
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's pretty obvious that control must return from the forward() method. If it didn't, then that would mean the method would run forever. And eventually the server would be choked with threads that had not completed. This can't be the case.
 
Niranjan Deshpande
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so the usage of the words like 'temporaray handoff' and 'permanent handoff'
respectively for include( ) and forward( ) misleads us.
is it !!!
 
Elan Ram
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Just want to share my understanding regarding the include() and forward().

When we are using forward(), forwarded resource is responsible for handling response. The container has to close response object in the forwarded resource (Eg end of service method in case of Servlet).
You can see the control coming back to the forwarding resource, but we cannot write anything to the response to alter the response content. Because the container provider must close the response object in the forwarded resource itself. So what we can do here is we can log events, print into server console, and many more except the alteration in the response.

Incase of include(), just to add some extra content into response by included resource. But included resource has less privilege than forwarded resource, because it cannot set header or status to the response.
 
madhav changala
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

i have the same answer as elan

yes you are correct elan

bye

all the best for niranjan
 
Micheal John
Ranch Hand
Posts: 344
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good explanation elan!!

And I think, you can modify the response from the forwarded resource using filter and servlet 2.4 having the option for it <dispatcher>FORWARD/INCLUDE</dispatcher>

Am i right .. ?elan
 
Elan Ram
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jhon,

As per 2.4 spec... Yes. we can, If we wraps the original response object in the filter. otherwise it wont work.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic