Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting host name in java

Is it possible to detect the hostname used in java (even through a cname)?

I am looking for something similar to this: http://api.drupal.org/api/function/conf_path/6

In that code, drupal checks to see what domain name is accessing it in order to load the correct configuration (multi-site). If the domain doesn't match any configurations, it just goes to the default configuration.

like image 332
knpwrs Avatar asked Mar 22 '26 11:03

knpwrs


1 Answers

Have a look at InetAddress.getLocalHost(), followed by calling getHostName() on the returned value, which does exactly this.

Be aware that not all machines will be able to resolve a network interface which can be used for the local hostname, and that multi-homed machines run into some additional complications too. Thus if you're developing software that will need to work in a variety of situations, consider the edge cases properly as well as the happy path.

like image 85
Andrzej Doyle Avatar answered Mar 24 '26 23:03

Andrzej Doyle



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!