This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
I found this question on Javabeat..could not understand what "pageContext.include" means.need some explanation.Thanks(answers are a,b,d)
Assuming that a JSP by name "one.jsp" wants to have a request time inclusion
to happen for the page "two.jsp". Choose the following right operations (assuming
that the following code is residing in "one.jsp"?
a. pageContext.include("two.jsp") b. <jsp:include page = "two.jsp" />
c. <@% include file "two.jsp" %>
d. request.getRequestDispatcher("two.jsp").include(request, response)
e. All the above.
calling include method of the PageContext has the same effect -as the answerd indicate- as calling the include method of the RequestDispatcher or using the jsp:include action. From the API
Causes the resource specified to be processed as part of the current ServletRequest and ServletResponse being processed by the calling Thread. The output of the target resources processing of the request is written directly to the ServletResponse output stream.
The current JspWriter "out" for this JSP is flushed as a side-effect of this call, prior to processing the include.
If the relativeUrlPath begins with a "/" then the URL specified is calculated relative to the DOCROOT of the ServletContext for this JSP. If the path does not begin with a "/" then the URL specified is calculated relative to the URL of the request that was mapped to the calling JSP.
And pageContext is a implicit object available in a JSP.
Looking into this, I got an unexpected result for the RequestDispatcher include method. Please see screenshot and code below. Does it make sense the file included via RequestDispatcher seems to appear always at the top, and not where it was placed in the including page? Has this something to do with flushing out? I checked the entry for RequestDispatcher.include() in the 1.4 api and didn't notice a clue about that. Hope I haven't missed something obvious or done something careless.
That's because RequestDispatcher sends included content directly to the response stream, ignoring jsp's output buffer, so included content will always appear at the begining of the stream i.e. on top of the page. That's why we need "pageContext.include" which uses the buffer.
What's the difference between Response Stream and Buffer.I thought the response is written on to the buffer It's getting confusing!
The future belongs to those who believe in the beauty of their dreams.Dream BIG!
Joined: Oct 26, 2009
As far as I understand it, jsp has it's own response stream with buffer (of type JspWriter) on top of the one we get from response.getWriter().
By the way, you can check this by setting In this case content included with RequestDispatcher will appear in the right place, because all otput from jsp will be automatically flushed to the actual response strean.