Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

"pageContext.include"

 
Kosala W.Abayagunawardene
Ranch Hand
Posts: 47
Firefox Browser Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Pete Pommelich
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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.

Correct me if I'm wrong.

Kind regards,
 
Mike Mitchell
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Thanks,
Miike








 
Paul Tokmakov
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mike Mitchell
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Paul!
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's the difference between Response Stream and Buffer.I thought the response is written on to the buffer
It's getting confusing!
 
Paul Tokmakov
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kosala W.Abayagunawardene
Ranch Hand
Posts: 47
Firefox Browser Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys you been big help.keep up the good work
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic