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

difference between pageContext.include and jsp:include

 
swapna rao
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Please let me know the difference between pageContext.inculde and jsp:include.
Is is like poageContext.include flushes the current page output first before including other components.please explain with example.
 
swapna rao
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
any update on this thread is greatly appreciated.
 
Vikas Parikh
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As long as i know there are 3 ways to include a particular resource

1) RequestDispacther

[a] URI Path in this case may be relative to current request (without starting with '/'

RequestDispacther rd = request.getRequestDispatcher(<uri path of resource> ;
rd.include(request, response);

OR

URI Path in this case has to start with '/' , relative to context of web app


RequestDispacther rd = <serletcontext>.getRequestDispatcher(<uri path of resource> ;
rd.include(request, response);

OR

[c] Fetching RD by the name of resource

RequestDispacther rd = <serletcontext>.getNamedDispatcher(<name of resource as defined DD> ;
rd.include(request, response);

2) PageContext

[a] This will directly include the resource into current output stream

[B]Note the following:


i) The current output of the buffer of calling JSP is flushed automatically & then Included resource is called)
Flush by default is true.

pageConext.include(<Relative URL Path> ;


OR

This will directly include the resource into current output stream

[B]Note the following:


i) The current output of the buffer of calling JSP is flushed according to the value supplied & then Included resource is called)


pageConext.include(<Relative URL Path>, <flush or not before calling the included resource> ;

3) JSP Include action

Note the following:

Here Page is a required attribute , where as flush is an optional attribute.

Default value of flush is false ...


<jsp:include page="" flush="" />

Hope that helps

Vikas Parikh
 
swapna rao
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Vikas for the explanation.
My understanding is that pageContext.include(relative path) is same as that of <jsp:include page="" flush="true">. In both the cases , current page output will be flushed before the included page.Hope i'm correct.
In which scenarios do we need to use pageContext.include() because both methods are going to give the same output. what is the need of two methods.Are there any pros and cons for these methods.
 
Vikas Parikh
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yep .. you are correct



AND



will produce the same.

One of the reason, which i am aware of, why are there two ways of doing same is as below:

Many times, one want to have Script less JSP Pages/ documents.
In such scenario, one can not use scriptlets in JSP.
So you can not invoke method through JSP. So in that situation Standard JSP action is provided as <jsp:include .. />

Fortunately, it is provided as JSPs standard action, otherwise one need to have Custom Tag to include another resource, just as to print or set the attributes, we have apache's JSTL reference implementation.

BTW, there is yet another way to include resource, if you want to explore

<c:import ..>

It has many more features than just including ..

Vikas
 
Anicca Golden
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree to Vikas:

no scripting in JSPs!
So pageContext.include... is for servlets/tag handler and <jsp:include... is for JSPs.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic