获取应用程序服务器下的真实(虚拟)主机名 [英] Getting the real (virtual) host name under an application server

查看:306
本文介绍了获取应用程序服务器下的真实(虚拟)主机名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在Jetty下运行的应用程序,我希望应用程序返回自引用绝对URL(生成RSS提要时,因此客户端必须能够在没有当前URL上下文的情况下工作)。

I have an application running under Jetty, and I want the application to return self referencing absolute URLs (when generating an RSS feed, so a client must be able to work without a "current URL" context).

问题在于我不知道应该在哪个主机名下部署应用程序,并且它很可能会部署在虚拟机中托管环境,其中多个主机名映射到相同的IP地址。我已经尝试使用 HttpServletRequest.getLocalName(),但是返回已接收请求的IP地址的解析主机名[1](通常是在本地主机文件)而不是客户端在HTTP请求中指定的URL中的主机名。

The problem is that I don't know ahead of time under which host name the application will be deployed, and it is quite likely that it will be deployed in a virtual hosting environment where several host names are mapped to the same IP address. I've tried using HttpServletRequest.getLocalName(), but that returns the resolved host name[1] for IP address on which the request was received (usually what is set in the local hosts file) and not the host name from the URL that the client specified in the HTTP request.

基本上我想得到CGI下的环境变量 SERVER_NAME 但它看起来不像Java API提供的那样。任何帮助将不胜感激。

Basically I want to get what under CGI would be the environment variable SERVER_NAME but it doesn't look like the Java API provides this. Any help will be appreciated.

[1]只有我很幸运 - 在某些设置上它只返回IP地址。

[1] only if I'm lucky - on some setups it only returns the IP address.

推荐答案

使用 ServletRequest.getServerName() 从主机标头获取虚拟主机名。这相当于CGI的 SERVER_NAME 变量。

这篇关于获取应用程序服务器下的真实(虚拟)主机名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆