使用ServletContext获取webapp目录,使用ClassLoader获取class路径

1.使用ServletContext获取webapp目录

在Servlet中 
String path = getServletContext().getRealPath("/"); 

获取到的路径 /usr/local/tomcat/webapps/taobye taobye 是我的项目名称

1.使用ClassLoader获取class路径

在任意JAVA类中 
this.getClass().getClassLoader().getResource("/").getPath();

获取到的路径 /usr/local/tomcat/webapps/taobye/WEB-INF/classes
正在加载评论...