以Qqq.html 里的代码为例 对于他来说
1.绝对路径:D:/UP/123.text
2.相对路径:123.text
2.1当前目录
./ 当前目录 也可以直接忽略不写
例如 :./123.text 就等于 D:/UP/123.text
例如 :123.text 就等于 D:/UP/123.text
2.2向上返回一级
../ 向上返回一级
例如 :../123.text 就等于 D:/123.text
2.2向上返回两级 (三级,四级都一个道理)
../../ 类推就是向上返回两级 例如 D:/Q/W/Qqq.html
例如 :../../ 123.text 就等于 D:/123.text
2.3项目的根目录
/ 就是项目的根目录
你的项目路径是D:/app 你的html的路径是D:/app/UP/Qqq.html
例如 :/123.text 就等于 D:/app/123.text
j2ee
假定你的web application 名称为news,你在浏览器中输入请求路径:
http://localhost:8080/news/main/list.jsp
1.1 request.getContextPath()
http://localhost:8080/news/main/list.jsp
打印结果:/news
1.2 request.getServletPath()
http://localhost:8080/news/main/list.jsp
打印结果:/main/list.jsp
1.3 request.getRequestURI()
http://localhost:8080/news/main/list.jsp
打印结果:/news/main/list.jsp
1.4 request.getRealPath("/")
http://localhost:8080/news/main/list.jsp
打印结果:F:\Tomcat 6.0\webapps\news\test
1.5 request.getScheme()
http://localhost:8080/news/main/list.jsp
等到的是协议名称,默认是http
1.6 request.getServerName()
得到的是在服务器的配置文件中配置的服务器名称
http://localhost:8080/news/main/list.jsp
比如:localhost 或者.baidu.com 等等
1.7 request.getServerPort()
得到的是服务器的配置文件中配置的端口号
http://localhost:8080/news/main/list.jsp
比如 8080等等
http://localhost:8080/news/main/list.jsp
request.getScheme() + ":////" +
http ://
+ request.getServerName()+ ":"
localhost :
+ request.getServerPort()
8080
+ request.getContextPath()+ "/";
/news /
(附录增加更新)从request获取各种路径总结
equest.getRealPath() 这个方法已经不推荐使用了,代替方法是:
request.getSession().getServletContext().getRealPath()
在servlet里用this.getServletContect().getRealPath()
在struts里用this.getServlet().getServletContext().getRealPath()
在Action里用ServletActionContext.getRequest().getRealPath();
以上三个获得都是当前运行文件在服务器上的绝对路径
从request获取各种路径总结
request.getRealPath(“url”); // 虚拟目录映射为实际目录
request.getRealPath(“./”); // 网页所在的目录
request.getRealPath(“../”); // 网页所在目录的上一层目录
request.getContextPath(); // 应用的web目录的名称
获取Web项目的全路径
String strDirPath = request.getSession().getServletContext().getRealPath(“/”);
以工程名为TEST为例:
(1)得到包含工程名的当前页面全路径:request.getRequestURI()
结果:/TEST/test.jsp
(2)得到工程名:request.getContextPath()
结果:/TEST
(3)得到当前页面所在目录下全名称:request.getServletPath()
结果:如果页面在jsp目录下 /TEST/jsp/test.jsp
(4)得到页面所在服务器的全路径:application.getRealPath(“页面.jsp”)
结果:D:\resin\webapps\TEST\test.jsp
(5)得到页面所在服务器的绝对路径:absPath=new java.io.File(application.getRealPath(request.getRequestURI())).getParent();
结果:D:\resin\webapps\TEST
2.在类中取得路径:
(1)类的绝对路径:Class.class.getClass().getResource(“/”).getPath()
结果:/D:/TEST/WebRoot/WEB-INF/classes/pack/
(2)得到工程的路径:System.getProperty(“user.dir”)
结果:D:\TEST
3.在Servlet中取得路径:
(1)得到工程目录:request.getSession().getServletContext().getRealPath(“”) 参数可具体到包名。
结果:E:\Tomcat\webapps\TEST
(2)得到IE地址栏地址:request.getRequestURL()
结果:http://localhost:8080/TEST/test
(3)得到相对地址:request.getRequestURI()
结果:/TEST/test