This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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 ?
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.
Joined: Jun 07, 2007
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 ?? :/