JavaWeb项目在不同容器中正确获取项目路径

来源:互联网 发布:程序员 简历 编辑:程序博客网 时间:2024/05/14 21:25
import java.io.File;import java.net.URLDecoder;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;public class InitServlet extends HttpServlet {@Overridepublic void init() throws ServletException{String webpath="";webpath = getServletConfig().getServletContext().getRealPath("");System.out.println("path1:========"+webpath);if(null==webpath || "".equals(webpath) || "null".equals(webpath)){webpath=this.getClass().getClassLoader().getResource("/").getPath();webpath=new File(webpath.substring(0,webpath.indexOf("WEB-INF"))).getPath();}webpath=URLDecoder.decode(webpath+"");System.out.println("path2:========"+webpath); //D:\Program Files\apache-tomcat-6.0.35\webapps\Test}}

0 0