1.获取服务器IP、端口等

比如,页面内部有一个连接,完整的路径应该是 http://192.168.0.1:8080/myblog/authen/login.do 
其中http://
server/是服务器的基本路径,myblog是当前应用程序的名字,那么,我的根路径应该是那么http://localhost:80/myblog/。 

String path = request.getContextPath(); 
String basePath = request.getScheme()
+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 
<base href=" <%=basePath%>"> 

这个语句是用来拼装当前网页的相对路径的。 

request.getSchema()可以返回当前页面使用的协议,就是上面例子中的“http” 
request.getServerName()可以返回当前页面所在的服务器的名字,就是上面例子中的“localhost" 
request.getServerPort()可以返回当前页面所在的服务器使用的端口,就是8080, 
request.getContextPath()可以返回当前页面所在的应用的名字,就是上面例子中的myblog 

原文地址:https://www.cnblogs.com/zkx4213/p/4892841.html