从request获取各类路径总结
request.getRealPath("url"); // 虚拟目录映射为实际目录java
request.getRealPath("./"); // 网页所在的目录web
request.getRealPath("../"); // 网页所在目录的上一层目录服务器
request.getContextPath(); // 应用的web目录的名称app
例如 http://localhost:8080/bookStore/
/bookStore/ => [contextPath] (request.getContextPath())webapp
获取Web项目的全路径
String strDirPath = request.getSession().getServletContext().getRealPath("/");jsp
以工程名为TEST为例:url
(1)获得包含工程名的当前页面全路径:request.getRequestURI()
结果:/TEST/test.jspget
(2)获得工程名:request.getContextPath()
结果:/TESTio
(3)获得当前页面所在目录下全名称:request.getServletPath()
结果:若是页面在jsp目录下 /TEST/jsp/test.jspclass
(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