aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes RequestDispatcher and flushBuffer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "RequestDispatcher and flushBuffer" Watch "RequestDispatcher and flushBuffer" New topic
Author

RequestDispatcher and flushBuffer

Muhammad Faisal
Greenhorn

Joined: Oct 14, 2002
Posts: 11
HI,
According to Sun specifications for servlets,
"The forward method of the RequestDispatcher interface may be called by the
calling servlet only when no output has been committed to the client. 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. If the response has been
committed, an IllegalStateException must be thrown."
--------------------------------------------------
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class requestWriter extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException,ServletException
{
PrintWriter out = response.getWriter();

out.println("I am the source servlet ");

response.flushBuffer();

RequestDispatcher rd = request.getRequestDispatcher("requestWriter1");

rd.forward(request,response);

out.println("<BR> is committed <BR>"+response.isCommitted());


}
}
--------------------------------------------------
This servlet was deployed to Tomcat 4.1.6 . The output was
"I am the source servlet"
According to Sun specifications it should have thrown the IllegalStateException
bcaz i called RequestDispatcher.forward method after committing the response.
Need your comments.


SCWCD J2EE, OCP(Internet App Dev), SCJP 2
AJAY RANA
Ranch Hand

Joined: May 30, 2002
Posts: 75
Dear Orazone ,
U will find such ambiguities in these methods.The reason is that Tomcat implementation is smart enough to understand and not throw any exceptions.
Regards,
Ajay Rana
SCJP,SCWCD
See El
Ranch Hand

Joined: Oct 07, 2002
Posts: 44
Hi Orazone,
I've tried your code and noted the same behavior however after some investigation I've found that there is an entry with exception in /logs/localhost_log.%DATE%
I'm still using Tomcat 4.0.3.


See El<br />SCJP2, SCWCD, IBM XML, SCBCD
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
I don't think there is an ambiguity here, and that Tomcat threw the IllegalStateException the way it was supposed to. If you expected a 500 Internal Server Error response, think again. The response had already been committed. Tomcat had no way to return a 500 response code anymore. In such cases, some servlet containers simply append the stack trace to whatever had been written to the client in the hope that it will actually be displayed somewhere, some don't. Apparently Tomcat doesn't. Try this:

- Peter
Muhammad Faisal
Greenhorn

Joined: Oct 14, 2002
Posts: 11
Yes, It works.
Thank you very much guys for posting your replies.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: RequestDispatcher and flushBuffer