zl程序教程

您现在的位置是:首页 >  后端

当前栏目

java中取得上下文路径的方法详解编程语言

JAVA方法编程语言 详解 路径 取得 上下文
2023-06-13 09:11:51 时间

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

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

3.在任意的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等的符号。

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/19521.html

c