一 、问题描述
当我们项目中,服务端与客户端相互请求 , 我们应该什么怎么写路径呢???
二、路径的构成
ip+端口+url
三、请求路径的分类
请求路径分为 客户端路径 和 服务器端路径
客户端 :页面相关的(html 、jsp)
服务器端 :服务器内部的(重定向、请求转发 )
四、客户端路径分类 、重定向
超链接、表单都是客户端路径
客户端路径可以分为三种方式:
绝对路径 、相对路径(以“/”开头) 、相对路径(不以“/”开头)
1. 超链接(a 标签):
绝对路径:
<a href="http://localhost:8080/hello/index.html">链接1</a>
相对路径:
以“/”开头的相对路径:
<a href="/hello/pages/index.html">链接2</a>
不以“/”开头的相对路径:
<a href="index.html">链接3</a>
注意 : hello是项目名字
2.表单:
绝对路径:
<form action="http://localhost:8080/hello2/index.html">
<input type="submit"value="表单1"/>
</form>
相对路径:
以“/”开头的相对路径:
<form action="/hello2/index.html">
<input type="submit"value="表单2"/>
</form>
不以“/”开头的相对路径:
<form action="index.html">
<input type="submit"value="表单3"/>
</form>
五 、服务端路径分类
只有相对路径 ,没有绝对路径的这个说法
"redirect : findAll" : localhost:8080/hello/上级路径名/findAll
"redirect : /findAll" : localhost:8080/hello/上级路径名/findAll
两者区别 :
不加入/ 会自动加入你所处的位置
加入 / 项目路径+位置
建议 以/ 开头
六、扩展
路径拼接:
- 上下文路径
${pageContext.request.contextPath} :
${pageContext.request.contextPath}/pages/index.html
${pageContext.request.contextPath} 就相当于 带项目的路径 /hello+拼接的地址
打印出来是 带/ 的相对路径 /hello/pages/index.html
带/ 的相对路径 直接拼接到http://localhost:8080后面的地址i
2. 拼接路径
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
七 、小结
客户端:
超链接,表单 :
以”/”开头的路径相对于主机根目录:http://localhost:8080
不以"/"开头的路径是相对于当前页面的路径 (同一级的 处于同一个目录下的)
服务端:
服务器端的必须是相对路径,不能是绝对路径 没有绝对路径这一说法
请求转发、重定向:
以“/”开头的的路径相对项目根目录【http://localhost:8080/项目名称/】
不以’/'开头的相对路径是相对于当前的请求的路径