java中检测主机名
是否可以检测java中使用的主机名(甚至通过cname)?
我正在寻找类似的东西: http://api.drupal.org/api/function/conf_path/6
在该代码中,drupal 检查哪些域名正在访问它,以便加载正确的配置(多站点)。如果该域与任何配置都不匹配,则只会转到默认配置。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看看 InetAddress.getLocalHost( ),然后调用 getHostName() 返回的值,它正是这样做的。
请注意,并非所有计算机都能够解析可用于本地主机名的网络接口,并且多宿主计算机也会遇到一些额外的复杂情况。因此,如果您正在开发需要在各种情况下工作的软件,请正确考虑边缘情况以及最佳路径。
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.
当然,这样做会导致每个 getHostName() 调用进行反向 DNS 查找。
对于繁忙的网站来说,这是一种性能损失。
我建议执行 getHostAddress() 然后查找主机名。
Of course doing this causes a reverse DNS lookup for each getHostName() call.
It is a performance penalty on busy sites.
I would recommend doing getHostAddress() and later on looking up the host names.