java - 在哪种情况下 getRemoteHost 方法返回 IP 地址而不是主机名?

标签 java networking

在我们私有(private)网络的服务器上,我们有一个 HttpServlet,它由同一网络的 PC 联系。

我们需要知道联系服务器的客户端的主机名。为此,我们称 HttpServletRequest 的 getRemoteHost 方法。

有时此方法会返回客户端的 PC 名称(需要的行为),有时此方法会返回 IP 地址。 (同一个客户端,同一个服务器,同一个私有(private)网络)

API说:

java.lang.String getRemoteHost()

Returns the fully qualified name of the client or the last proxy that sent the request. If the engine cannot or chooses not to resolve the hostname (to improve performance), this method returns the dotted-string form of the IP address. For HTTP servlets, same as the value of the CGI variable REMOTE_HOST

Returns: a String containing the fully qualified name of the client

我看到对于 HTTP servlet,该值与 CGI 变量 REMOTE_HOST 相同。这是什么意思?是否由服务器决定解析地址?有没有办法强制这种行为?

最佳答案

例如,在 Tomcat 中,连接器有一个设置“enableLookups”,出于性能原因,该设置在默认情况下是禁用的。参见 http://tomcat.apache.org/tomcat-7.0-doc/config/http.html

其他容器可能有不同的方法来做同样的事情。

关于java - 在哪种情况下 getRemoteHost 方法返回 IP 地址而不是主机名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14849543/

相关文章:

java - GSON 的自定义打印格式

java - 使用自定义 Json 输出在 DropWizard 中进行 Bean 验证

java - 使用 Birt 脚本从字符串中提取数字

java - 网络和多平台中的 Endian - 澄清吗?

c# - 运行多个线程时程序执行缓慢

elasticsearch - 如何从外部访问存储在Docker容器中的Elasticsearch?

Java String pool相关疑惑

Java9 JNLP --add-opens 不工作

c++ - 如何在 C++ 中使用 snmpwalk

linux - 如何在 FreeBSD 中使用 TCP_NOPUSH 调用 sendfile 之前添加 header