FAQ


Advertisement



How to interpret paths in JSP's inclusion?

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 (in the JSP specification 1.2 page 78.):

  • 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"
  • 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".
  • 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".
  • 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".

Bascilly, the <jsp:include page=... /> happens at runtime but <%@ include file=... %> happens at compile time. <jsp:include> simply passes control to a different page for a while. Under <jsp:include>, the two pages share 'request' scope but have a different 'page' scope. You can find more information about the inclusion in JSP pages at When and How to use inclusion in JSP pages?

  • 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"

    A.jsp is including dir/B.jsp at translation time then B.jsp includes C.jsp at translation time. Because they are both at translation time both includes are relative to whatever file is doing the including. The including file C.jsp path in dir/B.jsp is relative dir/B.jsp file location which is in dir directory. Therefore, the relative specification C.jsp resolves to dir/C.jsp.
  • 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".
    A.jsp includes dir/B.jsp at request time (it is relative to the current A.jsp page location), then dir/B.jsp includes C.jsp at request time. The C.jsp is relative to the current page dir/B.jsp. Therefore, the relative specification "C.jsp" resolves to "dir/C.jsp".
  • 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".
    A.jsp includes dir/B.jsp at request time and then dir/B.jsp includes C.jsp at translation time. The inclusion of C.jsp happens at at translation time and it is relative to dir/B.jsp. Therefore, the relative specification "C.jsp" resolves to "dir/C.jsp".
  • 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".
    A.jsp includes dir/B.jsp at translation time. The dir/B.jspbecome part of code in A.jsp page as the same "translation unit". Then at request time the new "translation unit" includes C.jsp, but becasue it is at request time and dir/B.jsp has been included in A.jsp as one "translation unit". The including C.jsp is relative to the new "translation unit" and not dir/B.jsp. Therefore, the relative specification "C.jsp" resolves to "C.jsp".


Printer-friendly version Printer-friendly version | Send this 
article to a friend Mail this to a friend

Previous Next vertical dots separating previous/next from contents/index/pdf Contents

  |   |