aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes RequestDispatcher Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "RequestDispatcher" Watch "RequestDispatcher" New topic
Author

RequestDispatcher

deepa raj
Ranch Hand

Joined: Jul 25, 2008
Posts: 124
test.jsp



test1.jsp




What could be the answer , if test.jsp executes.
Balaji Natarajan
Greenhorn

Joined: Nov 24, 2007
Posts: 29
Good question.

For the above scenario, the output is given below,
this is test1.jsp - not bolded
this is test.jsp
Back from test1.jsp

If you want the text to be printed in order with correct style, then just use include directive or jsp:include standard action.


Best Regards,<br />Balaji Natarajan<br /> <br />SCJP 1.5 100%<br />SCWCD 1.5 92%<br />SCBCD (In Progress)
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

hi Balaji Natarajan ,

please, Can you explain me the reason behind this ?
deepa raj
Ranch Hand

Joined: Jul 25, 2008
Posts: 124
why is it like that.

include directive and include action are place centric (in which part of the page is called ,the called page will be included in that place only. )

whether RequestDispatcher.include() isn' a place centric call??
Milind Patil
Greenhorn

Joined: Oct 19, 2003
Posts: 26
Dynamic includes are not place centric. Mostly the content generated by Dynamic includes are placed at the top of generated output. Dynamic includes contains,
1. include action (<jsp:include>
2. RequestDispatcher.include() call


Static includes are place centric. Contents is inserted at the point where it is called. Static includes contains,
1. include directive (<%include file="hello.html"%>

So as Balaji already mentioned, the output will be.. (I have tested it )

this is test1.jsp - not bolded
this is test.jsp
Back from test1.jsp


Milind B. Patil - SCJP-1.4, SCWCD-5.0, SCBCD-5.0
deepa raj
Ranch Hand

Joined: Jul 25, 2008
Posts: 124


Disagree

because i have run the following code


it is giving the following output.


so only the RequestDispatcher .include is not place centric.
But i need the reason for that.
raja ram
Ranch Hand

Joined: Mar 02, 2008
Posts: 169
Hi,

There are following ways to include the file.
<%@ include file="test1.jsp" %> static inclusion
<jsp:inlcude page="test1.jsp"/> Dynamic inclusion
in Jsp side both of them are place centric in the same lines we have
request.getRequestDispatcher("/test1.jsp").include() or config.getServletContext().getRequestDispatcher("/test1.jsp").include()
this is also dynamic and behaves in same way as that of <jsp:include> but on servlet side
I executed your code in the Jsp side and found same kind of issue you faced later i wrote two servlets and included
the secondservlet in first and got the correct out put so the issue is that we have to use this on the servlet side to
get correct behaviour as in the Jsp side this is my understanding please correct me if i am wrong.

Thanks
Milind Patil
Greenhorn

Joined: Oct 19, 2003
Posts: 26
Deepa,

You are right my bad...
Looks like call to include action (<jsp:include> is place centric and call for RequestDispatcher.include() is not.

I think its because JSP pages have their in-build buffering mechanism. So its just buffering the output and flush it at then end.

But in case of RequestDispatcher. It uses HttpServletResponse's writer object. So I think that may be reason, why content of RequestDispatcher.include() are not place centric.

Anybody have any idea?
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

any update on this?. if anybody knows please share your thought
Rufus Addis
Ranch Hand

Joined: Aug 21, 2008
Posts: 83
I think it's already been answered, but a little more depth if you care.

If you include one JSP page in another using the RequestDispatcher then you are creating two instances of the JSPWriter with unpredictable results. In Tomcat, the test2.jsp writes and flushes before test.jsp has flushed, hence it's output appears first. However with different buffering they may appear in a different order.

If you add an explicit flush before the request dispatcher:



then the first part of test.jsp gets commited before test2.jsp starts!

In a <jsp:include you are using a different include mechanism where the out object is passed to the included page, so they share the same JSPWriter. i.e. in Tomcat:

org.apache.jasper.runtime.JspRuntimeLibrary.include(request, response, "test1.jsp", out, false);

HTH - Rufus
siyabonga khanyile
Greenhorn

Joined: Aug 22, 2008
Posts: 12
I know different containers react differently to this issue but basically it all has to do with the different buffering mechanism used by <jsp:include> RequestDispacher.include

You can check here for more info:

http://forums.sun.com/thread.jspa?threadID=710985&messageID=4116014
 
Consider Paul's rocket mass heater.
 
subject: RequestDispatcher