File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes RequestDispatcher Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "RequestDispatcher" Watch "RequestDispatcher" New topic
Author

RequestDispatcher

Ogeh Ikem
Ranch Hand

Joined: May 13, 2002
Posts: 180
Am I correct in assuming that after servlet uses a RequestDispatcher to include content of another component, the servlet can no longer do anything that involves sending HTTP headers?
Im making this assumption based on the fact that when a servlet uses a RequestDispatcher to include the content of another component, the servlet's response output stream automatically gets flushed before the content of the other component is included.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

"ogeh",
The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at http://www.javaranch.com/name.jsp.
We require names to have at least two words, separated by a space, and strongly recommend that you use your full real name. Please edit your profile and select a new name which meets the requirements.
Thanks.
Dave
Bob Kerfoot
Ranch Hand

Joined: Oct 01, 2000
Posts: 47
Ogeh,
You can still send HTTP headers from the including servlet after using a RequestDispatcher to include() the content of another component as long as the response has not been committed. A response has been committed if it has already had its status code and headers written. You can check the commit status with response.isCommitted().
Also, neither the Servlet specification nor Javadocs specify that an automatic flush of the response output stream occurs before the content of the other component is included.
Bob Kerfoot
Ogeh Ikem
Ranch Hand

Joined: May 13, 2002
Posts: 180
Thanks Bob Kerfoot, for your reply. The SCWCD Exam Study Kit by Hanumant Deshmukh and Jignesh Malavia says that the following method calls are all equivalent:
Construct 1
<%
RequestDispatcher rd =
request.getRequestDispatcher("other.jsp");
rd.include(request, response);
%>
Construct 2
<%
pageContext.include("other.jsp");
%>
Construct 3
<jsp:include page="other.jsp" flush="true"/>
In other words, the RequestDispatcher and PageContext always flush the output of the current page before including the other components. Do you support this?
Bob Kerfoot
Ranch Hand

Joined: Oct 01, 2000
Posts: 47
ogeh,
I support most of it. However, as regards Construct 3, if you look at page 77 of the JSP 1.2 specification it states that the default value for the flush attribute is false:
"The flush attribute controls flushing. If true, then, if the page output is buffered and the flush attribute is given a �true� value, then the buffer is flushed prior to the inclusion, otherwise the buffer is not flushed. The default value for the flush attribute is �false�"
As regards Construct 1, neither the Servlet 2.3 specification nor Javadocs specify that an automatic flush of the response output stream occurs before the content of the other component is included. I found through testing that Tomcat 4.1.24 indeed does not flush the buffer and send the status code and headers to the client. Specifically, after an rd.include() you can still do response.sendError(404, "Some error") without raising an exception. However, if you execute response.flushBuffer() then response.sendError(404, "Some error"), a java.lang.IllegalStateException will be raised.
Therefore, unless there is a hidden flush mechanism that does not commit the response, I still contend that the buffer is not flushed before the rd.include() and the empirical evidence as stated above seems to bear this out.
Let me know what you think.
Bob Kerfoot
Ogeh Ikem
Ranch Hand

Joined: May 13, 2002
Posts: 180
Looks like the book may be wrong. I'll contact the authors now. Thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: RequestDispatcher