File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes question of Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "question of "include" in JSP spec 2.0 " Watch "question of "include" in JSP spec 2.0 " New topic
Author

question of "include" in JSP spec 2.0

kenneth See
Greenhorn

Joined: Aug 21, 2008
Posts: 7
Dear all,

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.

Kenneth

-------------------------------------------------
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.
Charles Lyons
Author
Ranch Hand

Joined: Mar 27, 2003
Posts: 836
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 / Amazon Amazon UK )
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: question of "include" in JSP spec 2.0