Difference between getLocalPort() and getServerPort() in servlets?

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

In a simple setup (where your application server/servlet container accepts the request of the client directly) both methods will return the same value. 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).

Let's see what the javadoc is saying about getLocalPort(): Returns the Internet Protocol (IP) port number of the interface on which the request was received. And this is what we can read about 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.So, if a client sends a request to mydomain.com:80/ that is then "routed" to a container listening on another port (and maybe another machine), getServerPort() will return 80 and getLocalPort() will return the port of the container to which the request was forwarded, let's say 7001 for a WebLogic instance listening to that port.

I read the API when I first read the question, and it wasn't immediately obvious to me what the difference was until I saw Joachim's answer (and I do this for a living). So I think it's pretty reasonable for the original poster (who is "studying servlets") to struggle with the difference. – Jack Leow Feb 3 '10 at 2:34 @Jack Absolutely, I only pasted the API doc as reference (and for the host header stuff).

– Pascal Thivent Feb 3 '10 at 2:40.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions