java web 相对路径中已/开头和不已/开头的区别

通俗的讲,有/会从跟目录开始算,没有会从当前目录开始算

1.前台页面

​页面中向服务器页面请求静态资源且没有指定<base href="<%=basePath%>">

访问以下页面​

localhost:8080/resolvent/​index.jsp

localhost:8080/resolvent/​servlet/dnf

有/的​

跟目录是域名+端口号(localhost:8080)

请求以下资源​

<script type="text/css" src=''/demo.js"></script>

最终浏览器发送的请求都是​localhost:8080/demo.js

没有/

<script type="text/css" src=''demo.js"></script>​

​localhost:8080/resolvent/demo.js  当前目录localhost:8080/resolvent/

localhost:8080/resolvent/servlet/demo.js​ 当前目录 localhost:8080/resolvent/servlet/

2.后台代码

请求转发,请求重定向,已以下两个url为例

​​localhost:8080/resolvent/servlet/dnf

localhost:8080/resolvent/index.jsp

​有/的

根目录是​(localhost:8080/resolvent)

request.getRequestDispatcher("/login.jsp").forward(request,response);​

最后都会定位到​

​localhost:8080/resolvent/login.jsp

没有/

request.getRequestDispatcher("login.jsp").forward(request,response);​

localhost:8080/resolvent/servlet/login.jsp​  当前目录localhost:8080/resolvent/servlet/

localhost:8080/resolvent/login.jsp​  当前目录localhost:8080/resolvent/

无论在哪里出生的孩子,都能看到整个世界
原文地址:https://www.cnblogs.com/resolvent/p/5736678.html