This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
I have read the JSP spec 2.0 (section 5.4) about command "Include" but I cannot understand the difference between "relative to current JSP page" and "relative to current JSP files". Can anybody explain about this? Can you also explain why we got those results in the following 4 cases? Many many thanks.
------------------------------------------------- From the spec: "The page attribute of both the jsp:include and the jsp:forward actions are interpreted relative to the current JSP page, while the file attribute in an include directive is interpreted relative to the current JSP file. See below for some examples of combinations of this."
For an example of a more complex set of inclusions, consider the following four situations built using four JSP files: A.jsp, C.jsp, dir/B.jsp and dir/C.jsp:
JavaServer Pages 2.0 Specification Case1� A.jsp says <%@ include file=�dir/B.jsp�%> and dir/B.jsp says <%@ include file=�C.jsp�%>. In this case the relative specification C.jsp resolves to dir/C.jsp.
Case2� A.jsp says <jsp:include page=�dir/B.jsp�/> and dir/B.jsp says <jsp:include page=�C.jsp� />. In this case the relative specification C.jsp resolves to dir/C.jsp.
Case3� A.jsp says <jsp:include page=�dir/B.jsp�/> and dir/B.jsp says <%@ include file=�C.jsp� %>. In this case the relative specification C.jsp resolves to dir/C.jsp.
Case4� A.jsp says <%@ include file=�dir/B.jsp�%> and dir/B.jsp says <jsp:include page=�C.jsp�/>. In this case the relative specification C.jsp resolves to C.jsp.
It's to do with static vs. runtime includes. The <jsp:include /> mechanism effectively uses a RequestDispatcher and its include() method at runtime, while the <%@ include ... %> directive copies another JSP's source code into the current JSP before it is compiled into a servlet.
So when they refer to "relative to JSP file", they mean relative to the location of the actual JSP file on the hard disk.
When they say "relative to JSP page" they are referring to the complete JSP compilation unit, which may contain other .jsp files statically copied and compiled in.
So in case 1 we're using all static inclusions and it does what we want: A.jsp statically includes /dir/B.jsp which statically includes C.jsp in the same directory---hence /dir/C.jsp. Overall we now have A.jsp as a single JSP with all the other JSPs statically compiled in. It does no runtime calls using a RequestDispatcher.
In case 4: A.jsp includes /dir/B.jsp by filesystem path. This produces the JSP file A.jsp which also has /dir/B.jsp statically compiled into it at the appropriate point (i.e. where the <%@ include ... %> occurs). The JSP page is still A.jsp, which effectively consists of two JSP files. The <jsp:include page="C.jsp" /> action is copied into A.jsp verbatim before compilation into the servlet. So now when it executes (at runtime, using RequestDispatcher) the path is relative to the current page which is A.jsp. So it will try to include C.jsp.
The rule is a JSP page is a complete compilation unit which may consist one or more JSP files. Static inclusion is relative to the file. Dynamic inclusion is relative to the entire JSP compilation unit ("page").
Hope that helps. Try to explain the other two cases (2 and 3) for yourself and post back if still you have a problem
Charles Lyons (SCJP 1.4, April 2003; SCJP 5, Dec 2006; SCWCD 1.4b, April 2004)
Author of OCEJWCD Study Companion for Oracle Exam 1Z0-899 (ISBN 0955160340 / AmazonAmazon UK )