0

I have directory structure like this:

enter image description here

Trying to include header.jsp in home.jsp like this:

<%--
  Created by IntelliJ IDEA.
  User: Irina
  Date: 31.03.20
  Time: 20:58
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<jsp:include page="${pageContext.request.contextPath}/shared/header.jsp" />
<a href="${pageContext.request.contextPath}/login">Login</a>
<a href="${pageContext.request.contextPath}/signup">Signup</a>

</body>
</html>

fails with org.apache.jasper.JasperException: javax.servlet.ServletException: File [/comediansapp/shared/header.jsp] not found error. What I'm doing wrong?

parsecer
  • 4,758
  • 13
  • 71
  • 140

1 Answers1

2

Provide the path relative to the current page. Try:

<jsp:include page="shared/header.jsp"/>  

${pageContext.request.contextPath} is the current contextPath of the app in your case is comediansapp so it will try to find a file on the path /comediansapp/shared/header.jsp

Please check: https://stackoverflow.com/a/5850406/4325878

Complete example that I tried:

Project

index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<jsp:include page="shared/header.jsp" />
<a href="${pageContext.request.contextPath}/login.jsp">Login</a>
<a href="${pageContext.request.contextPath}/signup.jsp">Signup</a>

</body>
</html>

shared/header.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>

<nav style="height:50px; background:red;">
    <strong> JSP!!! </strong>
</nav>

Working Example: Working Example

vladwoguer
  • 951
  • 1
  • 14
  • 28
  • 1
    Thank you. It started working after I put it out of shared into the same folder. Then I put it back into shared and it kept working. Not sure why. In your answer you provide structure for Eclipse, it seems. Similar enough, but I'd recommend checking out Idea + Maven ; ) Anyways I appreaciate your help and effort, hopefully your answer will help many other people too. – parsecer Mar 31 '20 at 22:48