This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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
Joined: Jul 25, 2008
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.
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.
Joined: Oct 19, 2003
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.
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: