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.jspsays<%@ include file="dir/B.jsp"%>anddir/B.jspsays<%@ include file="C.jsp"%>. In this case the relative specification "C.jsp" resolves to "dir/C.jsp" -
A.jspsays<jsp:include page="dir/B.jsp"/>anddir/B.jspsays<jsp:include page="C.jsp"/>. In this case the relative specification "C.jsp" resolves to "dir/C.jsp". -
A.jspsays<jsp:include page="dir/B.jsp"/>anddir/B.jspsays<%@ include file="C.jsp" %>. In this case the relative specification "C.jsp" resolves to "dir/C.jsp". -
A.jspsays<%@ include file="dir/B.jsp"%>anddir/B.jspsays<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.jspsays<%@ include file="dir/B.jsp"%>anddir/B.jspsays<%@ include file="C.jsp"%>. In this case the relative specification "C.jsp" resolves to "dir/C.jsp"A.jspis includingdir/B.jspat translation time thenB.jspincludesC.jspat translation time. Because they are both at translation time both includes are relative to whatever file is doing the including. The including fileC.jsppath indir/B.jspis relativedir/B.jspfile location which is indirdirectory. Therefore, the relative specificationC.jspresolves todir/C.jsp. -
A.jspsays<jsp:include page="dir/B.jsp"/>anddir/B.jspsays<jsp:include page="C.jsp"/>. In this case the relative specification "C.jsp" resolves to "dir/C.jsp".A.jspincludesdir/B.jspat request time (it is relative to the currentA.jsppage location), thendir/B.jspincludesC.jspat request time. TheC.jspis relative to the current pagedir/B.jsp. Therefore, the relative specification "C.jsp" resolves to "dir/C.jsp". -
A.jspsays<jsp:include page="dir/B.jsp"/>anddir/B.jspsays<%@ include file="C.jsp" %>. In this case the relative specification "C.jsp" resolves to "dir/C.jsp".A.jspincludesdir/B.jspat request time and thendir/B.jspincludesC.jspat translation time. The inclusion ofC.jsphappens at at translation time and it is relative todir/B.jsp. Therefore, the relative specification "C.jsp" resolves to "dir/C.jsp". -
A.jspsays<%@ include file="dir/B.jsp"%>anddir/B.jspsays<jsp:include page="C.jsp"/>. In this case the relative specification "C.jsp" resolves to "C.jsp".A.jspincludesdir/B.jspat translation time. Thedir/B.jspbecome part of code inA.jsppage as the same "translation unit". Then at request time the new "translation unit" includesC.jsp, but becasue it is at request time anddir/B.jsphas been included inA.jspas one "translation unit". The includingC.jspis relative to the new "translation unit" and notdir/B.jsp. Therefore, the relative specification "C.jsp" resolves to "C.jsp".
Most Recent jsp Faqs
- How to get the real requested URI from inside the error page?
- Can I define a method in a JSP page?
- What is the difference between request.getParameter() and request.getAttribute()?
- What is difference between page and pageContext in JSP pages?
- How to config a JSP file in web.xml?
- What is the difference between jsp:forward and response.sendRedirect?
- How to retrieve values from HTML form input elements in JSP?
Most Viewed jsp Faqs
- How to config a JSP file in web.xml?
- What is difference between page and pageContext in JSP pages?
- How to disable browser caching for a specific JSP?
- How to retrieve values from HTML form input elements in JSP?
- What is the difference between jsp:forward and response.sendRedirect?
- What is the difference between request.getParameter() and request.getAttribute()?
- How to get the current JSP page name?