This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

RequestDispatcher and flushBuffer

 
Muhammad Faisal
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
AJAY RANA
Ranch Hand
Posts: 75
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 44
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, It works.
Thank you very much guys for posting your replies.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic