*
The moose likes Servlets and the fly likes Urgent -RequestDispatcher.include and forward Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Urgent -RequestDispatcher.include and forward" Watch "Urgent -RequestDispatcher.include and forward" New topic
Author

Urgent -RequestDispatcher.include and forward

Ruchi Verma
Greenhorn

Joined: Mar 28, 2002
Posts: 9
Hi All,
I am working on a application where I have one controller servlet and in that controller servlet I am basically including the response from the other multiple servlets in this controller servlet. Then I need to forward to the jsp page from this controller servlet. My problem is I am able to include in the response in the controller servlet from the other servlets with RequestDispatcher.include. But when I try to forward using RequestDispather.forward to JSP page I get the IllegalStateException error.
Please help me!!
Thanks in Advance
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
Could it be possible that you have already written output to the client before forwarding?


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
chanoch wiggers
Author
Ranch Hand

Joined: May 24, 2001
Posts: 245
yeah, do you set any cookies?
maybe set header information?


chanoch<p><a href="http://www.amazon.com/exec/obidos/ASIN/1861007736/" target="_blank" rel="nofollow">Author of Professional Apache Tomcat</a></p>
Jignesh Malavia
Author
Ranch Hand

Joined: May 18, 2001
Posts: 81
Hello Ruchi,
The combination of include and forward that you have chosen will never work. The reason is that when you use RequestDispatcher.forward() -
a) If the data is already committed, then it throws IllegalStateException, and
b) If the data is still in the output buffer (not committed), then the forward() method discards the data (clears the buffer) before executing the forwarded resource. Therefore the data generated by any previous components (the other multiple worker servlets in your case) is lost.
So in either of the above cases, you will not achieve the desired result.
HTH
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
hi jignesh
i have a question about this.
the usual buffer size is 8k. so if the included content is > 8k then it would have to COMMIT it. right??? and if this happens then forward() will fail for sure (apart from the perfect valid reasons u mentioned). correct me if i am wrong.
thanks!
maulin
Jignesh Malavia
Author
Ranch Hand

Joined: May 18, 2001
Posts: 81
Yes, if the response buffer gets full, then addition of more data to the buffer will automatically send the data to the client, which means response will be committed. And if that happens then according to the specs, forward() 'should' fail for sure.
But tomcat 4 behaves a bit weird.
a) If the data is not commited, forward() works as expected
b) If the data is already commited, then forward() does not do anything. It neither forwards the request nor does it throw any exceptions. And you see the output that was generated before the call to forward()
Btw, about the buffer size; it is mandated for JSP pages to be atleast 8kb by default, I m not sure about servlets.
-j
Jignesh Malavia
Author
Ranch Hand

Joined: May 18, 2001
Posts: 81
Sorry about the previous post. The forward method does throw the IllegalStateException. But the tomcat container catches it, logs the error in the log file, and flushes the data present in the buffer. So we see the buffer output on the browser window instead of an error message. Check the log file and you will find the exception stack trace.
-j
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
thanks jignesh!
sorry for late reply. i am able to come on javaranch only during weekends
regards
maulin
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Urgent -RequestDispatcher.include and forward