下面四种方式都可以得到webroot/WEB-INF/classes这个路径,有什么区别,哪种方式最好?
String path = this.getClass().getResource("/").getPath();
String path2 = this.getClass().getClassLoader().getResource("/").getPath();
String path3 = Thread.currentThread().getContextClassLoader().getResource("/").getPath();
String path4 = pageContext.getServletContext().getRealPath("/")+ "/WEB-INF/classes/ ;
 

解决方案 »

  1.   

    好象都不好页面上用这个
    <%=request.getContextPath() %>==webroot
    如果webroot下有个index.jsphref="<%=request.getContextPath() %>/index.jsp"
    如果webroot下有个文件夹a下有index.jsp
    href="<%=request.getContextPath() %>/a/index.jsp"
      

  2.   

    个人认为第三种
    第一种有线程安全问题
    第二种在liunx下不能用是因为liunx不是用“/”而是用“\”所以没有跨平台性
    第四种使用起来不够灵活配置文件不能移动到其它文件夹下
      

  3.   

    那前两种有何区别?一个是通过类来获取资源,一个是通过类加载器获取资源,为什么得到的路径一样?
    楼上说第一种有线程安全问题?能否详解?
    第二种在liunx下不能用是因为liunx不是用“/”而是用“\”,那我在Linux下改成getResource("\")不就可以了?我更想搞清楚这几种内在的区别
      

  4.   

    我用这个:request.getContextPath()