• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Urgent -RequestDispatcher.include and forward

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1514
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could it be possible that you have already written output to the client before forwarding?
 
Author
Posts: 245
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yeah, do you set any cookies?
maybe set header information?
 
Author
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks jignesh!
sorry for late reply. i am able to come on javaranch only during weekends
regards
maulin
 
reply
    Bookmark Topic Watch Topic
  • New Topic