Search code examples
javajspjsp-tags

Dynamically including website components in JSP


Is there a way to use root path when refering to includes in JSP? what I mean is instead of using this method:

<jsp:include page="../inc/header.jsp" />
<jsp:include page="../../inc/header.jsp" />

etc.
To just refer to it like this:

<jsp:include page="jsp/inc/header.jsp" />

Also this is how my project tree looks like Project tree


Solution

  • Anybody trying to figure it out here is the answer that I came up with. For some reason intelij IDE tells me that the path cannot be resolved(liar!) looking at my project tree you can include header to any file using this piece of code in your project:

    <%@ include file="/jsp/inc/header.jsp" %>
    This is page content
    <jsp:include page="/jsp/inc/footer.jsp" />
    

    In both cases the IDE will tell you that it cannot resolve URL but it works like a charm I hope this helps not only me but future visitors