无论是jsp还是项目进程要获取一些项目部署的实际路径或者项目的访问路径的,那么如何获取呢?
在web后端:
例如访问地址是:http://localhost:8080/test
request.getContextPath(); 获取到工程名称: /test
request.getServletContext().getRealPath("/");
获取到实际路径例如:E:\java\tomcat7\apache-tomcat-7.0.55\wtpwebapps\test\
request.getRequestURI(); 获取到包含工程名的当前页面全路径:/test/action.do
String path=application.getRealPath(request.getRequestURI());
String realPath1 = "http://" + request.getServerName() + ":" + request.getServerPort() +request.getContextPath()+request.getServletPath().substring(0,request.getServletPath().lastIndexOf("/")+1);
String projectPath = "http://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath();