• 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

RequestDispatcher

 
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looks like the book may be wrong. I'll contact the authors now. Thanks.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic