String cla = this.getClass().getClassLoader().getResource(".").getPath();
取得class的文件目录之后 ,然后找其上层父目录

解决方案 »

  1.   

    servletContext.getRealPath("/index.html") 
      

  2.   

    <%String baseDir=servletContext.getRealPath("/") %>
      

  3.   

    请看清要求:
    如果没有用户的请求没有用户的请求,能得到servletContext吗?所以我的最正确:
    String cla = this.getClass().getClassLoader().getResource(".").getPath();
    取得class的文件目录之后 ,然后找其上层父目录
      

  4.   

    wuyg(平平) cool.
    不过一般用Thread.currentThread().getContextClassLoader()来取得类加载器比较保险.
      

  5.   

    我说的是web应用的根目录,照你们的方法,我获得的是D:/Program Files/jakarta-tomcat-5.0.28/common/classes/,而不是D:/Program Files/jakarta-tomcat-5.0.28/webapps/test
      

  6.   

    写一个类,实现ServletContextListener接口,在contextInitialized(ServletContextEvent sce)方法中调用sce.getServletContext().getRealPath("/");就得到web的根目录了。