1

I'd like to know which port tomcat uses for my Spring app.

To get the address I use:

InetAddress inetAddress = InetAddress.getLocalHost();
String host = inetAddress.getHostAddress()

But there isn't port info anywhere inside. I don't have application.properties with server.host or server.port specified inside, so environment.getProperty("server.port") returns null.

I've also tried this solution:

@Inject
private ServerProperties serverProperties;
...
serverProperties.getPort()

but it also returns null

I read here that I should use Socket#getLocalPort() but I don't know how to get Socket and a new instance returns default value: -1

There's also a way to do it while capturing request:

ServletRequest.getLocalAddr();
ServletRequest.getLocalPort();

but in my case it's not useful.

Community
  • 1
  • 1
Kamil
  • 1,456
  • 4
  • 32
  • 50
  • 2
    You need this why? Tomcat could be listening at several ports and several IP addresses. The host and port used for any specific request are available as request properties. That should be all you need. – user207421 Jul 13 '16 at 10:54

0 Answers0