Jsp包括不起作用:找不到文件,状态500

问题描述 投票:0回答:1

我具有这样的目录结构:

enter image description here

试图像这样在header.jsp中包含home.jsp

<%--
  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>

失败,出现org.apache.jasper.JasperException: javax.servlet.ServletException: File [/comediansapp/shared/header.jsp] not found错误。我做错了吗?

java jsp tomcat servlets
1个回答
2
投票

提供相对于当前页面的路径。试试:

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

${pageContext.request.contextPath}是您的应用程序的当前contextPath是comediansapp,因此它将尝试在路径/ comediansapp / shared / header.jsp

上查找文件

请检查:https://stackoverflow.com/a/5850406/4325878

我尝试的完整示例:

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

© www.soinside.com 2019 - 2024. All rights reserved.