packagecom.test.dir;importjava.io.File;importjava.io.IOException;importjavax.servlet.ServletContext;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjavax.servlet.http.HttpSession;public class TomcatDir extendsHttpServlet {public voiddoGet(HttpServletRequest request, HttpServletResponse response)throwsServletException, IOException {
System.out.println(System.getProperty("user.dir")); //D:\apache-tomcat-6.0.41\bin
System.out.println(System.getProperty("catalina.home")); //D:\apache-tomcat-6.0.41 不建议使用,获取的是环境变量中tomcat的位置
HttpSession session=request.getSession();
ServletContext application=session.getServletContext();
String serverRealPath= application.getRealPath("/") ;
System.out.println(serverRealPath); //D:\apache-tomcat-6.0.41\webapps\test2\
File f= new File(getClass().getResource("/").getPath());
f= new File(f.getPath().replace("%20"," ") + "/databasePool.conf");
System.out.println(f.getAbsolutePath()); //D:\apache-tomcat-6.0.41\webapps\test2\WEB-INF\classes\databasePool.conf
String path=request.getContextPath();
String basePath= request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
System.out.println(basePath); //http://localhost:8080/test2/
System.out.println(request.getContextPath()); // /test2
}public voiddoPost(HttpServletRequest request, HttpServletResponse response)throwsServletException, IOException {
doGet(request, response);
}
}