首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

取得下上文路径

2012-10-26 
取得上下文路径1.在任意的class里调用URLDecoder.decode(this.getClass().getClassLoader().getResource(

取得上下文路径
1.在任意的class里调用
URLDecoder.decode(this.getClass().getClassLoader().getResource("/").getPath(), "UTF-8");
将获得当前class的全路径。
如 : C:\Program Files\Apache Software Foundation\Tomcat 5.0\webapps\clientweb\WEB-INF\classes
URLDecoder.decode负责把编码还原,不然路径中就会有类似于%20%ED等的符号。

2.可以在servlet的init方法里
String path = getServletContext().getRealPath("/");
这将获取web项目的全路径
例如:\eclipse\workspace\tree\
tree是web项目的根目录

3.request.getContextPath();
获得web根的上下文环境
如 /tree
tree是web项目的root context

热点排行