JSP include directive, jsp:include action, relative vs. absolute paths
/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
Matt Mc
Favorite articles: http://ijoshsmith.com/2011/11/28/debugging-exceptions-in-xcode-4-2/#comment-631 SOreadytohelp
Updated on September 12, 2022Comments
-
Matt Mc over 1 year
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 thejsp:include
action:<jsp:include page="template/Body-Footer.jsp"></jsp:include>
It generates the error
javax.servlet.ServletException: File "/template/Body-Footer.jsp" 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? -
Luiggi Mendoza about 10 yearsThe other path is absolute regarding the application context URL.
-
developerwjk about 10 yearsIts absolute RELATIVE to the application context, thus not really absolute at all. There's a clear confusion in terminology going on. Of course your relative paths have to be complete, otherwise, how would they work?
-
Luiggi Mendoza about 10 yearsThat's what I've said. I don't negate the fact that they are relatives paths after all.
-
Matt Mc about 9 yearsThis is helpful, thanks. However, on your recommendation of putting the JSPs outside of the WEB-INF folder, maybe I missed it but I couldn't see something in that link that recommended that. In support of putting the JSPs in WEB-INF, we have this: stackoverflow.com/questions/6825907/why-put-jsp-in-web-inf
-
Christopher Schultz almost 4 yearsPlacing your JSP files under
/WEB-INF/
is a common practice which "hides" the pages from direct-requests, allowing you to ensure that all requests are directed e.g. through a controller or other mechanism that protects your scripts from casual browsing.