从request获取各种路径总结 request.getRealPath("url"); // 虚拟目录映射为实际目录 request.getRealPat
2015-07-07 10:51
701 查看
转:http://zjutsoft.iteye.com/blog/1084260 从request获取各种路径总结 request.getRealPath("url"); // 虚拟目录映射为实际目录 request.getRealPath("./"); // 网页所在的目录 request.getRealPath("../"); // 网页所在目录的上一层目录 request.getContextPath(); // 应用的web目录的名称 如http://localhost:7001/bookStore/ /bookStore/ => [contextPath] (request.getContextPath()) 获取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
相关文章推荐
- quickSort
- Java break [flag]; 与continue [flag]; 区别
- IOS-UICollectionViewLayout
- [SoapUI] 通过Groovy写文本文件
- easyui 中datagrid获取选中行的行号
- Leetcode 232 Implement Queue using Stacks 和 231 Power of Two
- [LeetCode] Implement Queue using Stacks 用栈来实现队列
- JAVA 中 CharSequence 类型
- 华为S8508交换机的问题
- [SoapUI] context.expand 和 groovyUtils.getXmlHolder 有什么不一样
- 从客户端(hidXML="<FlexCell xml:space=...")中检测到有潜在危险的Request.Form值
- [XCode] 默认源文件和build目录
- 不要直接对Request.Headers["If-Modified-Since"]使用Convert.ToDateTime
- UIScrollView详解
- windows 10 build 10162无法识别网卡怎么办?
- ext笔记(te be continued..)
- 全新Win10预览版本周推送,或为Build 10164
- 视频:Win10 Mobile Build 10158模拟器上手视频演示
- EasyUi 中datagrid 实现查询方法
- [SoapUI] 通过Groovy获取SoapUI当前Project所在的目录