import java.io.File;
import java.net.URLDecoder;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
public class InitServlet extends HttpServlet {
@Override
public 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
}
}
JavaWeb项目在不同容器中正确获取项目路径
最新推荐文章于 2024-05-30 10:51:27 发布