Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

forward--RequestDispatcher

 
Verity smith
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
RequestDispatcher,forward()
The execution control returns to the resource that has called the forward() method after the callee resource finishes processing

could someone please explain this to me.
regards
verity
[ June 13, 2003: Message edited by: Verity smith ]
 
Panagiotis Varlagas
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We have a resource that calls the forward() method
on a RequestDispatcher. We say "resource" because it can be either a JSP or a servlet that calls the forward() method on the RequestDispatcher. (Well,under the hood, they are really both servlets at runtime, but on development time they are two different animals to the developer).
After the resource that the request
has been forwarded to finishes execution than the execution control returns to the original resource. This means that if (say) servlet A originally called forward on a RequestDispatcher and this resulted to the request getting forwarded to (say) servlet B, as soon as servlet B is done with its service() method, the control returns to servlet A (i.e. the statements after forward() in A DO get executed!).
Hope this helps,
Panagiotis
(back to Javaranch after a long virtual absense...)
Originally posted by Verity smith:
hi,
RequestDispatcher,forward()
The execution control returns to the resource that has called the forward() method after the callee resource finishes processing

could someone please explain this to me.
regards
verity
[ June 13, 2003: Message edited by: Verity smith ]
 
Ken Januski
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe I'm just getting tired. Isn't this what RequestDispatcher.include() does?
RequestDispatcher.forward() allows the servlet/jsp that is forwarded to to finish the processing. I didn't think that it ever returned to original page.
 
Amer Khan
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
RequestDispatcher.forward() is just a normal method call , so the control does return to the calling resource.

for example in a simple java program:
method A() calls method B() and method B() calls method C()
After executing method C (method C prints something or whatever) control RETURNS to method B then to method A.It is as simple as that!
Now the question is if the fowarded resource throws an exception WHAT HAPPENS then?
 
Ken Januski
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm. What happens then? I need to think about that. But I'm still a little confused by the forward/include differentiation. If forward returns to the caller like any other method, then why not just use include? I thought that the purpose of forward was to end processing in the first page and transfer it to the second to finish all processing of that request? Though it may be true that forward can return to the original page I'm just not sure why it would make sense to do so. Am I missing something?
 
Raymond Kong
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
RequestDispatcher is an interface and has two method, forward(...) and include(...).
Suppose Servlet A calls Servlet B.
For the include(...), the Servlet A can use PrintWriter to write output first and then calls Servlet B to append another output, then back to Serlvet A. Servlet A can still append more output.
For the forward(...), all output written by Serlvet A will be cleared before calling to Serlvet B. And Serlvet B is responsible to write the output and then back to Serlvet A. At this moment, Serlvet A cannot append more output else IllegalStateException will be thrown.
 
s khosa
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, for forward() call, the called servlet can modify the response headers and it can bee seen, but for include() call, the called servlet even if modifies the headers, the effect is negated as soon as the call returns back to the caller servlet.
 
Ken Januski
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To get back to Amer's question about what happens if a resource that's been forwarded to throws an exception. ServletExceptions and IOExceptions are sent back to caller. According to specification runtime exceptions should also be propagated back to calling servlet.
All other exceptions I believe are wrapped as ServletExceptions and also propagated back, with the original exception noted as root cause.
 
yogen joshi
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now this is getting confusing,
Can anybody once and for all definitely confirm what actually happens,
requestDispatcher.forward(req, res) -- > returns control back to the calling servlet after the call. [true / false]
requestDispatcher.include(req, res) --> returns the control back to the calling servlet after the call. [true / false]
yogen
 
Panagiotis Varlagas
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yogen,
First of all: Don't get mad...
Here is what the spec says: (Java Servlet Spec, v2.3)
"SRV.8.4 The Forward Method
The forward method of the RequestDispatcher interface may be called by the
calling servlet only when no output has been committed to the client."

This means that if you have committed any output to the client (e.g. by way of an out.flush()) then the servlet container won't let you invoke forward()...
"... If output data exists in the response buffer that has not been committed, the content must be cleared before the target servlet’s service method is called."
This means that the servlet container will clear any uncommitted data in the buffer when you invoke forward() and before the request reaches the target servlet.
"... If the response has been committed, an IllegalStateException must be thrown.
[...]
Before the forward method of the RequestDispatcher interface returns, the response content must be sent and committed, and closed by the servlet container."

This means that the servlet container ensures that the response has been committed before it returns.
One thing that confuses people is what happens after the forward() method return. Well, remember that this is still good ol' Java language. The statements (if any) following forward() _have_ to be executed. If e.g. you have a logging statement after forward(), (say)

then it will get print to your standard output just fine.
So, the answer to your first question is [true]
[ June 20, 2003: Message edited by: Panagiotis Varlagas ]
 
Panagiotis Varlagas
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer to your second question is also [true]. I mean, the initial servlet, is even supposed to send more stuff the the response stream after include() is over with, so those statements have to be executed.
Let me reiterate the main point here. The servlet code that makes the call to include() or forward() is really nothing more than Java code. In Java statements have to get executed one by one. The only way for the statement immediately following (say) forward() not to get executed is if forward throws an exception. If forward() does its work without any exceptional conditions getting raised, then the next statement will get executed just fine.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic