JSP include directive, jsp:include action, relative vs. absolute paths

Matt Mc picture Matt Mc · Feb 17, 2014 · Viewed 11.9k times · Source

I am doing some basic templating in my JSP-based webapp. For example, I want to have a standard header and footer (basic HTML) that I pull into each of my JSPs.

My content JSP is at /WEB-INF/jsp/home.jsp, and I have template JSPs at /WEB-INF/jsp/template/, such as /WEB-INF/jsp/template/Body-Footer.jsp.

So now, within home.jsp, I want to pull in my template files. First, I try the jsp:include action:

<jsp:include page="template/Body-Footer.jsp"></jsp:include>

It generates the error javax.servlet.ServletException: File &quot;/template/Body-Footer.jsp&quot; not found

Strange to me, considering that Eclipse says that the path is valid.

Okay, so then I switch to the include directive:

<%@ include file="template/Body-Footer.jsp" %>

This works just fine, pulls in my footer HTML.

But why does the jsp:include not work? After some experimentation, I find that putting in the absolute path does get it to work:

<jsp:include page="/WEB-INF/jsp/template/Body-Footer.jsp"></jsp:include>

Now it works fine, no errors.

So here's my question: why? Why do I (apparently) need to use an absolute path with the jsp:include action, but not with the include directive?

Answer

developerwjk picture developerwjk · Feb 17, 2014

/WEB-INF/jsp/template/Body-Footer.jsp is not an absolute path. Its also a relative path. The problem is that template/Body-Footer.jsp is an incomplete relative path, whereas the other is complete. That is, the paths are relative to your app path. Since /WEB-INF/ is under your app path, you have to include it. Absolute path means like C:/program files/tomcat/webapps/yourapp/WEB-INF/jsp/template/Body-Footer.jsp