您的位置:首页 > 编程语言 > Java开发

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

2014-03-20 22:31 323 查看
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 servlet Java