先放代码:
String path = request.getContextPath(); String basePath = request.getScheme() + “://” + request.getServerName() + “:” + request.getServerPort() + path + “/”;下面解释一下代码的作用:
request.getContextPath();
返回项目的名字(应用的名字),如果项目为根目录,则得到一个”“,即空的字条串。 request.getScheme(); 返回当前页面使用的协议,比如: http request.getServerName(); 返回服务器的名字 比如:本机的话就是 localhost request.getServerPort(); 返回当前页面所在服务器的端口 比如: 8080举个例子:
**.jsp这种相对路径的好处就是可以避免路径错误
以上是个人理解,有说的不对的地方 欢迎指正补充。