gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:26:10 25 4
gpt4 key购买 nike

在我们私有(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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com