假定项目名为test
http://localhost:8080/test/main/index.jsp
1.1 System.out.println(request.getContextPath());
打印结果:/news
1.2 System.out.println(request.getServletPath());
打印结果:/main/index.jsp
1.3 System.out.println(request.getRequestURI());
打印结果:/test/main/index.jsp
1.4 System.out.println(request.getRealPath("/"));
打印结果:E:\java\Tomcat 6.0\webapps\test\
Class.getResource(String path)
path不以’/’开头时,默认是从此类所在的包下取资源;
path 以’/’开头时,则是从ClassPath根下获取;
Class.getClassLoader().getResource(String path)
path不能以’/’开头时;
path是从ClassPath根下获取;
getResourceAsStream方法和上面一样。
System.getProperty(“user.dir”)获取项目的根目录