Search code examples
javajspscriptlet

JSP include page failing with request.getContextPath()


I'm trying to include a jsp page in another, and since I don't want to hardcode paths I want to use the request.getContextPath() methods. But I just can't get it to work. I've tried various things, based on various answers here:

First thing I did was

<jsp:include  page="<%=request.getContextPath()%>/structure/Navbar.jsp" />

gives me:

HTTP Status 500 - /structure/ArtiMarziali/Aikido.jsp (line: 11, column: 24) attribute value for [page] is not properly terminated

So I tried:

<% String aikido = request.getContextPath() + "/structure/Navbar.jsp"; %>
<jsp:include  page="<%= aikido %>" />

throws an exception:

HTTP Status 500 - javax.servlet.ServletException: File [/ProgettoPW/structure/Navbar.jsp] not found

Then:

<jsp:include  page="<%= request.getContextPath() + "/structure/Navbar.jsp"  %>" />

says I need to escape the quotes, which I can't really do

and

<jsp:include  page='<%= request.getContextPath() + "/structure/Navbar.jsp"  %>' />

gives the same error as the first one. I tried setting the result of getContextPath to a variable and using that in the tag, but same errors as before.

I really don't know what the problem i, I need some help, thank you.

(Yes I know that using <%= %> and other scriptlets is frowned upon nowadays, but my course was taught this way and I want to adhere to it for the project)

Edit: This is the file structure, might be useful:

enter image description here


Solution

  • The path is already relative to the context path such that you have no need to add it in your include directive (hopefully), which means that this will work:

    <jsp:include page="/structure/Navbar.jsp" />