1

Currently i have a .jsp project where my welcome page is a servlet

<welcome-file>frontpage</welcome-file>

The servlet sets gets two ressources, a header file containing the < nav> and a footer containing the < footer>

request.setAttribute("header1", sc.getResource("/includes/nav.jsp").toString());
request.setAttribute("footer", sc.getResource("/includes/footer.jsp").toString());

And forwards to the index.jsp page

getServletContext().getRequestDispatcher("/WEB-INF/jsp/index.jsp").forward(request, response);

My question is. When i get the ressource (footer.jsp), how can i in the footer.jsp dynamically import / include images?

I tried the following

<img src="${pageContext.request.contextPath}/images/picture1.png" alt="picture1"/>

But the expression ${pageContext.request.contextPath} gets treated as a string instead of a command, and does not get the context path.

I suspect its because the content of the footer.jsp is fetched in this manner and their for the context path isint actually ever requested within the footer.jsp.

But how do i solve this?

2 Answers2

3

add <%@ page isELIgnored="false" %> in top of your JSP page, to enable expression language.

and to include a JSP page with other use <jsp:include like:

<jsp:include page="/includes/nav.jsp"/>
<jsp:include page="/includes/footer.jsp"/>
Abhishek Nayak
  • 3,732
  • 3
  • 33
  • 64
1

This is not the way to include stuff. Use jsp:include action to include the header/footer. If for some reason you really want to do it in the servlet, see this post. As long as you just grab a resource like you do, you're reading the file like any text, there is no JSP compilation/evaluation.

Community
  • 1
  • 1
kaqqao
  • 12,984
  • 10
  • 64
  • 118