在Jsp或者Servlet中对于路径的获取是经常使用的,通常借助request这个对象获取
可以获取相对路径,也可以获取绝对路径
String path = request.getContextPath();
String servletPath=request.getServletPath();
//相对路径、已经舍弃
String realPath=request.getRealPath("/");
//使用ServletContext获取
request.getServletContext().getRealPath("/")
在jsp页面中
${pageContext.request.contextPath}<br>
${pageContext.request.servletPath}<br>
${pageContext.request.getServletContext().contextPath}
/SSM
/WEB-INF/jsp/File.jsp
/SSM
在Servlet中
System.out.println(request.getServletContext().getRealPath("/"));
System.out.println(request.getServletPath());
System.out.println(request.getContextPath());
System.out.println(request.getRealPath("/"));
C:\Users\IdeaProjects\MavenSpring\SSMOne\target\SSMOne\
/File/file
/SSM
C:\Users\IdeaProjects\MavenSpring\SSMOne\target\SSMOne\