3

How do I include a file into my jsp file from the resources path? What should the path be specifically. Using either @include or jsp:include.

My directory structure:

enter image description here

I want to include resources/static/template/index.html what should be the include path in home.jsp.

user3690467
  • 3,049
  • 6
  • 27
  • 54

3 Answers3

3

You shouldn't have .jsp files in any subdirectory of WEB-INF, and if webapp is the context root of your web application, there's no way you can include anything outside it, since both the include directive and standard tag only work on files under the context root.

If you were to move the whole resources directory under webapp, then you'll be able to use one of the following:

<%@include file="/resources/static/template/index.html"%>

Or

<jsp:include page="/resources/static/template/index.html"/>
Emanuele Giona
  • 781
  • 1
  • 8
  • 20
  • Makes sense. I was reading Manning's Spring in Action and they put jsp files under `WEB-INF/views`, is this bad practice? – user3690467 Sep 12 '17 at 14:32
  • It depends on what's the purpose of that .jsp page. As [this answer](https://stackoverflow.com/questions/6825907/why-put-jsp-in-web-inf/6825953#6825953) states, the servlet container doesn't let you directly access that page and it would be great for security reasons; but if your page is intended to be accessed that way, you'll either have to specify an url-pattern in `web.xml` or you won't be able to browse it – Emanuele Giona Sep 12 '17 at 14:39
0

I have solved it like this

  • Move the static files like .html from src/main/resources/static/ to src/main/webapp/includes/. e.g. I have my header.html and footer.html here.
  • To include .html file, use <%@include file="????.html" %>
  • To include .jsp file, use <jsp:include page="????.jsp"/>
  • Replace ???? with the relative path of the file
Bobi John
  • 1
  • 1
-2

Try using:

<%@include file="../../resources/static/template/index.html" %>
ShanayL
  • 1,217
  • 2
  • 14
  • 29
Siby
  • 1