javaWeb 下的各种路径获取方法
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
response.setContentType("text/html;charset=utf-8");
//1. 获取当前appWeb的根目录的绝对路径
String path = getServletContext().getRealPath("");
out.println(path+"<br>");
//也能获取到当前appWeb的根目录的绝对路径
path = request.getRequestURI();
out.println(path+"<br>");
//2. 获取当前appWeb的根目录的相对路径, 以tomcat的应用根目录为参考点,一般情况下,在JSP/HTML页面等引用的CSS,Javascript.Action等属性前面最好都加上<%=request.getContextPath()%>,以确保所引用的文件都属于Web应用中的目录
path = request.getContextPath();
out.println(path+"<br>");
//3. 获取WEB-INF下的classes目录路径
path = this.getClass().getClassLoader().getResource("").getPath();
out.println(path+"<br>");
//4. 获取WEB-INF下的classes目录路径的上级目录。
path = new File(this.getClass().getClassLoader().getResource("").getPath()).getParent();
out.println(path+"<br>");
//5. 获取tomcat服务器web应用的临时工作目录
File file = (File)getServletContext().getAttribute("javax.servlet.context.tempdir");
path = file.getAbsolutePath();
out.println(path+"<br>");
}