java获取服务器的ip和地址

时间:2024-01-05 17:19:50
HttpServletRequest httpRequest=(HttpServletRequest)request;

String strBackUrl = "http://" + request.getServerName() //服务器地址
+ ":"
+ request.getServerPort() //端口号
+ httpRequest.getContextPath() //项目名称
+ httpRequest.getServletPath() //请求页面或其他地址
+ "?" + (httpRequest.getQueryString()); //参数
InetAddress address = InetAddress.getLocalHost();//获取的是本地的IP地址
String hostAddress = address.getHostAddress();
request.getSession().getServletContext().getRealPath("/");获取项目所在服务器的全路径,如:D:\Program Files\apache-tomcat-7.0.25\webapps\TestSytem\
request.getServletPath()    获取客户端请求的路径名,如:/object/delObject
request.getServerName()     获取服务器地址,如:localhost
request.getServerPort()    获取服务器端口,如8080
request.getContextPath()    获取项目名称,如:TestSytem
request.getLocalAddr()     获取本地地址,如:127.0.0.1
request.getLocalName()    获取本地IP映射名,如:localhost
request.getLocalPort()     获取本地端口,如:8090
request.getRealPath("/")    获取项目所在服务器的全路径,如:D:\Program Files\apache-tomcat-7.0.25\webapps\TestSytem\
request.getRemoteAddr()    获取远程主机地址,如:127.0.0.1
request.getRemoteHost()    获取远程主机,如:127.0.0.1
request.getRemotePort()    获取远程客户端端口,如:3623
request.getRequestedSessionId()     获取会话session的ID,如:823A6BACAC64FB114235CBFE85A46CAA
request.getRequestURI()     获取包含项目名称的请求路径,如:/TestSytem/object/delObject
request.getRequestURL().toString()  获取请求的全路径,如:http://localhost:8090/TestSytem/object/delObject