getServerPort() vs getLocalPort()
843841Dec 24 2007 — edited Jan 23 2008In 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 ?
Cheers,
Adrian Sosialuk