The problem of using absolute path in jsp:include in jsp

http://www.iteye.com/problems/34149/


The error reported by  <jsp:include page="  /project name/templates/header.jsp "></jsp:include>  is org.apache.jasper.JasperException: javax.servlet.ServletException: File "  /project name/templates /header.jsp " not found  The page that cannot be found is the page given by the parameter "/project name/templates/header.jsp"  This path after removing the leading "/" should be absolutely fine, unless this "/ "Does not represent the root directory (server address) of  the  server <jsp:include page="  http://localhost:8080/project name/templates/header.jsp ">  The error reported is  org.apache.jasper.JasperException: javax .servlet.ServletException: File " /templates/http:/localhost:8080/project name/templates/header.jsp " not found  The page that cannot be found is more than the parameter /templates/ 












That is to say, there is no "special treatment" because the parameter starts with "http://", but it is treated as a normal string, and it is treated as a relative path because it does not start with "/". And added "/templates/" in front of 
it. If so, this "/templates/" should be its own location. According to this reasoning, this "/" represents? ?




Experimented again 

<jsp:include page="  ../templates/ a non-existent page "></jsp:include> 
reported an error as 
org.apache.jasper.JasperException: javax.servlet.ServletException: File "  /templates /A non-existent page " not found 



Conclusion I observed it, thought about it, and got it. It turned out that I made a big circle. The problem is actually very simple:  To summarize, the application of the <jsp:include> tag in JSP:  1. Dynamic syntax such as <%= %> is not supported, especially the value of the page attribute can only be a hard-coded string (I just heard that there are some special syntaxes of its own, which I will learn later)  2, For the path problem of the imported page, two application methods of "absolute path" and "relative path" can also be used.     An absolute path is a path starting with "/", and other paths, including those starting with "http://", are relative paths, and "./", "../", etc. can be used.  









   相对路径 没什么好说的。强调一点,这里的相对路径也是相对于访问页面的URL的路径,本质上与页面文件在项目中的存储位置没直接关系,所以这里用相对路径时也存在由于访问URL不确定带来的问题(是不是这样呢?如文章开头说的,这里都是我自己的结论)。 
  
    主要是绝对路径的问题: 
     我们知道大部分情况下写在页面(不管静态还是动态)里并输出到IE中的路径都是由IE来解析的,以“/”开头的绝对路径 这个“/”代表的是服务器的根目录:比如访问URL为 http://localhost:8080/项目名/templates/template.jsp 
那么“/”代表的就是 "http://localhost:8080/" 而并不带有项目名信息。 

而<jsp:include>引入页面的过程,是在服务器运行向IE输出数据的过程中由JAVA程序调用执行,路径也当然由JAVA程序来解析,此时以"/"开头的绝对路径中的“/” 代表的已经是本项目的根目录了。 
比如 URL为 http://localhost:8080/项目名/templates/template.jsp 
那么“/”代表的就是 "http://localhost:8080/项目名/"。已经带有了项目名信息。 

同时,也不用像引入JS CSS文件那样在路径前加上 <%=request.getContextPath()%> 来解决项目名本身修改的问题了。只需一个“/”就什么也不用管了。 


如上所述: 
需求需要的这段代码其实很简单 

引用
<jsp:include page="/templates/header.jsp"></jsp:include>


需要被任意位置的页面引入的页面 存放在项目根目录下的templates目录下 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325979488&siteId=291194637
jsp