File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes getServerPort() vs getLocalPort() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "getServerPort() vs getLocalPort()" Watch "getServerPort() vs getLocalPort()" New topic

getServerPort() vs getLocalPort()

Adrian Sosialuk
Ranch Hand

Joined: Jun 07, 2007
Posts: 57
Hi everybody !

Could someone please explain me why requests are forwarded to different
ports on the server ?

I understand, that getServerPort() returns the original port number that
the request was made to and that getLocalPort() returns the current one
that is using but why would we need it ? Can't we handle multiple client's
connections on the same port ? And secondly, how is it achieved ?
I mean normally, a container gets request, creates or allocates a thread
which uses servlet's object to handle the request. So when the request is
made, it uses containers port number to communicate with it (so socket for
a container is created with an associated port number). Where does the new
port number fit in this scenario ? Client sends some new data to the new
port number and .... what happens on the server side ?

Thanks and Merry Christmas Everybody !!!

Adrian Sosialuk
David O'Meara

Joined: Mar 06, 2001
Posts: 13459

If (for example) you put Apache in front of Tomcat, the port that Apache serves content on may not be the same as the one that Tomcat listens on. Likewise, if you use port forwarding on a router you could have two different ports depending on whether you are talking about external or internal requests.
Adrian Sosialuk
Ranch Hand

Joined: Jun 07, 2007
Posts: 57
Hi David !

Thanks for you prompt reply.

Well - what you have said makes sense for me, but I'm wondering
about another context. In HFSJ you can read: "The difference between
getLocalPort() and getServerPort() is more subtle - getServerPort()
says, 'to which port was the request originally sent' while getLocalPort()
says, 'on which port did the request end up ?' Yes, there is a difference,
because although the requests are sent to a single port (where the server is listening), the server turns around and finds a different local port for
each thread, so that the app can handle multiple clients at the same time."

I don't understand why the app wouldn't be able to handle multiple clients
at the same time without having different port numbers for each thread ...

And when it happens, does it mean that container creates socket for each
client and container then starts listening on those ports ? It would mean
that container needs some kind of connection manager so it knows what
client connects on which port and forwards to which threads. So I see it
this way (assuming there is only tomcat standalone) : client connects
to tomcat on port let's say 8080. Container creates/allocates a thread,
loads the servlet, creates another socket with port let's say 8081, passes
the request through the thread to servlet and it generates the output.
Next time the same client sends request, it uses port 8081 on which the
container is now listening and the story continues ... Am I right here ?? :/

Also, would it be done that way for efficiency ?


Adrian Sosialuk
I agree. Here's the link:
subject: getServerPort() vs getLocalPort()
It's not a secret anymore!