Reputation: 9297
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 "/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?
Upvotes: 2
Views: 13653
Reputation: 48753
I read JSP 2.0 spec and here:
Relative URL Specifications
* A context-relative path is a path that starts with a slash (/).
It is to be interpreted as relative to the application to which
the JSP page or tag file belongs. That is, its ServletContext
object provides the base context URL.
* A page relative path is a path that does not start with a
slash (/). It is to be in- terpreted as relative to the current
JSP page, or the current JSP file or tag file, depending on where
the path is being used.
For now javax.servlet.ServletContext.getRealPath("/WEB-INF/test/test.jsp")
is null for security reason.
Assume that context-relative path is path from your WAR root.
Upvotes: 0
Reputation: 47
Answer to WHY - The jsp:include
is a runtime directive unlike the <%@ include ... %>
directive which happens to be a compile time directive (translation time, actually).
See more on: JSP Performance using jsp:include
Bottom line - directives are run against different folders as a base.
Btw. JSP pages should be outside of WEB-INF folder, if you want to follow official recommendation:
The Java EE 6 Tutorial - Web Module Structure
Upvotes: 0
Reputation: 8659
/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
Upvotes: 6