java servlet 中 getLocalPort() 和 getServerPort() 的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2184286/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 19:55:11  来源:igfitidea点击:

Difference between getLocalPort() and getServerPort() in servlets

javaservlets

提问by giri

I am studying servlets, I have come up with a doubt difference between getLocalPort()and getServerPort(). Here the getLocalPort()means server side only then whats the meaning of getServerPort()?

我正在研究 servlet,我对getLocalPort()getServerPort(). 这里的getLocalPort()意思是服务器端才什么意思getServerPort()

Thanks.

谢谢。

回答by Joachim Sauer

In a simple setup (where your application server/servlet container accepts the request of the client directly) both methods will return the same value.

在一个简单的设置中(您的应用程序服务器/servlet 容器直接接受客户端的请求)这两种方法将返回相同的值。

When the servlet container is not the application that accepts the client request (for example when you use an Apache HTTP server with mod_jk to accept the request and forward the request to a Tomcat instance via AJP), then getServerPort()will return the port the client connected to (probably 80, when the default port is used) and getLocalPort()will return the port that Tomcat used to accept the connection from the HTTP server (probably 8009 or something similar).

当servlet容器不是接受客户端请求的应用程序时(例如当您使用带有mod_jk的Apache HTTP服务器接受请求并通过AJP将请求转发到Tomcat实例时),getServerPort()则将返回客户端连接的端口(可能是 80,当使用默认端口时)getLocalPort()并将返回 Tomcat 用来接受来自 HTTP 服务器的连接的端口(可能是 8009 或类似的东西)。

回答by Pascal Thivent

Let's see what the javadoc is saying about getLocalPort():

让我们看看 javadoc 是怎么说的getLocalPort()

Returns the Internet Protocol (IP) port number of the interface on which the request was received.

返回接收请求的接口的 Internet 协议 (IP) 端口号。

And this is what we can read about getServerPort():

这就是我们可以阅读的内容getServerPort()

Returns the port number to which the request was sent. It is the value of the part after ":" in the Host header value, if any, or the server port where the client connection was accepted on.

返回请求发送到的端口号。它是 Host 标头值中“:”之后的部分的值(如果有),或者是接受客户端连接的服务器端口。

So, if a client sends a request to http://mydomain.com:80/that is then "routed" to a container listening on another port (and maybe another machine), getServerPort()will return 80and getLocalPort()will return the port of the container to which the request was forwarded, let's say 7001for a WebLogic instance listening to that port.

因此,如果客户端向http://mydomain.com:80/发送请求,然后该请求被“路由”到侦听另一个端口(也可能是另一台机器)的容器,getServerPort()将返回80并将getLocalPort()容器的端口返回到请求被转发,假设7001一个 WebLogic 实例侦听该端口。

回答by Haixi Liu

I looked at Jetty and Tomcat's implementation of the server port, and this is my findings:

我查看了 Jetty 和 Tomcat 对服务器端口的实现,这是我的发现:

  1. When request URL has a port number in it (e.g. http(s)://website.com:8888/blah/blah-blah), the getServerPort() always return what was included in the request URL (in the example, 8888)
  2. When request URL does not have a port number in it, then
    • 443 will be returned if
      • Tomcat sees SSLEnabled=true
      • Jetty sees schema=https
    • 80 will be returned if
      • sees SSLEnabled=false
      • Jetty sees schema=http
  1. 当请求 URL 中包含端口号(例如 http(s)://website.com:8888/blah/blah-blah)时,getServerPort() 始终返回请求 URL 中包含的内容(在示例中为 8888 )
  2. 当请求 URL 中没有端口号时,则
    • 443 将被返回,如果
      • Tomcat 看到 SSLEnabled=true
      • Jetty 看到 schema=https
    • 80 将被退回,如果
      • 看到 SSLEnabled=false
      • Jetty 看到 schema=http

Therefore, getServerPort() has nothing to do with apache HTTP server or load balancer sitting in front of it (if any), or IP table routes sitting on Tomcat/Jetty server. They will always return standard port number based on http/https, unless a port number is specified in the request URL.

因此,getServerPort() 与位于其前面的 apache HTTP 服务器或负载均衡器(如果有)或位于 Tomcat/Jetty 服务器上的 IP 表路由无关。它们将始终基于 http/https 返回标准端口号,除非在请求 URL 中指定了端口号。