简体   繁体   中英

pageContext.request.contextPath not working

I'm using Tomcat 6.0.32 and I'm trying to include a JSP file into another, but somehow the file cannot be found (yes, the file exists). Here is the file structure and code:

<jsp:include page="${pageContext.request.contextPath}/templates/header.jsp">
</jsp:include>

WebContent
- folder/caller.jsp
- templates/header.jsp

However, if I use ${pageContext.request.contextPath} outside of that to check if it outputs correctly, it works, does anyone know why this is happening?

I'm not even sure if you can use runtime variables inside a <jsp:include> , but the other problem is that the contextPath refers to the context of the URL being requested from the server, and does not necessarily correspond to your filesystem layout.

Why wouldn't you just use <jsp:include page="templates/header.jsp"> ?

i completely agree with the above answer however if u have to use it this way then the below code shd work

<c:set var="myContext" value="${pageContext.request.contextPath}"/>
<jsp:include page="${myContext}/templates/header.jsp"> </jsp:include> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM