
1. 路劲可分为 绝对路径 和 相对路径
2. 绝对路径 (开头带"/")
前端: http://localhost:8080/myWebApp/user/login.jsp
/myWebApp/user/login.jsp
后端: /user/login.jsp
这里可以看出一个区别, 其中"/"这个斜杠在前后端有着不同的意义:
前端--> (表示) 服务器的根路径(例如:http://localhost:8080/)
后端-->(表示) 应用的根路径 (例如: http://localhost:8080/yourWebAppName/)
3. 相对路径 (开头不带"/")
举个例子, 假如当前的路径是 http://localhost:8080/myWebApp/user/login.jsp
那么这时候访问 “main.jsp”页面,即访问的是 http://localhost:8080/myWebApp/user/main.jsp
也就是说,使用相对路径,那么访问的是 当前页面路径 所属 目录 下的文件路径。
这个特性无论是在前端 、 后端 都是一样的。
在java中,无论是JSP或者Servlet , 都可以通过request这个对象获得requestURL( ) , 该方法返回当前所访问的页面的绝对路径的一个StringBuilder。
应用服务器正是通过该路径来得到用户想要访问的页面路径。
而一般使用forward时,就是通过修改该路径(无论当前指定的是绝对路径或者相对路径),来请求新的路径。
Base path :scheme+serverName+serverport+requestContext): http://192.168.10.64:8080 //应用路径
req.reqeustURL(): http://192.168.10.64:8080/testPath/test.do //请求路径
req.requestURI(): /testPath/test.do //请求路径(相对服务器根目录部分)
req.getSession.getServletContext().getRealPath("")F:\tomcat\apache-tomcat-7.0.47\webapps\testPath
//根目录的真实路径
req.getSession().getServletContext().getResource("/") : jndi:/localhost/testPath/index.jsp //这里必须要‘/'开头
req.getServletPath(): /test.do //servlet的映射路径
req.getSession().getServletContext().getContextPath() : /testPath //应用上下文路径(标准的)
req.getContextPath(): //请求request 所指向的应用上下文路径 一般为应用路径,即/AppName (如果场景是把整个应用服务器当成一个应用,所以应用上下文为"/testPath")
testPath App
/testPath
req.getRealPath("file.upload") :
F:\tomcat\apache-tomcat-7.0.47\webapps\testPath\file.upload
req.getSession().getServletContext().getResource("/") :
jndi:/localhost/testPath/index.jsp
req.getSession.getServletContext().getRealPath("") :
F:\tomcat\apache-tomcat-7.0.47\webapps\testPath
req.getSession().getServletContext().getContextPath() :
/testPath
req.getRequestURL() :
http://192.168.10.64:8080/testPath/test.do
req.getRequestURI() :
/testPath/test.do
req.getAuthType() :
null
req.getMethod() : :
GET
req.getQueryString() :
null
req.getContentType() :
null
req.getContextPath() :
/testPath
req.getContentLength() :
-1
req.getLocale() :
zh_CN
req.getLocalAddr() :
192.168.10.64
req.getLocalName() :
c-PC
req.getLocalPort() :
8080
req.getRemoteAddr() :
192.168.10.64
req.getRemoteHost() :
192.168.10.64
req.getRemotePort() :
61783
req.getRemoteUser() :
null
req.getScheme() :
http
req.getServerName() :
192.168.10.64
req.getServerPort() :
8080
req.getServletPath() :
/test.do
/testPath
req.getScheme()+"://"+req.getServerName()+":"+req.getServerPort()+path+"/" :
http://192.168.10.64:8080/testPath/ ROOT
req.getContextPath() :
""
req.getRealPath("file.upload") :
F:\tomcat\apache-tomcat-7.0.47\webapps\ROOT\file.upload
req.getSession().getServletContext().getResource("/") :
jndi:/localhost/index.jsp
req.getSession.getServletContext().getRealPath("") :
F:\tomcat\apache-tomcat-7.0.47\webapps\ROOT
req.getSession().getServletContext().getContextPath() : req.getRequestURL() :
http://192.168.10.64:8080/test.do
req.getRequestURI() :
/test.do
req.getAuthType() :
null
req.getMethod() : :
GET
req.getQueryString() :
null
req.getContentType() :
null
req.getContextPath() : req.getContentLength() :
-1
req.getLocale() :
zh_CN
req.getLocalAddr() :
192.168.10.64
req.getLocalName() :
c-PC
req.getLocalPort() :
8080
req.getRemoteAddr() :
192.168.10.64
req.getRemoteHost() :
192.168.10.64
req.getRemotePort() :
61836
req.getRemoteUser() :
null
req.getScheme() :
http
req.getServerName() :
192.168.10.64
req.getServerPort() :
8080
req.getServletPath() :
/test.do req.getScheme()+"://"+req.getServerName()+":"+req.getServerPort()+path+"/" :
http://192.168.10.64:8080/