Get IP tomcat server

S

Sylvain

Hi !

I am using Tomcat and i would like to get the host IP from the
servletContext object. Does anybody know how to get it ?

Thank you for your help

Best regards

Sylvain Caillet
 
A

Arne Vajhøj

Sylvain said:
I am using Tomcat and i would like to get the host IP from the
servletContext object. Does anybody know how to get it ?

request.getLocalAddr()

Arne
 
S

Sylvain

Hi !

Thank you Arne for this answer but i would like to get this data at the
start of the context not at the first request. That's why i was looking for
getting the server's IP with the servletContext.

Best regards

Sylvain
 
S

Sylvain

I have done it by getting the output stream of a IFCONFIG command shell and
parsing it to find the first IP with regular expressions. It works fine.

Thank you all

Sylvain
 
N

Nigel Wade

Sylvain said:
I have done it by getting the output stream of a IFCONFIG command shell and
parsing it to find the first IP with regular expressions. It works fine.

Does the static method:
NetworkInterface.getNetworkInterfaces()
not work in a servlet? It should provide the same information as ifconfig, but
without the overhead of creating a Process and parsing the output. Also,
parsing the output of ifconfig is almost certainly not portable.

Your original request was to extract the information from the servletContext,
which AFIAK is not possible.
 
A

Arne Vajhøj

Sylvain said:
Thank you Arne for this answer but i would like to get this data at the
start of the context not at the first request. That's why i was looking for
getting the server's IP with the servletContext.

That is not necessarily unique.

You can lookup all the IP addresses on the system. And
pick a random one of those.

Enumeration e = NetworkInterface.getNetworkInterfaces();
while(e.hasMoreElements()) {
NetworkInterface ni = (NetworkInterface)e.nextElement();
Enumeration e2 = ni.getInetAddresses();
while (e2.hasMoreElements()){
InetAddress ip = (InetAddress)e2.nextElement();
// save ip somewhere
}
}

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top