简体   繁体   English

在servlet中获取tomcat IP和端口

[英]Get tomcat IP and Port in the servlet

I am running multiple instances of my Servlet on 3 different tomcat instances with different ports and IP s, now I want to log which request is coming from which tomcat instance. 我正在3个具有不同portsIP的不同tomcat实例上运行Servlet多个实例,现在我想记录哪个请求来自哪个tomcat实例。

Any idea how can I achieve this? 知道我该如何实现吗? Thanks. 谢谢。

Update 更新

I tried getServerName() from mabac answer, but it sends the host name not the ip. 我从mabac答案尝试了getServerName() ,但是它发送的是主机名而不是IP。 If I send below curl request: 如果我发送以下curl请求:

curl -v  \ -H 'Host:example.com' \ 127.0.0.1:1234/ 

then the serverName is example.com 那么serverName是example.com

ServletRequest.getServerName()
ServletRequest.getServerPort()

In order to get the local server name and port name: 为了获得本地服务器名称和端口名称:

ServletRequest.getLocalName();  // pc-name
ServletRequest.getLocalAddr(); // 127.0.0.1
ServletRequest.getLocalPort();  // 1234

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM